-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
135 lines (110 loc) · 2.79 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
124
125
126
127
128
129
130
131
132
133
134
135
package cache_lib
import (
"context"
"encoding/json"
"errors"
"log"
"time"
"github.com/go-redis/redis/v8"
)
type CacheOptions struct {
SubscriptionTimeout time.Duration
UnsubscribeAndClose bool
}
type Cache[Data any] interface {
RememberBlocking(ctx context.Context, missFn MissFunc[Data], hitFn HitFunc[Data], key string, ttl time.Duration) (*Data, error)
}
type cache[Data any] struct {
client *redis.Client
options *CacheOptions
}
type MissFunc[Data any] func(ctx context.Context) (*Data, error)
type HitFunc[Data any] func(ctx context.Context, data *Data)
func NewCache[Data any](client *redis.Client, options *CacheOptions) Cache[Data] {
builtOptions := &CacheOptions{}
if options != nil {
builtOptions = options
}
return &cache[Data]{
client: client,
options: builtOptions,
}
}
func (c *cache[Data]) getCachedData(ctx context.Context, key string) *Data {
cachedData, _ := c.client.Get(ctx, key).Result()
if cachedData == "" {
return nil
}
var marshaledData Data
err := json.Unmarshal([]byte(cachedData), &marshaledData)
if err != nil {
return nil
}
return &marshaledData
}
func (c *cache[Data]) RememberBlocking(ctx context.Context, missFn MissFunc[Data], hitFn HitFunc[Data], key string, ttl time.Duration) (*Data, error) {
cachedData := c.getCachedData(ctx, key)
if cachedData != nil {
hitFn(ctx, cachedData)
return cachedData, nil
}
success, err := c.client.SetNX(ctx, key, "", ttl).Result()
if err != nil {
log.Println(err)
return nil, err
}
if !success {
return c.rememberWait(ctx, key)
}
data, err := missFn(ctx)
if err != nil {
c.client.Publish(ctx, key, "cache miss")
return nil, err
}
bytedata, _ := json.Marshal(*data)
_, err = c.client.Set(ctx, key, string(bytedata), ttl).Result()
if err != nil {
log.Println(err)
return nil, err
}
_, err = c.client.Publish(ctx, key, string(bytedata)).Result()
if err != nil {
return nil, err
}
return data, nil
}
func (c *cache[Data]) rememberWait(ctx context.Context, key string) (*Data, error) {
subscription := NewCacheSubscription(c.client, key)
subscription.Subscribe(ctx)
defer func() {
if c.options.UnsubscribeAndClose {
subscription.UnsubscribeAndClose(ctx)
return
}
err := subscription.Unsubscribe(ctx)
if err != nil {
log.Println(err)
}
}()
channel, err := subscription.GetChannel(ctx)
if err != nil {
return nil, err
}
if c.options.SubscriptionTimeout != 0*time.Second {
go func() {
time.Sleep(c.options.SubscriptionTimeout)
subscription.UnsubscribeAndClose(ctx)
}()
}
for msg := range channel {
if msg.Payload != "" {
var u Data
// Unmarshal the data into the user
if err := json.Unmarshal([]byte(msg.Payload), &u); err != nil {
return nil, err
}
return &u, nil
}
}
return nil, errors.New("error reading from pub/sub")
}