-
Notifications
You must be signed in to change notification settings - Fork 10
/
key.go
46 lines (36 loc) · 921 Bytes
/
key.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
package cache
import "strings"
const (
packageKey = "ca"
topicKey = "tp"
// delimiters
cacheDelim = ":"
topicDelim = "#"
)
func customKey(delimiter string, components ...string) string {
return strings.Join(components, delimiter)
}
func getCacheKey(pfx, key string) string {
return customKey(cacheDelim, packageKey, pfx, key)
}
func getCacheKeys(pfx string, keys []string) []string {
cacheKeys := make([]string, len(keys))
for i, k := range keys {
cacheKeys[i] = getCacheKey(pfx, k)
}
return cacheKeys
}
func getPrefixAndKey(cacheKey string) (string, string) {
// cacheKey = packageKey + prefix + key
idx := strings.Index(cacheKey, cacheDelim)
if idx < 0 {
return cacheKey, ""
}
// mixedKey = prefix + key
mixedKey := cacheKey[idx+len(cacheDelim):]
idx = strings.Index(mixedKey, cacheDelim)
if idx < 0 {
return mixedKey, ""
}
return mixedKey[:idx], mixedKey[idx+len(cacheDelim):]
}