forked from motiv-labs/janus
/
redis.go
97 lines (79 loc) · 2.4 KB
/
redis.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
86
87
88
89
90
91
92
93
94
95
96
97
package store
import (
"encoding/json"
"time"
"github.com/go-redis/redis"
"github.com/hellofresh/janus/pkg/notifier"
log "github.com/sirupsen/logrus"
)
const defaultPrefix = "janus"
// RedisStore is the redis store.
type RedisStore struct {
// The prefix to use for the key.
Prefix string
// go-redis Client instance
Client *redis.Client
// The maximum number of retry under race conditions.
MaxRetry int
}
// NewRedisStore returns an instance of redis store.
func NewRedisStore(client *redis.Client, prefix string) (Store, error) {
if prefix == "" {
prefix = defaultPrefix
}
return NewRedisStoreWithOptions(client, Options{
Prefix: prefix,
})
}
// NewRedisStoreWithOptions returns an instance of redis store with custom options.
func NewRedisStoreWithOptions(client *redis.Client, options Options) (Store, error) {
store := &RedisStore{
Client: client,
Prefix: options.Prefix,
}
return store, nil
}
// ping checks if redis is alive.
func (s *RedisStore) ping() (bool, error) {
data, err := s.Client.Ping().Result()
return data == "PONG", err
}
// Exists checks if a key exists in the store
func (s *RedisStore) Exists(key string) (bool, error) {
data, err := s.Client.Exists(key).Result()
return data == 1, err
}
// Get retreives a value from the store
func (s *RedisStore) Get(key string) (string, error) {
return s.Client.Get(key).Result()
}
// Remove a value from the store
func (s *RedisStore) Remove(key string) error {
return s.Client.Del(key).Err()
}
// Set a value in the store
func (s *RedisStore) Set(key string, value string, expire int64) error {
return s.Client.Set(key, value, time.Duration(expire)*time.Second).Err()
}
// Publish publishes to a topic in redis
func (s *RedisStore) Publish(topic string, data []byte) error {
return s.Client.Publish(topic, data).Err()
}
// Subscribe subscribes to a topic in redis
func (s *RedisStore) Subscribe(channel string, callback func(notifier.Notification)) error {
pubsub := s.Client.Subscribe(channel)
defer pubsub.Close()
for {
v, err := pubsub.ReceiveMessage()
if err != nil {
log.WithError(err).Debug("An error occurred when getting the message")
return err
}
notification := notifier.Notification{}
if marshallErr := json.Unmarshal([]byte(v.Payload), ¬ification); marshallErr != nil {
log.WithError(marshallErr).Error("Unmarshalling message body failed, malformed")
return marshallErr
}
callback(notification)
}
}