-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_impl.go
More file actions
471 lines (382 loc) · 11.2 KB
/
cache_impl.go
File metadata and controls
471 lines (382 loc) · 11.2 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package cachex
import (
"context"
"fmt"
"sync"
"time"
"github.com/redis/go-redis/v9"
"github.com/seasbee/go-logx"
)
// redisCache implements the Cache interface using go-redis client
type redisCache struct {
client *redis.Client
codec Codec
keyBuilder KeyBuilder
keyHasher KeyHasher
ctx context.Context
mu sync.RWMutex
closed bool
closedFlag *bool
}
// NewRedisCache creates a new cache instance using go-redis client
func NewRedisCache(client *redis.Client, codec Codec, keyBuilder KeyBuilder, keyHasher KeyHasher) Cache {
if codec == nil {
codec = &JSONCodec{}
}
if keyBuilder == nil {
keyBuilder = &DefaultKeyBuilder{}
}
if keyHasher == nil {
keyHasher = &DefaultKeyHasher{}
}
// Create a shared closed flag
closedFlag := false
return &redisCache{
client: client,
codec: codec,
keyBuilder: keyBuilder,
keyHasher: keyHasher,
ctx: context.Background(),
closed: false,
closedFlag: &closedFlag,
}
}
// isClosed checks if the cache is closed
func (c *redisCache) isClosed() bool {
if c.closedFlag != nil {
return *c.closedFlag
}
return c.closed
}
// WithContext returns a new cache instance with the given context
func (c *redisCache) WithContext(ctx context.Context) Cache {
if ctx == nil {
ctx = context.Background()
}
// Create new cache without copying the mutex
newCache := &redisCache{
client: c.client,
codec: c.codec,
keyBuilder: c.keyBuilder,
keyHasher: c.keyHasher,
ctx: ctx,
closed: c.closed,
closedFlag: c.closedFlag,
}
return newCache
}
// Get retrieves a value from the cache (non-blocking)
func (c *redisCache) Get(ctx context.Context, key string) <-chan AsyncCacheResult[any] {
result := make(chan AsyncCacheResult[any], 1)
go func() {
defer close(result)
c.mu.RLock()
if c.isClosed() {
c.mu.RUnlock()
result <- AsyncCacheResult[any]{Error: ErrStoreClosed}
return
}
c.mu.RUnlock()
// Execute get operation asynchronously
asyncResult := c.executeGetAsync(ctx, key)
result <- asyncResult
}()
return result
}
// executeGetAsync performs the actual get operation (async)
func (c *redisCache) executeGetAsync(ctx context.Context, key string) AsyncCacheResult[any] {
// Get from Redis
val, err := c.client.Get(ctx, key).Result()
if err != nil {
if err == redis.Nil {
return AsyncCacheResult[any]{Found: false} // Key not found
}
return AsyncCacheResult[any]{Error: NewCacheError("get", key, "redis error", err)}
}
// Decode value - we'll return the raw decoded value as any
var value any
if err := c.codec.Decode([]byte(val), &value); err != nil {
return AsyncCacheResult[any]{Error: NewCacheError("get", key, "decode error", err)}
}
return AsyncCacheResult[any]{Value: value, Found: true}
}
// Set stores a value in the cache (non-blocking)
func (c *redisCache) Set(ctx context.Context, key string, val any, ttl time.Duration) <-chan AsyncCacheResult[any] {
result := make(chan AsyncCacheResult[any], 1)
go func() {
defer close(result)
c.mu.RLock()
if c.isClosed() {
c.mu.RUnlock()
result <- AsyncCacheResult[any]{Error: ErrStoreClosed}
return
}
c.mu.RUnlock()
// Execute set operation asynchronously
asyncResult := c.executeSetAsync(ctx, key, val, ttl)
result <- asyncResult
}()
return result
}
// executeSetAsync performs the actual set operation (async)
func (c *redisCache) executeSetAsync(ctx context.Context, key string, val any, ttl time.Duration) AsyncCacheResult[any] {
// Basic boundary condition validations
if key == "" {
return AsyncCacheResult[any]{Error: NewCacheError("set", key, "key validation error", fmt.Errorf("key cannot be empty"))}
}
// Encode value
data, err := c.codec.Encode(val)
if err != nil {
return AsyncCacheResult[any]{Error: NewCacheError("set", key, "encode error", err)}
}
// Check if encoded data is nil
if data == nil {
return AsyncCacheResult[any]{Error: NewCacheError("set", key, "value validation error", fmt.Errorf("value cannot be nil"))}
}
// Store in Redis
err = c.client.Set(ctx, key, data, ttl).Err()
if err != nil {
return AsyncCacheResult[any]{Error: NewCacheError("set", key, "redis error", err)}
}
return AsyncCacheResult[any]{Value: val}
}
// MGet retrieves multiple values from the cache (non-blocking)
func (c *redisCache) MGet(ctx context.Context, keys ...string) <-chan AsyncCacheResult[any] {
result := make(chan AsyncCacheResult[any], 1)
go func() {
defer close(result)
c.mu.RLock()
if c.isClosed() {
c.mu.RUnlock()
result <- AsyncCacheResult[any]{Error: ErrStoreClosed}
return
}
c.mu.RUnlock()
if len(keys) == 0 {
result <- AsyncCacheResult[any]{Values: make(map[string]any)}
return
}
// Get from Redis
vals, err := c.client.MGet(ctx, keys...).Result()
if err != nil {
result <- AsyncCacheResult[any]{Error: NewCacheError("mget", "multiple", "redis error", err)}
return
}
// Decode values
values := make(map[string]any)
for i, val := range vals {
if val != nil {
if strVal, ok := val.(string); ok && strVal != "" {
var value any
if err := c.codec.Decode([]byte(strVal), &value); err != nil {
logx.Warn("Failed to decode value in MGet", logx.String("key", keys[i]), logx.ErrorField(err))
continue
}
values[keys[i]] = value
}
}
}
result <- AsyncCacheResult[any]{Values: values}
}()
return result
}
// MSet stores multiple values in the cache (non-blocking)
func (c *redisCache) MSet(ctx context.Context, items map[string]any, ttl time.Duration) <-chan AsyncCacheResult[any] {
result := make(chan AsyncCacheResult[any], 1)
go func() {
defer close(result)
c.mu.RLock()
if c.isClosed() {
c.mu.RUnlock()
result <- AsyncCacheResult[any]{Error: ErrStoreClosed}
return
}
c.mu.RUnlock()
if len(items) == 0 {
result <- AsyncCacheResult[any]{}
return
}
// Encode values
dataMap := make(map[string]interface{})
for key, value := range items {
data, err := c.codec.Encode(value)
if err != nil {
result <- AsyncCacheResult[any]{Error: NewCacheError("mset", key, "encode error", err)}
return
}
// Check if encoded data is nil
if data == nil {
result <- AsyncCacheResult[any]{Error: NewCacheError("mset", key, "value validation error", fmt.Errorf("value cannot be nil"))}
return
}
dataMap[key] = data
}
// Store in Redis
err := c.client.MSet(ctx, dataMap).Err()
if err != nil {
result <- AsyncCacheResult[any]{Error: NewCacheError("mset", "multiple", "redis error", err)}
return
}
// Set TTL for all keys if specified
if ttl > 0 {
for key := range dataMap {
c.client.Expire(ctx, key, ttl)
}
}
result <- AsyncCacheResult[any]{}
}()
return result
}
// Del removes keys from the cache (non-blocking)
func (c *redisCache) Del(ctx context.Context, keys ...string) <-chan AsyncCacheResult[any] {
result := make(chan AsyncCacheResult[any], 1)
go func() {
defer close(result)
c.mu.RLock()
if c.isClosed() {
c.mu.RUnlock()
result <- AsyncCacheResult[any]{Error: ErrStoreClosed}
return
}
c.mu.RUnlock()
if len(keys) == 0 {
result <- AsyncCacheResult[any]{}
return
}
// Delete from Redis
count, err := c.client.Del(ctx, keys...).Result()
if err != nil {
result <- AsyncCacheResult[any]{Error: NewCacheError("del", "multiple", "redis error", err)}
return
}
result <- AsyncCacheResult[any]{Count: count}
}()
return result
}
// Exists checks if a key exists in the cache (non-blocking)
func (c *redisCache) Exists(ctx context.Context, key string) <-chan AsyncCacheResult[any] {
result := make(chan AsyncCacheResult[any], 1)
go func() {
defer close(result)
c.mu.RLock()
if c.isClosed() {
c.mu.RUnlock()
result <- AsyncCacheResult[any]{Error: ErrStoreClosed}
return
}
c.mu.RUnlock()
// Check in Redis
count, err := c.client.Exists(ctx, key).Result()
if err != nil {
result <- AsyncCacheResult[any]{Error: NewCacheError("exists", key, "redis error", err)}
return
}
result <- AsyncCacheResult[any]{Found: count > 0}
}()
return result
}
// TTL gets the time to live of a key (non-blocking)
func (c *redisCache) TTL(ctx context.Context, key string) <-chan AsyncCacheResult[any] {
result := make(chan AsyncCacheResult[any], 1)
go func() {
defer close(result)
c.mu.RLock()
if c.isClosed() {
c.mu.RUnlock()
result <- AsyncCacheResult[any]{Error: ErrStoreClosed}
return
}
c.mu.RUnlock()
// Get TTL from Redis
ttl, err := c.client.TTL(ctx, key).Result()
if err != nil {
result <- AsyncCacheResult[any]{Error: NewCacheError("ttl", key, "redis error", err)}
return
}
result <- AsyncCacheResult[any]{TTL: ttl}
}()
return result
}
// IncrBy increments a key by the given delta (non-blocking)
func (c *redisCache) IncrBy(ctx context.Context, key string, delta int64, ttlIfCreate time.Duration) <-chan AsyncCacheResult[any] {
result := make(chan AsyncCacheResult[any], 1)
go func() {
defer close(result)
c.mu.RLock()
if c.isClosed() {
c.mu.RUnlock()
result <- AsyncCacheResult[any]{Error: ErrStoreClosed}
return
}
c.mu.RUnlock()
// Increment in Redis
newVal, err := c.client.IncrBy(ctx, key, delta).Result()
if err != nil {
result <- AsyncCacheResult[any]{Error: NewCacheError("incrby", key, "redis error", err)}
return
}
// Set TTL if this is a new key
if ttlIfCreate > 0 {
exists, err := c.client.Exists(ctx, key).Result()
if err == nil && exists == 1 {
// Check if this was a new key by trying to set TTL
c.client.Expire(ctx, key, ttlIfCreate)
}
}
result <- AsyncCacheResult[any]{Int: newVal}
}()
return result
}
// Close closes the cache and releases resources
func (c *redisCache) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.isClosed() {
return nil
}
c.closed = true
if c.closedFlag != nil {
*c.closedFlag = true
}
// Close Redis client
if err := c.client.Close(); err != nil {
logx.Error("Failed to close Redis client", logx.ErrorField(err))
return err
}
return nil
}
// Default implementations
type DefaultKeyBuilder struct{}
func (b *DefaultKeyBuilder) Build(entity, id string) string {
return fmt.Sprintf("%s:%s", entity, id)
}
func (b *DefaultKeyBuilder) BuildList(entity string, filters map[string]any) string {
return fmt.Sprintf("list:%s", entity)
}
func (b *DefaultKeyBuilder) BuildComposite(entityA, idA, entityB, idB string) string {
return fmt.Sprintf("%s:%s:%s:%s", entityA, idA, entityB, idB)
}
func (b *DefaultKeyBuilder) BuildSession(sid string) string {
return fmt.Sprintf("session:%s", sid)
}
type DefaultKeyHasher struct{}
func (h *DefaultKeyHasher) Hash(data string) string {
// Simple hash implementation
return fmt.Sprintf("hash_%s", data)
}
// KeyBuilder helper methods for redisCache
func (c *redisCache) BuildKey(entity, id string) string {
return c.keyBuilder.Build(entity, id)
}
func (c *redisCache) BuildListKey(entity string, filters map[string]any) string {
return c.keyBuilder.BuildList(entity, filters)
}
func (c *redisCache) BuildCompositeKey(entityA, idA, entityB, idB string) string {
return c.keyBuilder.BuildComposite(entityA, idA, entityB, idB)
}
func (c *redisCache) BuildSessionKey(sid string) string {
return c.keyBuilder.BuildSession(sid)
}
func (c *redisCache) GetKeyBuilder() KeyBuilder {
return c.keyBuilder
}