This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
113 lines (96 loc) · 2.51 KB
/
store.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
package main
import (
"fmt"
"time"
"github.com/go-redis/redis"
)
var (
ErrStoreSetValue = fmt.Errorf("ErrStoreSetValue")
ErrStoreKeyNotExist = fmt.Errorf("ErrStoreKeyNotExist")
)
type DataStore interface {
Set(key string, value string) (err error)
Get(key string) (value string, err error)
Expire(key string, ttlSeconds int) (err error)
Delete(key string) (err error)
HSet(key string, field string, value string) (bool, error)
HGet(key string, field string) (string, error)
HGetAll(key string) (map[string]string, error)
Keys(pattern string) ([]string, error)
}
type RedisDataStore struct {
client *redis.Client
prefix string
}
var _ DataStore = &RedisDataStore{}
func NewRedisDataStore(client *redis.Client, prefix string) DataStore {
return &RedisDataStore{
client: client,
prefix: prefix,
}
}
func (s *RedisDataStore) fmtKey(key string) string {
return fmt.Sprintf("%s:%s", s.prefix, key)
}
func (s *RedisDataStore) extractKey(key string) string {
return key[len(s.prefix)+1:]
}
func (s *RedisDataStore) Set(key string, value string) (err error) {
var cmd *redis.StatusCmd
key = s.fmtKey(key)
cmd = s.client.Set(key, value, 0)
err = cmd.Err()
return
}
func (s *RedisDataStore) Expire(key string, ttl int) (err error) {
var cmd *redis.BoolCmd
key = s.fmtKey(key)
cmd = s.client.Expire(key, time.Duration(ttl)*time.Second)
err = cmd.Err()
return
}
func (s *RedisDataStore) Get(key string) (value string, err error) {
key = s.fmtKey(key)
var cmd = s.client.Get(key)
var bytesValue []byte
bytesValue, err = cmd.Bytes()
if err != nil {
return
}
if bytesValue == nil {
err = ErrStoreKeyNotExist
return
}
value = string(bytesValue)
return
}
func (s *RedisDataStore) Delete(key string) (err error) {
key = s.fmtKey(key)
var cmd = s.client.Del(key)
err = cmd.Err()
return
}
func (s *RedisDataStore) HSet(key string, field string, value string) (bool, error) {
key = s.fmtKey(key)
var cmd = s.client.HSet(key, field, value)
return cmd.Result()
}
func (s *RedisDataStore) HGetAll(key string) (map[string]string, error) {
key = s.fmtKey(key)
var cmd = s.client.HGetAll(key)
return cmd.Result()
}
func (s *RedisDataStore) HGet(key string, field string) (string, error) {
key = s.fmtKey(key)
var cmd = s.client.HGet(key, field)
return cmd.Result()
}
func (s *RedisDataStore) Keys(pattern string) (keys []string, err error) {
pattern = s.fmtKey(pattern)
var cmd = s.client.Keys(pattern)
keys, err = cmd.Result()
for i := 0; i< len(keys); i++ {
keys[i] = s.extractKey(keys[i])
}
return
}