-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey.go
More file actions
143 lines (119 loc) · 2.94 KB
/
Copy pathkey.go
File metadata and controls
143 lines (119 loc) · 2.94 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package storage
import (
"fmt"
"strconv"
"strings"
"github.com/dropbox/godropbox/errors"
)
type Key interface {
String() string
}
type KeyList []Key
type String string
func (this String) String() string {
return string(this)
}
type Int int
func (this Int) String() string {
return strconv.Itoa(int(this))
}
type KeyGetter interface {
GetKey() (key Key)
}
type KeyChangeable interface {
IsKeyChangeable() bool // 比如数据库主键auto increment,后会导致key 变化,配合 KeyGetter一起使用
}
type EmptyObjectError struct {
Key string
}
func (this EmptyObjectError) Error() string {
return fmt.Sprintf("key %s does not exists", this.Key)
}
func IsErrorEmpty(err error) bool {
if err == nil {
return false
}
switch err.(type) {
case EmptyObjectError:
return true
default:
return false
}
}
func IntList2KeyList(intList []int) (keyList []Key) {
keyList = make([]Key, len(intList))
for idx, i := range intList {
keyList[idx] = Int(i)
}
return
}
func Int64List2KeyList(intList []int64) (keyList []Key) {
keyList = make([]Key, len(intList))
for idx, i := range intList {
keyList[idx] = Int(int(i))
}
return
}
func KeyList2Int64List(keyList []Key) (intList []int64) {
intList = make([]int64, len(keyList))
for idx, key := range keyList {
intList[idx] = int64(int(key.(Int)))
}
return
}
func KeyList2IntList(keyList []Key) (intList []int) {
intList = make([]int, len(keyList))
for idx, key := range keyList {
intList[idx] = int(key.(Int))
}
return
}
func StringList2KeyList(stringList []string) (keyList []Key) {
keyList = make([]Key, len(stringList))
for idx, i := range stringList {
keyList[idx] = String(i)
}
return
}
func KeyList2StringList(keyList []Key) (stringList []string) {
stringList = make([]string, len(keyList))
for idx, i := range keyList {
stringList[idx] = i.String()
}
return
}
func BuildCacheKey(keyPrefix string, key Key) (cacheKey string, err error) {
if key == nil || key.String() == "" {
return "", errors.New("key should not be nil or to string should not be empty string")
}
cacheKey, err = strings.Join([]string{keyPrefix, key.String()}, "_"), nil
// log.Info("redis_cache_key ", cacheKey)
return
}
func BuildintCacheKey(keyPrefix string, key int) (cacheKey string) {
cacheKey = fmt.Sprintf("%s_%d", keyPrefix, key)
// log.Info("redis_cache_key ", cacheKey)
return
}
func Buildint64CacheKey(keyPrefix string, key int64) (cacheKey string) {
cacheKey = fmt.Sprintf("%s_%d", keyPrefix, key)
// log.Info("redis_cache_key ", cacheKey)
return
}
func BuildstringCacheKey(keyPrefix string, key string) (cacheKey string) {
cacheKey = fmt.Sprintf("%s_%s", keyPrefix, key)
// log.Info("redis_cache_key ", cacheKey)
return
}
func GetRawKey(key string) (rawKey String) {
keys := strings.Split(key, "_")
return String(keys[len(keys)-1])
}
func isInStringSlice(strList []string, ele string) bool {
for _, value := range strList {
if ele == value {
return true
}
}
return false
}