/
cache.go
85 lines (71 loc) · 1.86 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
package caching
import (
"context"
"fmt"
"log"
"strings"
"time"
"go.uber.org/zap"
"github.com/caarlos0/env"
"github.com/go-redis/redis/v8"
)
var (
ctx = context.TODO()
variableKeySymbol = "$"
)
type Cache interface {
Set(string, interface{}, time.Duration) error
Get(string) ([]byte, bool, error)
SetVar(string, string, interface{}, time.Duration) error
GetVar(string, string) ([]byte, bool, error)
}
type RedisConnectionData struct {
Host string `env:"REDIS_HOST"`
Password string `env:"REDIS_PASSWORD"`
}
type Redis struct {
cl *redis.Client
}
func InitRedis(lg *zap.Logger) *Redis {
cfg := RedisConnectionData{}
err := env.Parse(&cfg)
if err != nil {
log.Panicln("failed to parse env for redis connection", err)
}
redisClient := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:6379", cfg.Host),
Password: cfg.Password,
DB: 0, // use default DB
})
rs := &Redis{cl: redisClient}
if err = rs.Set("test", "", 1*time.Minute); err != nil {
log.Panicln("failed to test test key to redis")
}
lg.Info("redis client is ready")
return rs
}
func (r *Redis) Set(key string, val interface{}, ttl time.Duration) error {
if ttl < 0 {
ttl = 0
}
err := r.cl.Set(ctx, key, val, ttl).Err()
return err
}
func (r *Redis) Get(key string) ([]byte, bool, error) {
val, err := r.cl.Get(ctx, key).Result()
if err == redis.Nil {
return []byte{}, false, nil
} else if err != nil {
return []byte{}, false, err
} else {
return []byte(val), true, nil
}
}
func (r *Redis) SetVar(namespace, variable string, val interface{}, ttl time.Duration) error {
fullKey := strings.Join([]string{namespace, variable}, variableKeySymbol)
return r.Set(fullKey, val, ttl)
}
func (r *Redis) GetVar(namespace, variable string) ([]byte, bool, error) {
fullKey := strings.Join([]string{namespace, variable}, variableKeySymbol)
return r.Get(fullKey)
}