-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
139 lines (103 loc) · 2.73 KB
/
list.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
package api
import (
"fmt"
"log"
)
func (dc *DiskCache) LPush(cacheKey string, cacheValue string) error {
tx := dc.Tx()
defer tx.Commit()
keyID, _ := GetKeyIDByCU(tx, cacheKey)
vauleMd5 := GetMd5String(cacheValue)
// 插入内容
_, err := tx.Exec("INSERT INTO cache_value(key_id, value, value_md5) VALUES(?,?,?)",
keyID, cacheValue, vauleMd5)
if err != nil {
tx.Rollback()
return err
}
return nil
}
func (dc *DiskCache) LPop(cacheKey string) (string, error) {
return dc._pop(cacheKey, "left")
}
func (dc *DiskCache) RPop(cacheKey string) (string, error) {
return dc._pop(cacheKey, "right")
}
func (dc *DiskCache) _pop(cacheKey string, turnTo string) (string, error) {
orderBy := "id asc"
if turnTo == "left" {
orderBy = "id desc"
}
keyID, err := dc.GetKeyIDNotTx(cacheKey)
if err != nil {
return "", err
}
tx := dc.Tx()
defer tx.Commit()
var value string
var valueID int64
query := fmt.Sprintf("SELECT id, value FROM cache_value WHERE key_id = ? ORDER BY %s", orderBy)
err = tx.QueryRow(query, keyID).Scan(&valueID, &value)
if err != nil {
return "", err
}
_, err = tx.Exec("DELETE FROM cache_value WHERE id = ?", valueID)
if err != nil {
return "", err
}
return value, nil
}
func (dc *DiskCache) RRange(cacheKey string, offset int64, limit int64) []string {
return dc._range(cacheKey, offset, limit, "right")
}
func (dc *DiskCache) LRange(cacheKey string, offset int64, limit int64) []string {
return dc._range(cacheKey, offset, limit, "left")
}
func (dc *DiskCache) _range(cacheKey string, offset int64, limit int64, turnTo string) []string {
orderBy := "id asc"
if turnTo == "left" {
orderBy = "id desc"
}
keyID, err := dc.GetKeyIDNotTx(cacheKey)
if err != nil {
return []string{}
}
tx := dc.Tx()
defer tx.Commit()
query := fmt.Sprintf("SELECT value FROM cache_value WHERE key_id = ? ORDER BY %s limit ?, ?", orderBy)
rows, err := tx.Query(query, keyID, offset, limit)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// 遍历结果
var values []string
for rows.Next() {
var value string
if err := rows.Scan(&value); err != nil {
log.Fatal(err)
}
values = append(values, value)
}
return values
}
func (dc *DiskCache) LLen(cacheKey string) int64 {
keyID, err := dc.GetKeyIDNotTx(cacheKey)
if err != nil {
return 0
}
var c int64
dc.Conn.QueryRowContext(dc.Ctx, "SELECT count(*) FROM cache_value WHERE key_id = ?", keyID).Scan(&c)
return c
}
func (dc *DiskCache) LRem(cacheKey string, cacheValue string) error {
keyID, err := dc.GetKeyIDNotTx(cacheKey)
vauleMd5 := GetMd5String(cacheValue)
if err != nil {
return err
}
tx := dc.Tx()
defer tx.Commit()
tx.Exec("DELETE FROM cache_value WHERE key_id = ? and value_md5 = ?;", keyID, vauleMd5)
return nil
}