-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
43 lines (38 loc) · 966 Bytes
/
hash.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
package RedisGo
import "time"
func (r RedisInstance) WriteToRedisHash(key string, field string, value string) error {
err := r.HSet(key, field, value).Err()
if err != nil {
return err
}
return nil
}
func (r RedisInstance) WriteToRedisHashWithTTL(key string, field string, value string, ttl time.Duration) error {
// make hash set with ttl
err := r.HSet(key, field, value).Err()
if err != nil {
return err
}
// set the ttl
go r.Expire(key, ttl)
return nil
}
func (r RedisInstance) ReadFromRedisHash(key string, field string) (string, error) {
val, err := r.HGet(key, field).Result()
if err != nil {
return "", err
}
return val, nil
}
func (r RedisInstance) ReadFromRedisHashWithTTL(key string, field string) (string, time.Duration, error) {
val, err := r.HGet(key, field).Result()
if err != nil {
return "", 0, err
}
// get the ttl
ttlVal, err := r.TTL(key).Result()
if err != nil {
return "", 0, err
}
return val, ttlVal, nil
}