-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add OBJECT IDLETIME command * integration test --------- Co-authored-by: Sam Lancia <sam@vaion.com> Co-authored-by: Harmen <alicebob@lijzij.de>
- Loading branch information
1 parent
279adef
commit a64b9ef
Showing
11 changed files
with
200 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.