Skip to content

Commit

Permalink
Add OBJECT IDLETIME command (#345)
Browse files Browse the repository at this point in the history
* Add OBJECT IDLETIME command

* integration test

---------

Co-authored-by: Sam Lancia <sam@vaion.com>
Co-authored-by: Harmen <alicebob@lijzij.de>
  • Loading branch information
3 people committed Oct 11, 2023
1 parent 279adef commit a64b9ef
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 29 deletions.
4 changes: 2 additions & 2 deletions cmd_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func makeCmdExpire(m *Miniredis, unix bool, d time.Duration) func(*server.Peer,
return
}
db.ttl[opts.key] = newTTL
db.keyVersion[opts.key]++
db.touch(opts.key, true)
db.checkTTL(opts.key)
c.WriteInt(1)
})
Expand Down Expand Up @@ -274,7 +274,7 @@ func (m *Miniredis) cmdPersist(c *server.Peer, cmd string, args []string) {
return
}
delete(db.ttl, key)
db.keyVersion[key]++
db.touch(key, true)
c.WriteInt(1)
})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (m *Miniredis) cmdHsetnx(c *server.Peer, cmd string, args []string) {
return
}
db.hashKeys[opts.key][opts.field] = opts.value
db.keyVersion[opts.key]++
db.touch(opts.key, true)
c.WriteInt(1)
})
}
Expand Down
8 changes: 4 additions & 4 deletions cmd_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func (m *Miniredis) cmdLinsert(c *server.Peer, cmd string, args []string) {
}
}
db.listKeys[key] = l
db.keyVersion[key]++
db.touch(key, true)
c.WriteInt(len(l))
return
}
Expand Down Expand Up @@ -715,7 +715,7 @@ func (m *Miniredis) cmdLrem(c *server.Peer, cmd string, args []string) {
db.del(opts.key, true)
} else {
db.listKeys[opts.key] = newL
db.keyVersion[opts.key]++
db.touch(opts.key, true)
}

c.WriteInt(deleted)
Expand Down Expand Up @@ -769,7 +769,7 @@ func (m *Miniredis) cmdLset(c *server.Peer, cmd string, args []string) {
return
}
l[index] = opts.value
db.keyVersion[opts.key]++
db.touch(opts.key, true)

c.WriteOK()
})
Expand Down Expand Up @@ -823,7 +823,7 @@ func (m *Miniredis) cmdLtrim(c *server.Peer, cmd string, args []string) {
db.del(opts.key, true)
} else {
db.listKeys[opts.key] = l
db.keyVersion[opts.key]++
db.touch(opts.key, true)
}
c.WriteOK()
})
Expand Down
58 changes: 58 additions & 0 deletions cmd_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()))
})
}
75 changes: 75 additions & 0 deletions cmd_object_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package miniredis

import (
"strconv"
"testing"
"time"

"github.com/alicebob/miniredis/v2/proto"
)

// Test OBJECT IDLETIME.
func TestObjectIdletime(t *testing.T) {
s, err := Run()
ok(t, err)
defer s.Close()
c, err := proto.Dial(s.Addr())
ok(t, err)
defer c.Close()

{
start := time.Now()
s.SetTime(start)

mustOK(t, c,
"SET", "foo", "bar",
)

{
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Int(0),
)
}

s.SetTime(start.Add(time.Minute))

{
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Int(60),
)
}

s.Get("foo")

{
mustDo(t, c,
"object", "idletime", "foo",
proto.Int(0),
)
}

s.Del("foo")

{
mustDo(t, c,
"OBJECT", "IDLETIME", "foo",
proto.Nil,
)
}
}
}

func objectTime(c *proto.Client, k string) (int, error) {
ret, err := c.Do("OBJECT", "IDLETIME", k)
if err != nil {
return 0, nil
}

if ret == "(nil)" {
return -1, nil
}

return strconv.Atoi(ret)
}
4 changes: 2 additions & 2 deletions cmd_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (m *Miniredis) cmdXadd(c *server.Peer, cmd string, args []string) {
if minID != "" {
s.trimBefore(minID)
}
db.keyVersion[key]++
db.touch(key, true)

c.WriteBulk(newID)
})
Expand Down Expand Up @@ -931,7 +931,7 @@ func (m *Miniredis) cmdXdel(c *server.Peer, cmd string, args []string) {
c.WriteError(err.Error())
return
}
db.keyVersion[stream]++
db.touch(stream, true)
c.WriteInt(n)
})
}
Expand Down

0 comments on commit a64b9ef

Please sign in to comment.