forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_object.go
58 lines (49 loc) · 1.04 KB
/
cmd_object.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
package miniredis
import (
"fmt"
"strings"
"github.com/alicebob/miniredis/v2/server"
)
// commandsObject handles all object operations.
func commandsObject(m *Miniredis) {
m.srv.Register("OBJECT", m.cmdObject)
}
// OBJECT
func (m *Miniredis) cmdObject(c *server.Peer, cmd string, args []string) {
if len(args) == 0 {
setDirty(c)
c.WriteError(errWrongNumber(cmd))
return
}
if !m.handleAuth(c) {
return
}
if m.checkPubsub(c, cmd) {
return
}
switch sub := strings.ToLower(args[0]); sub {
case "idletime":
m.cmdObjectIdletime(c, args[1:])
default:
setDirty(c)
c.WriteError(fmt.Sprintf(msgFObjectUsage, sub))
}
}
// OBJECT IDLETIME
func (m *Miniredis) cmdObjectIdletime(c *server.Peer, args []string) {
if len(args) != 1 {
setDirty(c)
c.WriteError(errWrongNumber("object|idletime"))
return
}
key := args[0]
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
db := m.db(ctx.selectedDB)
t, ok := db.lru[key]
if !ok {
c.WriteNull()
return
}
c.WriteInt(int(db.master.effectiveNow().Sub(t).Seconds()))
})
}