Skip to content

Commit 26a70ee

Browse files
committed
update redis and validator
1 parent ca9fda7 commit 26a70ee

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

cache/cache.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package cache
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"strconv"
78
"time"
89

9-
"github.com/go-redis/redis/v7"
10+
"github.com/go-redis/redis/v8"
1011
"github.com/pkg/errors"
1112
)
1213

@@ -16,6 +17,10 @@ var (
1617
ErrNoTTLSet = errors.New("key does not have a TTL set")
1718
)
1819

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+
1924
// Cache defines basic cache operations including methods for setting and getting JSON objects.
2025
type Cache interface {
2126
Prefix() string
@@ -53,7 +58,7 @@ func NewRedis(redisHost string, redisPort string, prefix string) (*RedisClient,
5358
}
5459

5560
client := redis.NewClient(&opts)
56-
_, err := client.Ping().Result()
61+
_, err := client.Ping(ctx).Result()
5762
if err != nil {
5863
return nil, fmt.Errorf("could not ping REDIS: %w", err)
5964
}
@@ -70,14 +75,14 @@ func NewRedis(redisHost string, redisPort string, prefix string) (*RedisClient,
7075
// Redis `SET key value [expiration]` command.
7176
// Use expiration for `SETEX`-like behavior. Zero expiration means the key has no expiration time.
7277
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()
7479
}
7580

7681
// Get retrieves a value from REDIS.
7782
// If the client was set up with a prefix it will be added in front of the key.
7883
// If the value was not found ErrNotFound will be returned.
7984
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()
8186
if err == redis.Nil {
8287
return "", ErrNotFound
8388
}
@@ -124,7 +129,7 @@ func (r *RedisClient) GetInt(key string) (int64, error) {
124129
// If the client was set up with a prefix it will be added in front of the key.
125130
// It returns the new (incremented) value.
126131
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()
128133
}
129134

130135
// SetJSON saves JSON data as string to REDIS.
@@ -152,7 +157,7 @@ func (r *RedisClient) GetJSON(key string, result interface{}) error {
152157
// Del deletes a key value pair from REDIS.
153158
// If the client was set up with a prefix it will be added in front of the key.
154159
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()
156161
}
157162

158163
// Close closes the connection to the REDIS server.
@@ -163,7 +168,7 @@ func (r *RedisClient) Close() error {
163168
// TTL returns remaining time to live of the given key found in REDIS.
164169
// If the key doesn't exist, it returns ErrNotFound.
165170
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()
167172
if err != nil {
168173
return 0, err
169174
}

server/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package server
22

33
import (
4-
goValidator "github.com/go-playground/validator"
4+
goValidator "github.com/go-playground/validator/v10"
55
)
66

77
// Validator points to 3rd party validator package (library) which actually does the real validation

0 commit comments

Comments
 (0)