Skip to main content

Description

Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.

Syntax

Parameters

key
string
required
The key to set
value
TData
required
The value to store. Can be a string, number, object, or any serializable data
opts
SetCommandOptions
Optional configuration for the SET command

Type Parameters

TData
type
The type of the value being stored

Returns

result
TData | 'OK' | null
  • Returns "OK" if SET was executed correctly
  • Returns null if the SET operation was not performed (due to nx or xx conditions)
  • Returns the old value if the get option was specified

Examples

Basic Usage

Set with Expiration

Conditional Set

Set and Get Old Value

Working with Objects

Keep TTL

Notes

  • The ex, px, exat, pxat, and keepTtl options are mutually exclusive
  • The nx and xx options are mutually exclusive
  • When using the get option, the command returns the old value instead of "OK"
  • get - Get the value of a key
  • mset - Set multiple keys to multiple values

Redis Documentation

For more information, see the Redis SET documentation.