-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.go
123 lines (103 loc) · 2.65 KB
/
memory.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Package redis implements a Redis integration for the Joe bot library.
// https://github.com/go-joe/joe
package redis
import (
"fmt"
"github.com/go-joe/joe"
"github.com/go-redis/redis"
"go.uber.org/zap"
)
// Config contains all settings for the Redis memory.
type Config struct {
Addr string
Key string
Password string
DB int
Logger *zap.Logger
}
type memory struct {
logger *zap.Logger
Client *redis.Client
hkey string
}
// Memory returns a jos Module that configures the bot to use Redis as key-value
// store.
func Memory(addr string, opts ...Option) joe.Module {
return joe.ModuleFunc(func(joeConf *joe.Config) error {
conf := Config{Addr: addr}
for _, opt := range opts {
err := opt(&conf)
if err != nil {
return err
}
}
if conf.Logger == nil {
conf.Logger = joeConf.Logger("redis")
}
memory, err := NewMemory(conf)
if err != nil {
return err
}
joeConf.SetMemory(memory)
return nil
})
}
// NewMemory creates a Redis implementation of a joe.Memory.
func NewMemory(conf Config) (joe.Memory, error) {
if conf.Logger == nil {
conf.Logger = zap.NewNop()
}
if conf.Key == "" {
conf.Key = "joe-bot"
}
memory := &memory{
logger: conf.Logger,
hkey: conf.Key,
}
memory.logger.Debug("Connecting to redis memory",
zap.String("addr", conf.Addr),
zap.String("key", memory.hkey),
)
memory.Client = redis.NewClient(&redis.Options{
Addr: conf.Addr,
Password: conf.Password,
DB: conf.DB,
})
_, err := memory.Client.Ping().Result()
if err != nil {
return nil, fmt.Errorf("failed to ping redis: %w", err)
}
memory.logger.Info("Memory initialized successfully")
return memory, nil
}
// Set implements joe.Memory by settings the key to the given value in a Redis
// hash set.
func (b *memory) Set(key string, value []byte) error {
resp := b.Client.HSet(b.hkey, key, value)
return resp.Err()
}
// Get implements joe.Memory by retrieving a key from a Redis hash set.
func (b *memory) Get(key string) ([]byte, bool, error) {
res, err := b.Client.HGet(b.hkey, key).Result()
switch {
case err == redis.Nil:
return nil, false, nil
case err != nil:
return nil, false, err
default:
return []byte(res), true, nil
}
}
// Delete implements joe.Memory by deleting the given key from the Redis hash set.
func (b *memory) Delete(key string) (bool, error) {
res, err := b.Client.HDel(b.hkey, key).Result()
return res > 0, err
}
// Keys implements joe.Memory by returning all previously set keys from Redis
func (b *memory) Keys() ([]string, error) {
return b.Client.HKeys(b.hkey).Result()
}
// Close terminates the Redis connection
func (b *memory) Close() error {
return b.Client.Close()
}