-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_schema.go
78 lines (60 loc) · 1.15 KB
/
redis_schema.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
package qmq
import (
"google.golang.org/protobuf/proto"
)
type RedisSchema struct {
db *RedisConnection
kv map[string]proto.Message
ch chan string
}
func NewRedisSchema(conn *RedisConnection, kv map[string]proto.Message) Schema {
s := &RedisSchema{
db: conn,
kv: kv,
ch: make(chan string),
}
s.Initialize()
return s
}
func (s *RedisSchema) Get(key string) proto.Message {
v := s.kv[key]
if v != nil {
s.db.GetValue(key, v)
}
return v
}
func (s *RedisSchema) GetFull(key string) (proto.Message, *SchemaData) {
v := s.kv[key]
if v == nil {
return nil, nil
}
d, err := s.db.Get(key)
if err != nil {
return nil, nil
}
err = d.Data.UnmarshalTo(v)
if err != nil {
return nil, nil
}
return v, d
}
func (s *RedisSchema) Set(key string, value proto.Message) {
s.kv[key] = value
s.db.SetValue(key, value)
s.ch <- key
}
func (s *RedisSchema) SetNoNotify(key string, value proto.Message) {
s.kv[key] = value
s.db.SetValue(key, value)
}
func (s *RedisSchema) Ch() chan string {
return s.ch
}
func (s *RedisSchema) Initialize() {
for key := range s.kv {
s.Get(key)
}
for key := range s.kv {
s.SetNoNotify(key, s.kv[key])
}
}