forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_redis.go
More file actions
126 lines (112 loc) · 3.06 KB
/
cache_redis.go
File metadata and controls
126 lines (112 loc) · 3.06 KB
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
package redis
import (
"github.com/devfeel/dotweb/framework/redis"
"strconv"
)
var (
ZeroInt64 int64 = 0
)
// RedisCache is redis cache adapter.
// it contains serverIp for redis conn.
type RedisCache struct {
serverURL string //connection string, like "redis://:password@10.0.1.11:6379/0"
}
// NewRedisCache returns a new *RedisCache.
func NewRedisCache(serverURL string) *RedisCache {
cache := RedisCache{serverURL: serverURL}
return &cache
}
// Exists check item exist in redis cache.
func (ca *RedisCache) Exists(key string) (bool, error) {
redisClient := redisutil.GetRedisClient(ca.serverURL)
exists, err := redisClient.Exists(key)
return exists, err
}
// Incr increase int64 counter in redis cache.
func (ca *RedisCache) Incr(key string) (int64, error) {
redisClient := redisutil.GetRedisClient(ca.serverURL)
val, err := redisClient.INCR(key)
if err != nil {
return 0, err
}
return int64(val), nil
}
// Decr decrease counter in redis cache.
func (ca *RedisCache) Decr(key string) (int64, error) {
redisClient := redisutil.GetRedisClient(ca.serverURL)
val, err := redisClient.DECR(key)
if err != nil {
return 0, err
}
return int64(val), nil
}
// Get cache from redis cache.
// if non-existed or expired, return nil.
func (ca *RedisCache) Get(key string) (interface{}, error) {
redisClient := redisutil.GetRedisClient(ca.serverURL)
reply, err := redisClient.GetObj(key)
return reply, err
}
// returns value string format by given key
// if non-existed or expired, return "".
func (ca *RedisCache) GetString(key string) (string, error) {
redisClient := redisutil.GetRedisClient(ca.serverURL)
reply, err := redisClient.Get(key)
return reply, err
}
// returns value int format by given key
// if non-existed or expired, return nil.
func (ca *RedisCache) GetInt(key string) (int, error) {
v, err := ca.GetString(key)
if err != nil || v == "" {
return 0, err
} else {
i, e := strconv.Atoi(v)
if e != nil {
return 0, err
} else {
return i, nil
}
}
}
// returns value int64 format by given key
// if non-existed or expired, return nil.
func (ca *RedisCache) GetInt64(key string) (int64, error) {
v, err := ca.GetString(key)
if err != nil || v == "" {
return ZeroInt64, err
} else {
i, e := strconv.ParseInt(v, 10, 64)
if e != nil {
return ZeroInt64, err
} else {
return i, nil
}
}
}
// Set cache to redis.
// ttl is second, if ttl is 0, it will be forever.
func (ca *RedisCache) Set(key string, value interface{}, ttl int64) error {
redisClient := redisutil.GetRedisClient(ca.serverURL)
var err error
if ttl <= 0{
_, err = redisClient.Set(key, value)
}else{
_, err = redisClient.SetWithExpire(key, value, ttl)
}
return err
}
// Delete item in redis cacha.
// if not exists, we think it's success
func (ca *RedisCache) Delete(key string) error {
redisClient := redisutil.GetRedisClient(ca.serverURL)
_, err := redisClient.Del(key)
return err
}
// ClearAll will delete all item in redis cache.
// never error
func (ca *RedisCache) ClearAll() error {
redisClient := redisutil.GetRedisClient(ca.serverURL)
redisClient.FlushDB()
return nil
}