Skip to content

Commit

Permalink
Merge pull request DiceDB#1474 from DiceDB/arpit-readiness-cli-chat
Browse files Browse the repository at this point in the history
Refactoring the SetExpiry code to match the documentation
  • Loading branch information
arpitbbhayani authored Feb 10, 2025
2 parents e94e09f + 2462216 commit 16ecd4c
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions internal/store/expire.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ func EvaluateAndSetExpiry(subCommands []string, newExpiry int64, key string,
if obj == nil {
return false, nil
}
shouldSetExpiry = true
// if no condition exists
shouldSetExpiry = false

// If no sub-command is provided, set the expiry
if len(subCommands) == 0 {
store.SetUnixTimeExpiry(obj, newExpiry)
return shouldSetExpiry, nil
return true, nil
}

// Get the previous expiry time
expireTime, ok := GetExpiry(obj, store)
if ok {
prevExpiry = &expireTime
Expand All @@ -111,26 +113,34 @@ func EvaluateAndSetExpiry(subCommands []string, newExpiry int64, key string,
switch subCommand {
case NX:
nxCmd = true
if prevExpiry != nil {
shouldSetExpiry = false

// Set the expiration only if the key does not already have an expiration time.
if prevExpiry == nil {
shouldSetExpiry = true
}
case XX:
xxCmd = true
if prevExpiry == nil {
shouldSetExpiry = false

// Set the expiration only if the key already has an expiration time.
if prevExpiry != nil {
shouldSetExpiry = true
}
case GT:
gtCmd = true
if prevExpiry == nil || *prevExpiry > uint64(newExpInMilli) {
shouldSetExpiry = false

// Set the expiration only if the new expiration time is greater than the current one.
if prevExpiry != nil && uint64(newExpInMilli) > *prevExpiry {
shouldSetExpiry = true
}
case LT:
ltCmd = true
if prevExpiry == nil || *prevExpiry < uint64(newExpInMilli) {
shouldSetExpiry = false

// Set the expiration only if the new expiration time is less than the current one.
if prevExpiry != nil && uint64(newExpInMilli) < *prevExpiry {
shouldSetExpiry = true
}
default:
return false, diceerrors.ErrGeneral("Unsupported option " + subCommands[i])
return false, diceerrors.ErrGeneral("unsupported option " + subCommands[i])
}
}

Expand Down

0 comments on commit 16ecd4c

Please sign in to comment.