master
go 80 lines 1.71 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package proxysql
4
5 type (
6 cache struct {
7 commands map[string]*commandCache
8 users map[string]*userCache
9 backends map[string]*backendCache
10 hostgroups map[string]*hostgroupCache
11 }
12 commandCache struct {
13 command string
14 hasCharts, updated bool
15 }
16 userCache struct {
17 user string
18 hasCharts, updated bool
19 }
20 backendCache struct {
21 hg, host, port string
22 hasCharts, updated bool
23 }
24 hostgroupCache struct {
25 hg string
26 hasCharts, updated bool
27 }
28 )
29
30 func (c *cache) reset() {
31 for k, m := range c.commands {
32 c.commands[k] = &commandCache{command: m.command, hasCharts: m.hasCharts}
33 }
34 for k, m := range c.users {
35 c.users[k] = &userCache{user: m.user, hasCharts: m.hasCharts}
36 }
37 for k, m := range c.backends {
38 c.backends[k] = &backendCache{hg: m.hg, host: m.host, port: m.port, hasCharts: m.hasCharts}
39 }
40 for k, m := range c.hostgroups {
41 c.hostgroups[k] = &hostgroupCache{hg: m.hg, hasCharts: m.hasCharts}
42 }
43 }
44
45 func (c *cache) getCommand(command string) *commandCache {
46 v, ok := c.commands[command]
47 if !ok {
48 v = &commandCache{command: command}
49 c.commands[command] = v
50 }
51 return v
52 }
53
54 func (c *cache) getUser(user string) *userCache {
55 v, ok := c.users[user]
56 if !ok {
57 v = &userCache{user: user}
58 c.users[user] = v
59 }
60 return v
61 }
62
63 func (c *cache) getBackend(hg, host, port string) *backendCache {
64 id := backendID(hg, host, port)
65 v, ok := c.backends[id]
66 if !ok {
67 v = &backendCache{hg: hg, host: host, port: port}
68 c.backends[id] = v
69 }
70 return v
71 }
72
73 func (c *cache) getHostgroup(hg string) *hostgroupCache {
74 v, ok := c.hostgroups[hg]
75 if !ok {
76 v = &hostgroupCache{hg: hg}
77 c.hostgroups[hg] = v
78 }
79 return v
80 }