Skip to content

Commit

Permalink
implement TTL and EXPIRE commands (DiceDB#1465)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanChinJunKai authored Feb 10, 2025
1 parent d665865 commit e94e09f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
56 changes: 56 additions & 0 deletions internal/cmd/cmd_expire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cmd

import (
"strconv"

"github.com/dicedb/dice/internal/server/utils"
dstore "github.com/dicedb/dice/internal/store"
"github.com/dicedb/dicedb-go/wire"
)

var cEXPIRE = &DiceDBCommand{
Name: "EXPIRE",
HelpShort: "EXPIRE sets an expiry(in seconds) on a specified key",
Eval: evalEXPIRE,
}

func init() {
commandRegistry.AddCommand(cEXPIRE)
}

func evalEXPIRE(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) <= 1 {
return cmdResNil, errWrongArgumentCount("EXPIRE")
}

var key = c.C.Args[0]
exDurationSec, err := strconv.ParseInt(c.C.Args[1], 10, 64)

if err != nil || exDurationSec < 0 {
return cmdResNil, errInvalidExpireTime("EXPIRE")
}

obj := s.Get(key)

if obj == nil {
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: 0},
}}, nil
}

isExpirySet, err2 := dstore.EvaluateAndSetExpiry(c.C.Args[2:], utils.AddSecondsToUnixEpoch(exDurationSec), key, s)

if err2 != nil {
return cmdResNil, err2
}

if isExpirySet {
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: 1},
}}, nil
}

return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: 0},
}}, nil
}
47 changes: 47 additions & 0 deletions internal/cmd/cmd_ttl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"github.com/dicedb/dice/internal/server/utils"
dstore "github.com/dicedb/dice/internal/store"
"github.com/dicedb/dicedb-go/wire"
)

var cTTL = &DiceDBCommand{
Name: "TTL",
HelpShort: "TTL return the remaining time to live of a key that has an expiration set",
Eval: evalTTL,
}

func init() {
commandRegistry.AddCommand(cTTL)
}

func evalTTL(c *Cmd, s *dstore.Store) (*CmdRes, error) {
if len(c.C.Args) != 1 {
return cmdResNil, errWrongArgumentCount("TTL")
}

var key = c.C.Args[0]

obj := s.Get(key)

if obj == nil {
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: -2},
}}, nil
}

exp, isExpirySet := dstore.GetExpiry(obj, s)

if !isExpirySet {
return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: -1},
}}, nil
}

durationMs := exp - uint64(utils.GetCurrentTime().UnixMilli())

return &CmdRes{R: &wire.Response{
Value: &wire.Response_VInt{VInt: int64(durationMs / 1000)},
}}, nil
}
2 changes: 1 addition & 1 deletion internal/store/expire.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func EvaluateAndSetExpiry(subCommands []string, newExpiry int64, key string,
}
case LT:
ltCmd = true
if prevExpiry != nil && *prevExpiry < uint64(newExpInMilli) {
if prevExpiry == nil || *prevExpiry < uint64(newExpInMilli) {
shouldSetExpiry = false
}
default:
Expand Down

0 comments on commit e94e09f

Please sign in to comment.