forked from newgoo/redigo-pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.go
100 lines (85 loc) · 2.52 KB
/
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
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
package redigo_pack
import "github.com/garyburd/redigo/redis"
type hashRds struct {
}
//exist 为true 表示字段不存则设置其值
func (h *hashRds) HSet(key string, filed, value interface{}, exist ...bool) *Reply {
c := pool.Get()
defer c.Close()
if len(exist) > 0 && exist[0] {
return getReply(c.Do("hsetex", key, filed, value))
}
return getReply(c.Do("hset", key, filed, value))
}
//获取指定字段值
func (h *hashRds) HGet(key string, filed interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hget", key, filed))
}
//获取所有字段及值
func (h *hashRds) HGetAll(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hgetall", key))
}
//设置多个字段及值 [map]
func (h *hashRds) HMSetFromMap(key string, mp map[interface{}]interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hmset", redis.Args{}.Add(key).AddFlat(mp)...))
}
//设置多个字段及值 [struct]
func (h *hashRds) HMSetFromStruct(key string, obj interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hmset", redis.Args{}.Add(key).AddFlat(obj)...))
}
//返回多个字段值
func (h *hashRds) HMGet(key string, fileds interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hmget", redis.Args{}.Add(key).AddFlat(fileds)...))
}
//字段删除
func (h *hashRds) HDel(key string, fileds interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hdel", redis.Args{}.Add(key).AddFlat(fileds)...))
}
//判断字段是否存在
func (h *hashRds) HExists(key string, filed interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hexists", key, filed))
}
//返回所有字段
func (h *hashRds) HKeys(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hkeys", key))
}
//返回字段数量
func (h *hashRds) HLen(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hlen", key))
}
//返回所有字段值
func (h *hashRds) HVals(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hvals", key))
}
//为指定字段值增加
func (h *hashRds) HIncrBy(key string, filed interface{}, increment interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hincrby", key, filed, increment))
}
//为指定字段值增加浮点数
func (h *hashRds) HIncrByFloat(key string, filed interface{}, increment float64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hincrbyfloat", key, filed, increment))
}