summaryrefslogtreecommitdiffhomepage
path: root/storage/redis.go
blob: 317ee3e3f34196836b5aafcc6f1de6f7a361a7f2 (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
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
	}
	return r.Get(key).Result() // TODO: return NotFound conditionally
}

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