1
1
package cache
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"strconv"
7
8
"time"
8
9
9
- "github.com/go-redis/redis/v7 "
10
+ "github.com/go-redis/redis/v8 "
10
11
"github.com/pkg/errors"
11
12
)
12
13
16
17
ErrNoTTLSet = errors .New ("key does not have a TTL set" )
17
18
)
18
19
20
+ // Since we currently don't want to pass a Go context to all the cache methods,
21
+ // we use this dummy context instead.
22
+ var ctx = context .TODO ()
23
+
19
24
// Cache defines basic cache operations including methods for setting and getting JSON objects.
20
25
type Cache interface {
21
26
Prefix () string
@@ -53,7 +58,7 @@ func NewRedis(redisHost string, redisPort string, prefix string) (*RedisClient,
53
58
}
54
59
55
60
client := redis .NewClient (& opts )
56
- _ , err := client .Ping ().Result ()
61
+ _ , err := client .Ping (ctx ).Result ()
57
62
if err != nil {
58
63
return nil , fmt .Errorf ("could not ping REDIS: %w" , err )
59
64
}
@@ -70,14 +75,14 @@ func NewRedis(redisHost string, redisPort string, prefix string) (*RedisClient,
70
75
// Redis `SET key value [expiration]` command.
71
76
// Use expiration for `SETEX`-like behavior. Zero expiration means the key has no expiration time.
72
77
func (r * RedisClient ) Set (key string , value string , expiration time.Duration ) error {
73
- return r .Redis .Set (r .prefixedKey (key ), value , expiration ).Err ()
78
+ return r .Redis .Set (ctx , r .prefixedKey (key ), value , expiration ).Err ()
74
79
}
75
80
76
81
// Get retrieves a value from REDIS.
77
82
// If the client was set up with a prefix it will be added in front of the key.
78
83
// If the value was not found ErrNotFound will be returned.
79
84
func (r * RedisClient ) Get (key string ) (string , error ) {
80
- result , err := r .Redis .Get (r .prefixedKey (key )).Result ()
85
+ result , err := r .Redis .Get (ctx , r .prefixedKey (key )).Result ()
81
86
if err == redis .Nil {
82
87
return "" , ErrNotFound
83
88
}
@@ -124,7 +129,7 @@ func (r *RedisClient) GetInt(key string) (int64, error) {
124
129
// If the client was set up with a prefix it will be added in front of the key.
125
130
// It returns the new (incremented) value.
126
131
func (r * RedisClient ) Incr (key string ) (int64 , error ) {
127
- return r .Redis .Incr (r .prefixedKey (key )).Result ()
132
+ return r .Redis .Incr (ctx , r .prefixedKey (key )).Result ()
128
133
}
129
134
130
135
// SetJSON saves JSON data as string to REDIS.
@@ -152,7 +157,7 @@ func (r *RedisClient) GetJSON(key string, result interface{}) error {
152
157
// Del deletes a key value pair from REDIS.
153
158
// If the client was set up with a prefix it will be added in front of the key.
154
159
func (r * RedisClient ) Del (key string ) error {
155
- return r .Redis .Del (r .prefixedKey (key )).Err ()
160
+ return r .Redis .Del (ctx , r .prefixedKey (key )).Err ()
156
161
}
157
162
158
163
// Close closes the connection to the REDIS server.
@@ -163,7 +168,7 @@ func (r *RedisClient) Close() error {
163
168
// TTL returns remaining time to live of the given key found in REDIS.
164
169
// If the key doesn't exist, it returns ErrNotFound.
165
170
func (r * RedisClient ) TTL (key string ) (time.Duration , error ) {
166
- result , err := r .Redis .TTL (r .prefixedKey (key )).Result ()
171
+ result , err := r .Redis .TTL (ctx , r .prefixedKey (key )).Result ()
167
172
if err != nil {
168
173
return 0 , err
169
174
}
0 commit comments