fx.go
982 Bytes
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
package lxmysql
import (
"apigame/util/util-lx/lxconv"
lconv "github.com/lixu-any/go-tools/conv"
)
// HashCode 计算 SQL 语句的哈希码
func HashCode(sql string, args []interface{}) int {
hash := 17
hash = hash*31 + len(sql)
for _, arg := range args {
hash = hash*31 + HashCodeOfArg(arg)
}
return hash
}
// HashCodeOfArg 计算参数的哈希码
func HashCodeOfArg(arg interface{}) int {
switch v := arg.(type) {
case int:
return v
case int8:
return int(v)
case int16:
return int(v)
case int32:
return int(v)
case int64:
return int(v)
case uint:
return int(v)
case uint8:
return int(v)
case uint16:
return int(v)
case uint32:
return int(v)
case uint64:
return int(v)
case float32:
return int(v)
case float64:
return int(v)
case string:
return len(v)
default:
return 0
}
}
// GenCacheKey 生成缓存名字
func GenCacheKey(sql string, args []interface{}) string {
return lxconv.EncryMD5(sql + lconv.JsonEncode(args))
}