-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
218 lines (172 loc) · 5.39 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
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
package cacher
import (
"context"
"encoding/json"
"fmt"
"github.com/SolomonAIEngineering/backend-core-library/instrumentation"
"github.com/gomodule/redigo/redis"
"go.uber.org/zap"
)
type Client struct {
logger *zap.Logger
pool *redis.Pool
serviceName string
instrumentationClient *instrumentation.Client
cacheTTLInSeconds int
}
func New(opts ...Option) (*Client, error) {
c := &Client{}
for _, opt := range opts {
opt(c)
}
if err := c.Validate(); err != nil {
return nil, err
}
return c, nil
}
// WriteToCache writes a value to the cache
func (s *Client) WriteToCache(ctx context.Context, key string, value []byte) error {
if s.instrumentationClient != nil {
txn := s.instrumentationClient.GetTraceFromContext(ctx)
span := s.instrumentationClient.StartRedisDatastoreSegment(txn, "redis-write-to-cache")
defer span.End()
}
// validate the key
if key == "" {
return fmt.Errorf("empty key")
}
prefixedCacheKey := fmt.Sprintf("%s:%s", s.serviceName, key)
conn := s.pool.Get()
defer conn.Close()
if _, err := conn.Do("SET", prefixedCacheKey, string(value), "EX", s.cacheTTLInSeconds); err != nil {
return err
}
return nil
}
// WriteManyToCache writes many values to the cache
func (s *Client) WriteManyToCache(ctx context.Context, pairs map[string][]byte) error {
if s.instrumentationClient != nil {
txn := s.instrumentationClient.GetTraceFromContext(ctx)
span := s.instrumentationClient.StartRedisDatastoreSegment(txn, "redis-write-many-to-cache")
defer span.End()
}
// validate the key
if len(pairs) == 0 {
return fmt.Errorf("empty cache reference set")
}
conn := s.pool.Get()
defer conn.Close()
// Use the redis.Args helper to create the arguments for the MSET command
args := redis.Args{}.AddFlat(pairs)
if _, err := conn.Do("MSET", args...); err != nil {
return err
}
return nil
}
// GetFromCache reads a value from the cache
func (s *Client) GetFromCache(ctx context.Context, key string) ([]byte, error) {
if s.instrumentationClient != nil {
txn := s.instrumentationClient.GetTraceFromContext(ctx)
span := s.instrumentationClient.StartRedisDatastoreSegment(txn, "redis-read-from-cache")
defer span.End()
}
// validate the key
if key == "" {
return nil, fmt.Errorf("empty key")
}
conn := s.pool.Get()
defer conn.Close()
value, err := redis.Bytes(conn.Do("GET", key))
if err != nil {
return nil, err
}
return value, nil
}
// GetManyFromCache retrieves multiple values from the cache based on the provided keys.
// It returns a slice of byte slices representing the retrieved values, or an error if any occurred.
func (s *Client) GetManyFromCache(ctx context.Context, keys []string) ([][]byte, error) {
isNilByteSlice := func(b []byte) bool {
return b == nil
}
if s.instrumentationClient != nil {
txn := s.instrumentationClient.GetTraceFromContext(ctx)
span := s.instrumentationClient.StartRedisDatastoreSegment(txn, "redis-read-many-from-cache")
defer span.End()
}
// validate the key
if len(keys) == 0 {
return nil, fmt.Errorf("empty key")
}
args := redis.Args{}.AddFlat(keys)
conn := s.pool.Get()
defer conn.Close()
values, err := redis.ByteSlices(conn.Do("MGET", args...))
if err != nil {
return nil, err
}
for _, value := range values {
// this occurs if there was no matching value for the query
if isNilByteSlice(value) {
return nil, fmt.Errorf("nil value")
}
}
return values, nil
}
// DeleteFromCache deletes a value from the cache
func (s *Client) DeleteFromCache(ctx context.Context, key string) error {
if s.instrumentationClient != nil {
txn := s.instrumentationClient.GetTraceFromContext(ctx)
span := s.instrumentationClient.StartRedisDatastoreSegment(txn, "redis-delete-from-cache")
defer span.End()
}
// validate the key
if key == "" {
return fmt.Errorf("empty key")
}
conn := s.pool.Get()
defer conn.Close()
if _, err := conn.Do("DEL", key); err != nil {
return err
}
return nil
}
// WriteAnyToCache writes the given value to the cache using the specified key.
// The value is first marshaled into JSON format before being written to the cache.
// If an error occurs during marshaling or writing to the cache, it is returned.
func (s *Client) WriteAnyToCache(ctx context.Context, key string, value interface{}) error {
if s.instrumentationClient != nil {
txn := s.instrumentationClient.GetTraceFromContext(ctx)
span := s.instrumentationClient.StartRedisDatastoreSegment(txn, "redis-write-any-to-cache")
defer span.End()
}
data, err := json.Marshal(value)
if err != nil {
return err
}
if err := s.WriteToCache(ctx, key, data); err != nil {
return err
}
return nil
}
// WriteToCacheWithTTL writes a value to the cache with a defined ttl in seconds
func (s *Client) WriteToCacheWithTTL(ctx context.Context, key string, value []byte, timeToLiveInSeconds int) error {
if timeToLiveInSeconds <= 0 {
timeToLiveInSeconds = 60
}
if s.instrumentationClient != nil {
txn := s.instrumentationClient.GetTraceFromContext(ctx)
span := s.instrumentationClient.StartRedisDatastoreSegment(txn, "redis-write-to-cache-with-ttl")
defer span.End()
}
// validate the key
if key == "" {
return fmt.Errorf("empty key")
}
prefixedCacheKey := fmt.Sprintf("%s:%s", s.serviceName, key)
conn := s.pool.Get()
defer conn.Close()
if _, err := conn.Do("SET", prefixedCacheKey, string(value), "EX", timeToLiveInSeconds); err != nil {
return err
}
return nil
}