-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.go
96 lines (84 loc) · 2.2 KB
/
keys.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
package command
import (
"github.com/medusar/lucas/protocol"
"github.com/medusar/lucas/store"
"strconv"
)
var ttlFunc = func(args []string, r protocol.RedisRW) error {
var err error
if len(args) != 1 {
err = r.WriteError("ERR wrong number of arguments for 'ttl' command")
return err
}
ttl := store.Ttl(args[0])
return r.WriteInteger(ttl)
}
var expireFunc = func(args []string, r protocol.RedisRW) error {
if len(args) != 2 {
return r.WriteError("ERR wrong number of arguments for 'expire' command")
}
sec, err := strconv.Atoi(args[1])
if err != nil {
return r.WriteError("ERR value is not an integer or out of range")
}
set := store.Expire(args[0], sec)
if set {
return r.WriteInteger(1)
}
return r.WriteInteger(0)
}
var expireAtFunc = func(args []string, r protocol.RedisRW) error {
if len(args) != 2 {
return r.WriteError("ERR wrong number of arguments for 'expire' command")
}
timestamp, err := strconv.Atoi(args[1])
if err != nil {
return r.WriteError("ERR value is not an integer or out of range")
}
set := store.ExpireAt(args[0], int64(timestamp))
if set {
return r.WriteInteger(1)
}
return r.WriteInteger(0)
}
var keysFunc = func(args []string, r protocol.RedisRW) error {
if len(args) != 1 {
return r.WriteError("ERR wrong number of arguments for 'keys' command")
}
keys := store.Keys(args[0])
if keys == nil || len(keys) == 0 {
return r.WriteArray(nil)
}
return r.WriteArray(toBulkArray(keys))
}
var existsFunc = func(args []string, r protocol.RedisRW) error {
if len(args) == 0 {
return r.WriteError("ERR wrong number of arguments for 'exists' command")
}
total := 0
for _, key := range args {
if store.Exists(key) {
total = total + 1
}
}
return r.WriteInteger(total)
}
var delFunc = func(args []string, r protocol.RedisRW) error {
if len(args) == 0 {
return r.WriteError("ERR wrong number of arguments for 'del' command")
}
total := 0
for _, key := range args {
if store.Del(key) {
total = total + 1
}
}
return r.WriteInteger(total)
}
var typeFunc = func(args []string, r protocol.RedisRW) error {
if len(args) != 1 {
return r.WriteError("ERR wrong number of arguments for 'type' command")
}
t := store.Type(args[0])
return r.WriteString(t)
}