-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.go
186 lines (152 loc) · 3.83 KB
/
keys.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
package redis
import "regexp"
// Removes the specified keys. A key is ignored if it does not exist.
//
// Return value
// Integer reply: The number of keys that were removed.
func Del(key ...string) (deletedCount int) {
hashesMu.Lock()
defer hashesMu.Unlock()
listsMu.Lock()
defer listsMu.Unlock()
setsMu.Lock()
defer setsMu.Unlock()
stringsMu.Lock()
defer stringsMu.Unlock()
deletedCount = 0
for _, k := range key {
if _, exists := allHashes[k]; exists {
delete(allHashes, k)
deletedCount++
publish <- notice{"hash", k, "", nil}
continue
}
if _, exists := allLists[k]; exists {
delete(allLists, k)
deletedCount++
publish <- notice{"list", k, "", nil}
continue
}
if _, exists := allSets[k]; exists {
delete(allSets, k)
deletedCount++
publish <- notice{"set", k, "", nil}
continue
}
if _, exists := allStrings[k]; exists {
delete(allStrings, k)
deletedCount++
publish <- notice{"string", k, "", nil}
continue
}
}
return
}
// Returns if key exists.
//
// Return value
// Integer reply, specifically:
// 1 if the key exists.
// 0 if the key does not exist.
func Exists(key string) int {
hashesMu.RLock()
defer hashesMu.RUnlock()
listsMu.RLock()
defer listsMu.RUnlock()
setsMu.RLock()
defer setsMu.RUnlock()
stringsMu.RLock()
defer stringsMu.RUnlock()
if _, exists := allHashes[key]; exists {
return 1
}
if _, exists := allLists[key]; exists {
return 1
}
if _, exists := allSets[key]; exists {
return 1
}
if _, exists := allStrings[key]; exists {
return 1
}
return 0
}
// Returns the string representation of the type of the value stored at key. The different
// types that can be returned are: string, list, set, zset and hash.
//
// Return value
// Simple string reply: type of key, or none when key does not exist.
func Type(key string) string {
hashesMu.RLock()
defer hashesMu.RUnlock()
listsMu.RLock()
defer listsMu.RUnlock()
setsMu.RLock()
defer setsMu.RUnlock()
stringsMu.RLock()
defer stringsMu.RUnlock()
if _, exists := allHashes[key]; exists {
return "hash"
}
if _, exists := allLists[key]; exists {
return "list"
}
if _, exists := allSets[key]; exists {
return "set"
}
if _, exists := allStrings[key]; exists {
return "string"
}
return ""
}
// Returns all keys matching pattern.
//
// While the time complexity for this operation is O(N), the constant times are fairly
// low. For example, Redis running on an entry level laptop can scan a 1 million key
// database in 40 milliseconds.
// Warning: consider KEYS as a command that should only be used in production
// environments with extreme care. It may ruin performance when it is executed against
// large databases. This command is intended for debugging and special operations, such
// as changing your keyspace layout. Don't use KEYS in your regular application code.
// If you're looking for a way to find keys in a subset of your keyspace, consider using
// sets.
// Supported glob-style patterns:
// h?llo matches hello, hallo and hxllo
// h*llo matches hllo and heeeello
// h[ae]llo matches hello and hallo, but not hillo
// Use \ to escape special characters if you want to match them verbatim.
//
// Return value
// Array reply: list of keys matching pattern.
func Keys(pattern string) (out []string) {
hashesMu.RLock()
defer hashesMu.RUnlock()
listsMu.RLock()
defer listsMu.RUnlock()
setsMu.RLock()
defer setsMu.RUnlock()
stringsMu.RLock()
defer stringsMu.RUnlock()
r, _ := regexp.Compile(pattern)
for k, _ := range allHashes {
if r.MatchString(k) == true {
out = append(out, k)
}
}
for k, _ := range allLists {
if r.MatchString(k) == true {
out = append(out, k)
}
}
for k, _ := range allSets {
if r.MatchString(k) == true {
out = append(out, k)
}
}
for k, _ := range allStrings {
if r.MatchString(k) == true {
out = append(out, k)
}
}
return
}