-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
123 lines (101 loc) · 2.68 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package revolt
import "fmt"
// Client cache struct.
type Cache struct {
Users []*User `json:"users"`
Servers []*Server `json:"servers"`
Channels []*Channel `json:"channels"`
Members []*Member `json:"members"`
}
// Get a channel from cache by Id.
// Will return an empty channel struct if not found.
func (c *Cache) GetChannel(id string) *Channel {
for _, i := range c.Channels {
if i.Id == id {
return i
}
}
return &Channel{}
}
// Get a server from cache by Id.
// Will return an empty server struct if not found.
func (c *Cache) GetServer(id string) *Server {
for _, i := range c.Servers {
if i.Id == id {
return i
}
}
return &Server{}
}
// Get an user from cache by Id.
// Will return an empty user struct if not found.
func (c *Cache) GetUser(id string) *User {
for _, i := range c.Users {
if i.Id == id {
return i
}
}
return &User{}
}
// Get a member from cache by Id.
// Will return an empty member struct if not found.
func (c *Cache) GetMember(id string) *Member {
for _, i := range c.Members {
if i.Informations.UserId == id {
return i
}
}
return &Member{}
}
// Remove a channel from cache by Id.
// Will not delete the channel, just deletes the channel from cache.
// Will change the entire channel cache order!
func (c *Cache) RemoveChannel(id string) error {
for i, v := range c.Channels {
if v.Id == id {
c.Channels[i] = c.Channels[len(c.Channels)-1]
c.Channels = c.Channels[:len(c.Channels)-1]
return nil
}
}
return fmt.Errorf("channel not found")
}
// Remove a server from cache by Id.
// Will not delete the server, just deletes the server from cache.
// Will change the entire server cache order!
func (c *Cache) RemoveServer(id string) error {
for i, v := range c.Servers {
if v.Id == id {
c.Servers[i] = c.Servers[len(c.Servers)-1]
c.Servers = c.Servers[:len(c.Servers)-1]
return nil
}
}
return fmt.Errorf("server not found")
}
// Remove an user from cache by Id.
// Will not delete the user, just deletes the user from cache.
// Will change the entire user cache order!
func (c *Cache) RemoveUser(id string) error {
for i, v := range c.Users {
if v.Id == id {
c.Users[i] = c.Users[len(c.Users)-1]
c.Users = c.Users[:len(c.Users)-1]
return nil
}
}
return fmt.Errorf("user not found")
}
// Remove a member from cache by Id.
// Will not delete the member, just deletes the member from cache.
// Will change the entire member cache order!
func (c *Cache) RemoveMember(id string) error {
for i, v := range c.Members {
if v.Informations.UserId == id {
c.Members[i] = c.Members[len(c.Members)-1]
c.Members = c.Members[:len(c.Members)-1]
return nil
}
}
return fmt.Errorf("member not found")
}