-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
60 lines (48 loc) · 1.04 KB
/
state.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
package main
import (
"fmt"
"log"
"gopkg.in/redis.v5"
)
type State interface {
Get(key string) (string, error)
Set(key, value string) error
Clear(namespace string) error
}
type RedisState struct {
client *redis.Client
}
func NewRedisState(addr string) (*RedisState, error) {
redisClient := redis.NewClient(&redis.Options{
Addr: addr,
Password: "", // no password set
DB: 0, // use default DB
})
_, err := redisClient.Ping().Result()
if err != nil {
return nil, err
}
return &RedisState{
client: redisClient,
}, nil
}
func (r *RedisState) Set(key, value string) error {
res := r.client.Set(key, value, -1)
return res.Err()
}
func (r *RedisState) Get(key string) (string, error) {
res := r.client.Get(key)
return res.Val(), res.Err()
}
func (r *RedisState) Clear(namespace string) error {
keysCmd := r.client.Keys(fmt.Sprintf("k8s-deployer/%s/*", namespace))
keys, err := keysCmd.Result()
if err != nil {
return err
}
for _, key := range keys {
log.Println("Removing:", key)
r.client.Del(key)
}
return nil
}