-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
83 lines (69 loc) · 2.09 KB
/
redis.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
package gcache
import (
"context"
"errors"
"io"
"log/slog"
"time"
rediscache "github.com/go-redis/cache/v9"
)
// RedisOption is a configuration option.
type RedisOption func(*redisStore)
// WithRedisLogger sets the logger.
func WithRedisLogger(l *slog.Logger) RedisOption {
return func(r *redisStore) { r.logger = l }
}
// WithRedisTTL sets the TTL.
func WithRedisTTL(ttl time.Duration) RedisOption {
return func(r *redisStore) { r.ttl = ttl }
}
// WithRedisSkipLocalCache sets the skipLocalCache.
func WithRedisSkipLocalCache(skipLocalCache bool) RedisOption {
return func(r *redisStore) { r.skipLocalCache = skipLocalCache }
}
type redisStore struct {
backend *rediscache.Cache
logger *slog.Logger
ttl time.Duration
skipLocalCache bool
}
// NewRedis returns a new redisStore cache store.
func NewRedis(backend *rediscache.Cache, opts ...RedisOption) Store {
store := &redisStore{
backend: backend,
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
}
for _, opt := range opts {
opt(store)
}
return store
}
// Get returns the value for the given key.
func (r *redisStore) Get(ctx context.Context, key string) (e Entry, ok bool) {
switch err := r.backend.Get(ctx, key, &e); {
case errors.Is(err, rediscache.ErrCacheMiss):
return Entry{}, false
case err != nil:
r.logger.WarnContext(ctx, "gcache: failed to get from redisStore cache", slog.Any(ErrKey, err))
}
return e, true
}
// Set sets the value for the given key.
func (r *redisStore) Set(ctx context.Context, key string, e Entry) {
item := &rediscache.Item{
Ctx: ctx,
Key: key,
Value: e,
TTL: r.ttl,
SkipLocalCache: r.skipLocalCache,
}
if err := r.backend.Set(item); err != nil {
r.logger.WarnContext(ctx, "gcache: failed to set to redisStore cache", slog.Any(ErrKey, err))
}
}
// Remove removes the value for the given key.
func (r *redisStore) Remove(ctx context.Context, key string) {
if err := r.backend.Delete(ctx, key); err != nil {
r.logger.WarnContext(ctx, "gcache: failed to remove from redisStore cache", slog.Any(ErrKey, err))
}
}