-
Notifications
You must be signed in to change notification settings - Fork 25
/
dist.go
81 lines (71 loc) · 1.76 KB
/
dist.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
package dist
import (
"log"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/orca-zhang/ecache"
)
const topic = "orca-zhang/ecache"
// `RedisCli`` interface used by `dist` component
type RedisCli interface {
// if the redis client is ready
OK() bool
// pub a payload to channel
Pub(channel, payload string) error
// sub a payload from channel, callback uill tidy the local cache
Sub(channel string, callback func(payload string)) error
}
var redisCli RedisCli
var m sync.Map
func delAll(pool, key string) {
if caches, _ := m.Load(pool); caches != nil {
for _, c := range *(caches.(*[]*ecache.Cache)) {
c.Del(key)
}
}
}
// Init `dist` component with redis client
func Init(r RedisCli) {
if redisCli != r {
redisCli = r
go func() {
defer func() {
if err := recover(); err != nil {
log.Println(err)
debug.PrintStack()
}
}()
for {
for r == nil || !r.OK() {
time.Sleep(10 * time.Millisecond)
}
_ = r.Sub(topic, func(payload string) {
vs := strings.Split(payload, ":")
if len(vs) >= 2 {
delAll(vs[0], vs[1])
}
})
}
}()
}
}
// Bind - to enable distributed consistency
// `pool` is not necessary, it can be used to classify instances that store same items, but it will be more efficient if it is not empty
// `caches` is cache instances to be binded
func Bind(pool string, caches ...*ecache.Cache) error {
c, _ := m.LoadOrStore(pool, &[]*ecache.Cache{})
*(c.(*[]*ecache.Cache)) = append(*(c.(*[]*ecache.Cache)), caches...)
return nil
}
// OnDel - delete `key` in `pool` at distributed scale
func OnDel(pool, key string) error {
// pub to remote nodes
r := redisCli
if r != nil && r.Pub(topic, strings.Join([]string{pool, key}, ":")) == nil {
return nil
}
delAll(pool, key)
return nil
}