1
1
package configure
2
2
3
3
import (
4
- "encoding/json"
5
4
"fmt"
6
- "io/ioutil"
7
5
"log"
8
6
"math/rand"
9
- "sync"
10
- "time"
11
7
12
8
"github.com/go-redis/redis/v7"
9
+ "github.com/patrickmn/go-cache"
13
10
)
14
11
15
- var RoomKeys = LoadRoomKey ( * GetKeyFile ())
12
+ var RoomKeys * RoomKeysType
16
13
17
14
var roomUpdated = false
18
15
19
- var saveInFile = true
20
- var redisCli * redis.Client
16
+ var saveInLocal = true
17
+
18
+ type RoomKeysType struct {
19
+ redisCli * redis.Client
20
+ localCache * cache.Cache
21
+ }
21
22
22
23
func Init () {
23
- saveInFile = GetRedisAddr () == nil
24
-
25
- rand .Seed (time .Now ().UnixNano ())
26
- if saveInFile {
27
- go func () {
28
- for {
29
- time .Sleep (15 * time .Second )
30
- if roomUpdated {
31
- RoomKeys .Save (* roomKeySaveFile )
32
- roomUpdated = false
33
- }
34
- }
35
- }()
24
+ saveInLocal = GetRedisAddr () == nil
25
+
26
+ RoomKeys = & RoomKeysType {
27
+ localCache : cache .New (cache .NoExpiration , 0 ),
28
+ }
36
29
30
+ if saveInLocal {
37
31
return
38
32
}
39
33
40
- redisCli = redis .NewClient (& redis.Options {
34
+ RoomKeys . redisCli = redis .NewClient (& redis.Options {
41
35
Addr : * GetRedisAddr (),
42
36
Password : * GetRedisPwd (),
43
37
DB : 0 ,
44
38
})
45
39
46
- _ , err := redisCli .Ping ().Result ()
40
+ _ , err := RoomKeys . redisCli .Ping ().Result ()
47
41
if err != nil {
48
42
panic (err )
49
43
}
50
44
51
45
log .Printf ("Redis connected" )
52
46
}
53
47
54
- type RoomKeysType struct {
55
- mapChanKey sync.Map
56
- mapKeyChan sync.Map
57
- }
58
-
59
- func LoadRoomKey (f string ) * RoomKeysType {
60
- result := & RoomKeysType {
61
- mapChanKey : sync.Map {},
62
- mapKeyChan : sync.Map {},
63
- }
64
- raw := map [string ]string {}
65
- content , err := ioutil .ReadFile (f )
66
- if err != nil {
67
- log .Printf ("Failed to read file %s for room keys" , f )
68
- return result
69
- }
70
- if json .Unmarshal (content , & raw ) != nil {
71
- log .Printf ("Failed to unmarshal file %s for room keys" , f )
72
- return result
73
- }
74
- for room , key := range raw {
75
- result .mapChanKey .Store (room , key )
76
- result .mapKeyChan .Store (key , room )
77
- }
78
- return result
79
- }
80
-
81
- func (r * RoomKeysType ) Save (f string ) {
82
- raw := map [string ]string {}
83
- r .mapChanKey .Range (func (channel , key interface {}) bool {
84
- raw [channel .(string )] = key .(string )
85
- return true
86
- })
87
- content , err := json .Marshal (raw )
88
- if err != nil {
89
- log .Println ("Failed to marshal room keys" )
90
- return
91
- }
92
- if ioutil .WriteFile (f , content , 0644 ) != nil {
93
- log .Println ("Failed to save room keys" )
94
- return
95
- }
96
- }
97
-
98
48
// set/reset a random key for channel
99
49
func (r * RoomKeysType ) SetKey (channel string ) (key string , err error ) {
100
- if ! saveInFile {
50
+ if ! saveInLocal {
101
51
for {
102
52
key = randStringRunes (48 )
103
- if _ , err = redisCli .Get (key ).Result (); err == redis .Nil {
104
- err = redisCli .Set (channel , key , 0 ).Err ()
53
+ if _ , err = r . redisCli .Get (key ).Result (); err == redis .Nil {
54
+ err = r . redisCli .Set (channel , key , 0 ).Err ()
105
55
if err != nil {
106
56
return
107
57
}
108
58
109
- err = redisCli .Set (key , channel , 0 ).Err ()
59
+ err = r . redisCli .Set (key , channel , 0 ).Err ()
110
60
return
111
61
} else if err != nil {
112
62
return
@@ -116,9 +66,9 @@ func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
116
66
117
67
for {
118
68
key = randStringRunes (48 )
119
- if _ , found := r .mapKeyChan . Load (key ); ! found {
120
- r .mapChanKey . Store (channel , key )
121
- r .mapKeyChan . Store (key , channel )
69
+ if _ , found := r .localCache . Get (key ); ! found {
70
+ r .localCache . SetDefault (channel , key )
71
+ r .localCache . SetDefault (key , channel )
122
72
break
123
73
}
124
74
}
@@ -127,8 +77,8 @@ func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
127
77
}
128
78
129
79
func (r * RoomKeysType ) GetKey (channel string ) (newKey string , err error ) {
130
- if ! saveInFile {
131
- if newKey , err = redisCli .Get (channel ).Result (); err == redis .Nil {
80
+ if ! saveInLocal {
81
+ if newKey , err = r . redisCli .Get (channel ).Result (); err == redis .Nil {
132
82
newKey , err = r .SetKey (channel )
133
83
log .Printf ("[KEY] new channel [%s]: %s" , channel , newKey )
134
84
return
@@ -139,7 +89,7 @@ func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) {
139
89
140
90
var key interface {}
141
91
var found bool
142
- if key , found = r .mapChanKey . Load (channel ); found {
92
+ if key , found = r .localCache . Get (channel ); found {
143
93
return key .(string ), nil
144
94
}
145
95
newKey , err = r .SetKey (channel )
@@ -148,11 +98,11 @@ func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) {
148
98
}
149
99
150
100
func (r * RoomKeysType ) GetChannel (key string ) (channel string , err error ) {
151
- if ! saveInFile {
152
- return redisCli .Get (key ).Result ()
101
+ if ! saveInLocal {
102
+ return r . redisCli .Get (key ).Result ()
153
103
}
154
104
155
- chann , found := r .mapKeyChan . Load (key )
105
+ chann , found := r .localCache . Get (key )
156
106
if found {
157
107
return chann .(string ), nil
158
108
} else {
@@ -161,28 +111,28 @@ func (r *RoomKeysType) GetChannel(key string) (channel string, err error) {
161
111
}
162
112
163
113
func (r * RoomKeysType ) DeleteChannel (channel string ) bool {
164
- if ! saveInFile {
165
- return redisCli .Del (channel ).Err () != nil
114
+ if ! saveInLocal {
115
+ return r . redisCli .Del (channel ).Err () != nil
166
116
}
167
117
168
- key , ok := r .mapChanKey . Load (channel )
118
+ key , ok := r .localCache . Get (channel )
169
119
if ok {
170
- r .mapChanKey .Delete (channel )
171
- r .mapKeyChan .Delete (key )
120
+ r .localCache .Delete (channel )
121
+ r .localCache .Delete (key .( string ) )
172
122
return true
173
123
}
174
124
return false
175
125
}
176
126
177
127
func (r * RoomKeysType ) DeleteKey (key string ) bool {
178
- if ! saveInFile {
179
- return redisCli .Del (key ).Err () != nil
128
+ if ! saveInLocal {
129
+ return r . redisCli .Del (key ).Err () != nil
180
130
}
181
131
182
- channel , ok := r .mapKeyChan . Load (key )
132
+ channel , ok := r .localCache . Get (key )
183
133
if ok {
184
- r .mapChanKey .Delete (channel )
185
- r .mapKeyChan .Delete (key )
134
+ r .localCache .Delete (channel .( string ) )
135
+ r .localCache .Delete (key )
186
136
return true
187
137
}
188
138
return false
0 commit comments