-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
200 lines (168 loc) · 5.42 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package repositorysdk
import (
"context"
"encoding/json"
"github.com/go-redis/redis/v8"
"time"
)
type RedisRepository interface {
SaveCache(string, interface{}, int) error
SaveHashCache(string, string, string, int) error
SaveAllHashCache(string, map[string]string, int) error
GetCache(string, interface{}) error
GetHashCache(string, string) (string, error)
GetAllHashCache(string) (map[string]string, error)
RemoveCache(string) error
SetExpire(string, int) error
Exist(key string) (bool, error)
}
const RedisKeepTTL = 0
type redisRepository struct {
client *redis.Client
}
func NewRedisRepository(client *redis.Client) RedisRepository {
return &redisRepository{client: client}
}
// SaveCache saves cache to redis by using the command `SET`.
// Zero expiration time means no expiration time for cache.
//
// Parameters:
// - key: the cache key.
// - value: the cache value to be saved.
// - ttl: the expiration time for cache in seconds, 0 means no expiration time.
//
// Returns:
// - err: an error if something goes wrong, otherwise nil.
func (r *redisRepository) SaveCache(key string, value interface{}, ttl int) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
v, err := json.Marshal(value)
if err != nil {
return
}
return r.client.Set(ctx, key, v, time.Duration(ttl)*time.Second).Err()
}
// SaveHashCache saves a single field cache to redis.
//
// Parameters:
// - key: the cache key.
// - field: the cache field to be saved.
// - value: the cache value to be saved.
// - ttl: the expiration time for cache in seconds.
//
// Returns:
// - err: an error if something goes wrong, otherwise nil.
func (r *redisRepository) SaveHashCache(key string, field string, value string, ttl int) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := r.client.HSet(ctx, key, field, value).Err(); err != nil {
return err
}
if ttl > 0 {
return r.client.Expire(ctx, key, time.Duration(ttl)*time.Second).Err()
}
return nil
}
// SaveAllHashCache saves multiple field cache to redis.
//
// Parameters:
// - key: the cache key.
// - value: a map containing the fields and values to be saved.
// - ttl: the expiration time for cache in seconds.
//
// Returns:
// - err: an error if something goes wrong, otherwise nil.
func (r *redisRepository) SaveAllHashCache(key string, value map[string]string, ttl int) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := r.client.HSet(ctx, key, value).Err(); err != nil {
return err
}
if ttl > 0 {
return r.client.Expire(ctx, key, time.Duration(ttl)*time.Second).Err()
}
return nil
}
// GetHashCache retrieves a single field cache from redis.
//
// Parameters:
// - key: the cache key.
// - field: the cache field to be retrieved.
//
// Returns:
// - string: the cache value if it exists.
// - err: an error if something goes wrong, otherwise nil.
func (r *redisRepository) GetHashCache(key string, field string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return r.client.HGet(ctx, key, field).Result()
}
// GetAllHashCache retrieves all fields of a hash cache from redis.
//
// Parameters:
// - key: the cache key.
//
// Returns:
// - map[string]string: a map containing all the fields and their values if the hash exists.
// - error: an error if something goes wrong, otherwise nil.
func (r *redisRepository) GetAllHashCache(key string) (map[string]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return r.client.HGetAll(ctx, key).Result()
}
// GetCache retrieves a cache from redis.
//
// Parameters:
// - key: the cache key.
// - value: a pointer to the object that will hold the unmarshalled cache value.
//
// Returns:
// - error: an error if something goes wrong, otherwise nil.
func (r *redisRepository) GetCache(key string, value interface{}) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
v, err := r.client.Get(ctx, key).Result()
if err != nil {
return
}
return json.Unmarshal([]byte(v), value)
}
// RemoveCache removes a cache from redis.
//
// Parameters:
// - key: the cache key to be removed.
//
// Returns:
// - error: an error if something goes wrong, otherwise nil.
func (r *redisRepository) RemoveCache(key string) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_, err = r.client.Del(ctx, key).Result()
return err
}
// SetExpire sets an expiration time for a cache in redis.
//
// Parameters:
// - key: the cache key to set expiration for.
// - ttl: the expiration time for cache in seconds.
//
// Returns:
// - error: an error if something goes wrong, otherwise nil.
func (r *redisRepository) SetExpire(key string, ttl int) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return r.client.Expire(ctx, key, time.Duration(ttl)*time.Second).Err()
}
// Exist checks if a key exists in the Redis database.
// Parameters:
// - key: the key to check.
//
// Return values:
// - bool: true if the key exists, false otherwise.
// - error: if the Redis operation fails.
func (r *redisRepository) Exist(key string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res := r.client.Exists(ctx, key)
return res.Val() == 1, res.Err()
}