How to set string value at key in redis – Redis SET | SETNX | SETEX | PSETEX

1 month ago Lalit Bhagtani 0

In this tutorial, we will learn about how to set a string value at the specified key in redis datastore, by using redis SET, SETNX, SETEX, and PSETEX commands.

SET Command

This command is used to set a string value to a specified key. If key already exist, then it’s value will be overwritten irrespective of its type and any previous expiration time associated with the key will also be removed.

Optional Arguments

The Redis SET command takes following optional arguments :-

  1. EX seconds :- It sets expiration time on key in seconds.
  2. PX milliseconds :- It sets expiration time on key in milliseconds.
  3. NX :- It sets the string value, only if key does not exist.
  4. XX :- It sets the string value, only if key already exist.

The syntax of redis SET command is as follows :-

Syntax :-

redis host:post> SET <keyname> <value>

Output :-

- OK, if operation is successful and string value is set.
- Null, if operation is failed and no string value is set.

Example :-

Redis SET

SETNX Command

The SETNX command is used to set a string value at a specified key. If the key already exist, then operation will be failed and 0 will be returned otherwise string value is stored at the key and 1 will be returned. The syntax of redis SETNX command is as follows :-

Syntax :-

redis host:post> SETNX <keyname> <value>

Output :-

- 1, if operation is successful and string value is set.
- 0, if operation is failed and no string value is set.

It is equivalent to SET command with NX argument.

Example :-

Redis SETNX

SETEX Command

The SETEX command is used to set a string value at the specified key and sets an expiration time on that key in seconds. This number of seconds represents the time to live. If number of seconds are zero or negative, key will be deleted immediately. SETEX command is atomic by nature and is equivalent to executing the following commands :-

SET key value
Expire key seconds

The syntax of redis SETEX command is as follows :-

Syntax :-

redis host:post> SETEX <keyname> <seconds> <value>

Output :-

- OK, if operation is successful and string value is set.
- Null, if operation is failed and no string value is set.

It is equivalent to SET command with EX argument.

Example :-

Redis SETEX

PSETEX Command

This command is very similar to SETEX command with the difference is that, in PSETEX command expiration time is specified in milliseconds instead of seconds. The syntax of redis PSETEX command is as follows :-

Syntax :-

redis host:post> PSETEX <keyname> <milliseconds> <value>

Output :-

- OK, if operation is successful and string value is set.
- Null, if operation is failed and no string value is set.

It is equivalent to SET command with PX argument.

Example :-

Redis PSETEX

References :-

  1. SET Command Docs
  2. SETNX Command Docs
  3. SETEX Command Docs
  4. PSETEX Command Docs

That’s all for how to set a string value at a specified key in redis datastore. If you liked it, please share your thoughts in comments section and share it with others too.

<- String Commands