Skip to main content

SET

Syntax

SET key value [NX | XX] [GET] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds]

Module

generic

Categories

slow write

Description

Set the value of a key, considering the value's type. If the key already exists, it is overwritten.

Options

  • NX - Only set if the key does not exist.
  • XX - Only set if the key exists.
  • GET - Return the old value stored at key, or nil if the value does not exist.
  • EX - Expire the key after the specified number of seconds (positive integer).
  • PX - Expire the key after the specified number of milliseconds (positive integer).
  • EXAT - Expire at the exact time in unix seconds (positive integer).
  • PXAT - Expire at the exat time in unix milliseconds (positive integer).

Examples

Set a value at a key:

vault, err := echovault.NewEchoVault()
if err != nil {
log.Fatal(err)
}
ok, err := vault.Set("name", "EchoVault", echovault.SetOptions{})

Set a value only if the key does not exist:

vault, err := echovault.NewEchoVault()
if err != nil {
log.Fatal(err)
}
ok, err := vault.Set("name", "EchoVault", echovault.SetOptions{NX: true})

Set a value if key already exists and get the previous value:

vault, err := echovault.NewEchoVault()
if err != nil {
log.Fatal(err)
}
previousValue, err := vault.Set("name", "EchoVault", echovault.SetOptions{XX: true, GET: true})

Set a value if the key already exists, return the previous value, and expire after 10 seconds:

vault, err := echovault.NewEchoVault()
if err != nil {
log.Fatal(err)
}
previousValue, err := vault.Set("name", "EchoVault", echovault.SetOptions{XX: true, GET: true, EX: 10})