-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcache.go
182 lines (161 loc) · 3.68 KB
/
mcache.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package mcache
import (
"context"
"errors"
"math"
"sync"
"time"
)
var p sync.Pool
type cache struct {
clock Clock
size int
shardCount int
shardCap int
defaultVal interface{}
expiration time.Duration
loaderFunc LoaderFunc
mLoaderFunc MLoaderFunc
redisCli RedisCli
serializeFunc serializeFunc
deserializeFunc deserializeFunc
}
func (c cache) getOpt() options {
o, ok := p.Get().(options)
if !ok {
panic("unreachable")
}
return o
}
func (c *cache) putOpt(o options) {
o.RedisCli = c.redisCli
o.TTL = c.expiration
o.LoaderFunc = c.loaderFunc
o.MLoaderFunc = c.mLoaderFunc
o.DefaultVal = c.defaultVal
o.serializeFunc = c.serializeFunc
o.deserializeFunc = c.deserializeFunc
p.Put(o)
}
func (c cache) getOption(opts ...Option) options {
o := c.getOpt()
for _, opt := range opts {
opt(&o)
}
if o.LoaderFunc == nil {
if c.redisCli.Client != nil {
o.RealLoaderFunc = func(ctx context.Context, k string) (interface{}, error) {
v, err := c.redisCli.get(ctx, k, o)
if err == nil {
return v, nil
}
return nil, err
}
}
} else {
o.RealLoaderFunc = func(ctx context.Context, k string) (interface{}, error) {
if v, err := c.redisCli.get(ctx, k, o); err == nil {
return v, nil
}
v, err := o.LoaderFunc(ctx, k)
if err == nil {
if err := c.redisCli.set(ctx, k, v, o); err != nil && !errors.Is(err, RedisNotFoundError) {
return nil, err
}
return v, nil
} else if o.DefaultVal != nil {
return o.DefaultVal, DefaultValueSetError
}
return nil, err
}
}
if o.MLoaderFunc == nil {
if c.redisCli.Client != nil {
o.RealMLoaderFunc = func(ctx context.Context, keys []string) (map[string]interface{}, error) {
result, err := c.redisCli.mget(ctx, keys, o)
return result, err
}
}
} else {
o.RealMLoaderFunc = func(ctx context.Context, keys []string) (map[string]interface{}, error) {
result, _ := c.redisCli.mget(ctx, keys, o)
keysG := make([]string, 0, len(keys))
for _, key := range keys {
if _, ok := result[key]; !ok {
keysG = append(keysG, key)
}
}
val, err := o.MLoaderFunc(ctx, keysG)
if err == nil {
if err := c.redisCli.msetmap(ctx, val, o); err != nil && !errors.Is(err, RedisNotFoundError) {
return nil, err
}
for k, v := range val {
result[k] = v
}
}
return result, nil
}
}
return o
}
type builder[T any, P CachePolicy[T]] struct {
cache
}
func New[T any, P CachePolicy[T]](size int, opts ...Option) Cache {
o := options{}
for _, opt := range opts {
opt(&o)
}
b := builder[T, P]{
cache{
size: size,
clock: NewRealClock(),
},
}
if b.size == 0 {
b.size = defaultCacheSize
}
b.shardCount = int(math.Ceil(float64(b.size) / float64(defaultShardCap)))
if b.shardCount == 0 {
b.shardCount = defaultShardCount
}
b.shardCap = b.size / b.shardCount
if b.shardCap == 0 {
b.shardCap = defaultShardCap
}
b.formatByOpts(o)
p = sync.Pool{
New: func() interface{} {
return options{
RedisCli: b.redisCli,
TTL: b.expiration,
LoaderFunc: b.loaderFunc,
MLoaderFunc: b.mLoaderFunc,
DefaultVal: b.defaultVal,
serializeFunc: b.serializeFunc,
deserializeFunc: b.deserializeFunc,
}
},
}
return newCacheHandler(b)
}
func (b *builder[T, P]) formatByOpts(o options) {
if o.LoaderFunc != nil {
b.loaderFunc = o.LoaderFunc
}
if o.MLoaderFunc != nil {
b.mLoaderFunc = o.MLoaderFunc
}
if o.TTL > 0 {
b.expiration = o.TTL
}
b.redisCli = o.RedisCli
if o.serializeFunc != nil && o.deserializeFunc != nil {
b.serializeFunc = o.serializeFunc
b.deserializeFunc = o.deserializeFunc
}
if o.DefaultVal != nil {
b.defaultVal = o.DefaultVal
}
}