forked from StationA/tilenol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_cache.go
74 lines (66 loc) · 1.94 KB
/
redis_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
package tilenol
import (
"fmt"
"time"
"github.com/go-redis/redis"
)
// RedisConfig is the YAML configuration for a RedisCache
type RedisConfig struct {
// Host is the Redis cluster host name
Host string `yaml:"host"`
// Port is the Redis cluster port number
Port int `yaml:"port"`
// TTL is how long each cache entry should remain before refresh
TTL time.Duration `yaml:"ttl"`
}
// RedisCache is a Redis-backed Cache implementation
type RedisCache struct {
// Client is the backend Redis cluster client
Client *redis.Client
// TTL is how long each cache entry should remain before refresh
TTL time.Duration
}
// NewRedisCache creates a new RedisCache given a RedisConfig
func NewRedisCache(config *RedisConfig) (Cache, error) {
client := redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", config.Host, config.Port),
})
// TODO: Try to ping the server on boot?
return &RedisCache{
Client: client,
TTL: config.TTL,
}, nil
}
// Exists tries to get the cached value for a key, returning whether or not the key
// exists
func (r *RedisCache) Exists(key string) bool {
_, err := r.Client.Get(key).Bytes()
if err == redis.Nil {
return false
} else if err != nil {
// Log an error in case the connection to Redis fails, but recompute the response
Logger.Errorf("Could not talk to Redis: %v", err)
return false
}
return true
}
// Get retrieves the cached value for a given key
func (r *RedisCache) Get(key string) ([]byte, error) {
val, err := r.Client.Get(key).Bytes()
if err == redis.Nil {
return nil, ErrNoValue
} else if err != nil {
return []byte{}, err
}
return val, nil
}
// Put attempts to store a new value into Redis at a given key
func (r *RedisCache) Put(key string, val []byte) error {
err := r.Client.Set(key, val, r.TTL).Err()
if err != nil {
// Log an error in case the key can't be stored in Redis, but continue
Logger.Errorf("Could not store key [%s] in Redis: %v", key, err)
return err
}
return nil
}