Skip to main content

cmd_expire

EXPIRE - Overview

This file defines the EXPIRE command, which sets an expiration time (in seconds) on a specified key in the DiceDB store. The key will be automatically deleted after the expiry time.

Detailed Documentation

cEXPIRE Variable

var cEXPIRE = &CommandMeta{
Name: "EXPIRE",
Syntax: "EXPIRE key seconds [NX | XX]",
HelpShort: "EXPIRE sets an expiry (in seconds) on a specified key",
HelpLong: `
EXPIRE sets an expiry (in seconds) on a specified key. After the expiry time has elapsed, the key will be automatically deleted.

> If you want to delete the expirtation time on the key, you can use the PERSIST command.

The command returns 1 if the expiry was set, and 0 if the key already had an expiry set. The command supports the following options:

- NX: Set the expiration only if the key does not already have an expiration time.
- XX: Set the expiration only if the key already has an expiration time.
`,
Examples: `
locahost:7379> SET k1 v1
OK OK
locahost:7379> EXPIRE k1 10
OK 1
locahost:7379> SET k2 v2
OK OK
locahost:7379> EXPIRE k2 10 NX
OK 1
locahost:7379> EXPIRE k2 20 XX
OK 1
locahost:7379> EXPIRE k2 20 NX
OK 0
`,
Eval: evalEXPIRE,
Execute: executeEXPIRE,
}
  • Purpose: Defines the metadata for the EXPIRE command, including its name, syntax, help text, examples, and the functions to evaluate and execute the command.
  • Members:
    • Name: The name of the command ("EXPIRE").
    • Syntax: The command's syntax ("EXPIRE key seconds [NX | XX]").
    • HelpShort: A short description of the command.
    • HelpLong: A detailed description of the command, including options.
    • Examples: Examples of how to use the command.
    • Eval: The function to evaluate the command (evalEXPIRE).
    • Execute: The function to execute the command (executeEXPIRE).

init Function

func init() {
CommandRegistry.AddCommand(cEXPIRE)
}
  • Purpose: Registers the EXPIRE command with the CommandRegistry. This makes the command available for use.

evalEXPIRE Function

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

var key = c.C.Args[0]

exDurationSec, err := strconv.ParseInt(c.C.Args[1], 10, 64)
if err != nil || exDurationSec &lt; 0 {
return cmdResNil, errors.ErrInvalidExpireTime("EXPIRE")
}

obj := s.Get(key)
if obj == nil {
return cmdResInt0, nil
}

isExpirySet, err := dstore.EvaluateAndSetExpiry(c.C.Args[2:], utils.AddSecondsToUnixEpoch(exDurationSec), key, s)
if err != nil {
return cmdResNil, err
}

if isExpirySet {
return cmdResInt1, nil
}

return cmdResInt0, nil
}
  • Purpose: Evaluates the EXPIRE command. It parses the arguments, validates the expiry duration, and sets the expiry on the specified key in the store.
  • Parameters:
    • c: A pointer to the Cmd struct, containing the command's arguments.
    • s: A pointer to the dstore.Store struct, representing the data store.
  • Returns:
    • A pointer to a CmdRes struct, representing the result of the command.
    • An error, if any occurred during evaluation.
  • Logic:
    1. Validates that the correct number of arguments are provided.
    2. Parses the expiry duration from the arguments.
    3. Retrieves the object from the store using the provided key.
    4. If the object does not exist, returns 0.
    5. Calls dstore.EvaluateAndSetExpiry to handle the NX and XX options and set the expiry.
    6. Returns 1 if the expiry was set, 0 otherwise.

executeEXPIRE Function

func executeEXPIRE(c *Cmd, sm *shardmanager.ShardManager) (*CmdRes, error) {
if len(c.C.Args) <= 1 {
return cmdResNil, errors.ErrWrongArgumentCount("EXPIRE")
}
shard := sm.GetShardForKey(c.C.Args[0])
return evalEXPIRE(c, shard.Thread.Store())
}
  • Purpose: Executes the EXPIRE command. It retrieves the appropriate shard based on the key and then calls evalEXPIRE to perform the actual expiry setting.
  • Parameters:
    • c: A pointer to the Cmd struct, containing the command's arguments.
    • sm: A pointer to the shardmanager.ShardManager struct, used to manage shards.
  • Returns:
    • A pointer to a CmdRes struct, representing the result of the command.
    • An error, if any occurred during execution.
  • Logic:
    1. Validates that the correct number of arguments are provided.
    2. Gets the appropriate shard using the ShardManager.
    3. Calls evalEXPIRE with the store associated with the shard.

Code Examples

No specific code examples are needed as the code is self-explanatory.

Getting Started Relevance