summaryrefslogtreecommitdiffhomepage
path: root/storage/redis.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage/redis.go')
-rw-r--r--storage/redis.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/storage/redis.go b/storage/redis.go
new file mode 100644
index 0000000..865c0bb
--- /dev/null
+++ b/storage/redis.go
@@ -0,0 +1,40 @@
+package storage
+
+import "github.com/go-redis/redis/v7"
+
+// 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()
+}
+
+// Healthy determines whether redis is responding to pings
+func (r *Redis) Healthy() bool {
+ _, err := r.Ping().Result()
+ if err != nil {
+ return false
+ }
+ return true
+}