-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
106 lines (84 loc) · 2.17 KB
/
utils.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
package api
import (
"crypto/md5"
"database/sql"
"encoding/hex"
"time"
)
func GetMd5String(str string) string {
hash := md5.New()
hash.Write([]byte(str))
md5Hash := hash.Sum(nil)
md5String := hex.EncodeToString(md5Hash)
return md5String
}
func GetValue(tx *sql.Tx, keyID int64) (string, error) {
var value string
err := tx.QueryRow("SELECT value FROM cache_value where key_id = ?", keyID).Scan(&value)
return value, err
}
func UpdateKeyIDTTL(tx *sql.Tx, keyID int64, ttl float64) error {
var expireTime *int64 = nil
if ttl != 0 {
expire := time.Now().Add(time.Duration(ttl*1000) * time.Millisecond).Unix()
expireTime = &expire
}
_, err := tx.Exec("UPDATE cache_key SET expire_time=? WHERE id = ?", expireTime, keyID)
if err != nil {
tx.Rollback()
return err
}
return nil
}
func UpdateKeyID(tx *sql.Tx, keyID int64) error {
accessTime := time.Now().Unix()
_, err := tx.Exec("UPDATE cache_key SET access_time = ?, access_count=access_count+1 WHERE id = ?", accessTime, keyID)
if err != nil {
tx.Rollback()
return err
}
return nil
}
func InsertKeyID(tx *sql.Tx, cacheKey string) int64 {
accessTime := time.Now().Unix()
createTime := time.Now().Unix()
result, err := tx.Exec("INSERT INTO cache_key(key,expire_time,access_time,create_time) VALUES(?,?,?,?)",
cacheKey, nil, accessTime, createTime)
if err != nil {
tx.Rollback()
}
keyID, _ := result.LastInsertId()
return keyID
}
func getKeyID(tx *sql.Tx, cacheKey string) (int64, bool) {
var keyID int64
var expireTime float64
var isExists bool
err := tx.QueryRow("SELECT id, expire_time FROM cache_key where key = ?", cacheKey).Scan(&keyID, &expireTime)
nowTime := time.Now().Unix()
if err != nil {
// 不存在
isExists = false
} else if expireTime <= float64(nowTime) {
// 过期
isExists = false
} else {
isExists = true
}
return keyID, isExists
}
func GetKeyIDByCU(tx *sql.Tx, cacheKey string) (int64, int16) {
// 获取 keyID, 没有则创建,有则更新
var keyID int64
keyID, isExists := getKeyID(tx, cacheKey)
if keyID != 0 {
UpdateKeyID(tx, keyID)
} else {
keyID = InsertKeyID(tx, cacheKey)
}
if isExists {
return keyID, 0
} else {
return keyID, 1
}
}