Skip to content

Redis String API

Redis String commands for storing, reading and atomically updating single values.

1 class · 5 methods

String Commands

5 methods

Commands operating on Redis string values.

SET key value [EX seconds | PX ms] [NX | XX] [KEEPTTL]

Sets key to value with optional expiry and existence condition flags.

Parameters

NameTypeDescription
keystringKey to set.
valuestringValue to store.
EXintegerExpire in seconds.
NXflagOnly set if key does not exist.
XXflagOnly set if key already exists.

Returns

OK | nil

Example

redis
SET session:42 abc123 EX 3600 NX
GET key

Returns the string value stored at key, or nil if the key does not exist.

Parameters

NameTypeDescription
keystringKey to read.

Returns

string | nil

Example

redis
GET session:42
INCR key | INCRBY key delta

Atomically increments the integer value stored at key by one or by the given delta.

Parameters

NameTypeDescription
keystringKey holding an integer string.
deltaintegerIncrement for INCRBY; defaults to 1 for INCR.

Returns

integer

Example

redis
INCR counter:pageviews
APPEND key value

Appends value to the string at key, creating it if it does not exist. Returns the new length.

Parameters

NameTypeDescription
keystringKey to append to.
valuestringValue to append.

Returns

integer

Example

redis
APPEND log:today "newline\n"
EXPIRE key seconds [NX | XX | GT | LT]

Sets a time-to-live in seconds on a key, with optional condition flags.

Parameters

NameTypeDescription
keystringKey to expire.
secondsintegerTTL in seconds.

Returns

0 | 1

Example

redis
EXPIRE session:42 3600