-
Notifications
You must be signed in to change notification settings - Fork 1
/
default_distributed_refresher.go
60 lines (50 loc) · 1.63 KB
/
default_distributed_refresher.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
package mgcache
import (
"context"
"github.com/go-redis/redis/v8"
"go.uber.org/zap"
"time"
)
type (
// PubSubClientInterface PubSubClient Interface
PubSubClientInterface interface {
Publish(ctx context.Context, channel string, message interface{}) *redis.IntCmd
Subscribe(ctx context.Context, channels ...string) *redis.PubSub
}
defaultDistributedRefresher struct {
pubSubClient PubSubClientInterface
storageClient IStorage
topic string
}
)
// NewDefaultDistributedRefresher initializes the distributive cache refresher
func NewDefaultDistributedRefresher(topic string,
redisClient PubSubClientInterface,
storageClient IStorage) IDistributedRefresher {
refreshEventChan := redisClient.Subscribe(context.Background(), topic).Channel()
distributedRefresher := &defaultDistributedRefresher{
pubSubClient: redisClient,
storageClient: storageClient,
topic: topic,
}
go distributedRefresher.listenChannel(refreshEventChan)
return distributedRefresher
}
func (d defaultDistributedRefresher) listenChannel(refreshEventChan <-chan *redis.Message) {
for msg := range refreshEventChan {
go func(msg *redis.Message) {
if _, err := d.storageClient.Refresh(msg.Payload); err != nil {
zap.L().DPanic("failed to distributively refresh cache", zap.String("topic", d.topic), zap.Error(err))
}
}(msg)
}
}
func (d defaultDistributedRefresher) Notify(key string) (err error) {
if err = d.storageClient.Invalidate(key); err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
_, err = d.pubSubClient.Publish(ctx, d.topic, key).Result()
return
}