summaryrefslogtreecommitdiffhomepage
path: root/storage/redis.go
blob: a6680b5a0b21bc83d5670776d4bcb8dfff47c325 (plain) (blame)
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
package storage

import "github.com/go-redis/redis/v7"

var _ CHR = &Redis{}

// Redis storage engine
type Redis redis.Client

// Create an entry in redis
func (r *Redis) Create(key, value string, checkcollision bool) error {
	if !r.Healthy() {
		return Unhealthy
	}
	if checkcollision {
		col, err := r.Exists(key).Result()
		if err != nil {
			return Unhealthy
		}
		if col > 0 {
			return Collision
		}
	}
	_, err := r.Set(key, value, 0).Result()
	return err
}

func (r *Redis) Read(key string) (string, error) {
	if !r.Healthy() {
		return "", Unhealthy
	}
	res, err := r.Get(key).Result()
	if err == redis.Nil {
		return res, NotFound
	}
	return res, err
}

// Healthy determines whether redis is responding to pings
func (r *Redis) Healthy() bool {
	_, err := r.Ping().Result()
	if err != nil {
		return false
	}
	return true
}