/
hash.go
168 lines (146 loc) · 5.48 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
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
package cache
import (
"context"
"time"
"github.com/gomodule/redigo/redis"
)
// HashSet will set the hashKey to the value in the specified hashName and link a
// reference to each dependency for the entire hash
// Creates a new connection and closes connection at end of function call
//
// Custom connections use method: HashSetRaw()
func HashSet(ctx context.Context, client *Client, hashName, hashKey string,
value interface{}, dependencies ...string) error {
conn, err := client.GetConnectionWithContext(ctx)
if err != nil {
return err
}
defer client.CloseConnection(conn)
return HashSetRaw(conn, hashName, hashKey, value, dependencies...)
}
// HashSetRaw will set the hashKey to the value in the specified hashName and link a
// reference to each dependency for the entire hash
// Uses existing connection (does not close connection)
//
// Spec: https://redis.io/commands/hset
func HashSetRaw(conn redis.Conn, hashName, hashKey string, value interface{}, dependencies ...string) error {
if _, err := conn.Do(HashKeySetCommand, hashName, hashKey, value); err != nil {
return err
}
return linkDependencies(conn, hashName, dependencies...)
}
// HashGet gets a key from redis via hash
// Creates a new connection and closes connection at end of function call
//
// Custom connections use method: HashGetRaw()
func HashGet(ctx context.Context, client *Client, hash, key string) (string, error) {
conn, err := client.GetConnectionWithContext(ctx)
if err != nil {
return "", err
}
defer client.CloseConnection(conn)
return HashGetRaw(conn, hash, key)
}
// HashGetRaw gets a key from redis via hash
// Uses existing connection (does not close connection)
//
// Spec: https://redis.io/commands/hget
func HashGetRaw(conn redis.Conn, hash, key string) (string, error) {
return redis.String(conn.Do(HashGetCommand, hash, key))
}
// HashMapGet gets values from a hash map for corresponding keys
// Creates a new connection and closes connection at end of function call
//
// Custom connections use method: HashMapGetRaw()
func HashMapGet(ctx context.Context, client *Client, hashName string, keys ...interface{}) ([]string, error) {
conn, err := client.GetConnectionWithContext(ctx)
if err != nil {
return nil, err
}
defer client.CloseConnection(conn)
return HashMapGetRaw(conn, hashName, keys...)
}
// HashMapGetRaw gets values from a hash map for corresponding keys
// Uses existing connection (does not close connection)
//
// Spec: https://redis.io/commands/hmget
func HashMapGetRaw(conn redis.Conn, hashName string, keys ...interface{}) ([]string, error) {
// Build up the arguments
keys = append([]interface{}{hashName}, keys...)
// Fire the command with all keys
return redis.Strings(conn.Do(HashMapGetCommand, keys...))
}
// HashMapSet will set the hashKey to the value in the specified hashName and link a
// reference to each dependency for the entire hash
// Creates a new connection and closes connection at end of function call
//
// Custom connections use method: HashMapSetRaw()
func HashMapSet(ctx context.Context, client *Client, hashName string,
pairs [][2]interface{}, dependencies ...string) error {
conn, err := client.GetConnectionWithContext(ctx)
if err != nil {
return err
}
defer client.CloseConnection(conn)
return HashMapSetRaw(conn, hashName, pairs, dependencies...)
}
// HashMapSetRaw will set the hashKey to the value in the specified hashName and link a
// reference to each dependency for the entire hash
// Uses existing connection (does not close connection)
//
// Spec: https://redis.io/commands/hmset
func HashMapSetRaw(conn redis.Conn, hashName string, pairs [][2]interface{}, dependencies ...string) error {
// Set the arguments
args := make([]interface{}, 0, 2*len(pairs)+1)
args = append(args, hashName)
for _, pair := range pairs {
args = append(args, pair[0])
args = append(args, pair[1])
}
// Set the hash map
if _, err := conn.Do(HashMapSetCommand, args...); err != nil {
return err
}
// Link and return the error
return linkDependencies(conn, hashName, dependencies...)
}
// HashMapSetExp will set the hashKey to the value in the specified hashName and link a
// reference to each dependency for the entire hash
// Creates a new connection and closes connection at end of function call
//
// Custom connections use method: HashMapSetExpRaw()
func HashMapSetExp(ctx context.Context, client *Client, hashName string, pairs [][2]interface{},
ttl time.Duration, dependencies ...string) error {
conn, err := client.GetConnectionWithContext(ctx)
if err != nil {
return err
}
defer client.CloseConnection(conn)
return HashMapSetExpRaw(conn, hashName, pairs, ttl, dependencies...)
}
// HashMapSetExpRaw will set the hashKey to the value in the specified hashName and link a
// reference to each dependency for the entire hash
// Uses existing connection (does not close connection)
//
// Commands:
// https://redis.io/commands/hmset
// https://redis.io/commands/expire
func HashMapSetExpRaw(conn redis.Conn, hashName string, pairs [][2]interface{},
ttl time.Duration, dependencies ...string) error {
// Set the arguments
args := make([]interface{}, 0, 2*len(pairs)+1)
args = append(args, hashName)
for _, pair := range pairs {
args = append(args, pair[0], pair[1])
}
// Set the hash map
if _, err := conn.Do(HashMapSetCommand, args...); err != nil {
return err
}
// Fire the "expire" command
if _, err := conn.Do(ExpireCommand, hashName, int64(ttl.Seconds())); err != nil {
return err
}
// Link and return the error
return linkDependencies(conn, hashName, dependencies...)
}