SET

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Store

SET stores data. Usage is set key value.
GET retrieves stored data.

Example

Command>set key value
Result>OK
Command>get key
Result>value

Using double quotes

To include spaces in the value, use double quotes.

Example

Command>set key "Hello Redis!"
Result>OK
Command>get key
Result>Hello Redis!

Overwrite

If you run SET on the same key, the previous value is replaced with the new value.

Example

Command>set key Hello
Result>OK
Command>set key GoodBye
Result>OK
Command>get key
Result>GoodBye

View animation

Note To avoid overwriting, use the NX option.   The NX, XX, EX, and PX options were added in version 2.6.12.

Prevent overwrite: NX

With the NX option, the value is stored only if the key does not already exist in the database.
NX is often used for lock handling, because a single command can both check whether data exists and store it.

Example

Command>set key1 Hello NX
Result>OK
Command>set key1 Redis NX
Result>(nil)
Command>get key1
Result>Hello

View animation

The NX option is case-insensitive.
The first command stores the data because key1 does not exist in the database.
The second command returns (nil) because key1 was already stored by the first command.
This option behaves the same as the SETNX command.

Store only if the key already exists: XX

The XX option stores the value only if the specified key already exists in the database.

Example

Command>set key2 Hello XX
Result>(nil)
Command>set key2 Hello
Result>OK
Command>set key2 Redis XX
Result>OK
Command>get key2
Result>Redis

View animation

All Redis options are case-insensitive.
The first command with XX does not store data because key2 does not exist in the database.
The second command stores the value because XX is not used.
The third command stores the value because key2 already exists in the database.

Delete data after a given time: EX <seconds>

The EX seconds option deletes the data after the specified number of seconds.
This is useful when storing data that has an expiration time.

Example

Command>set key3 Hello EX 5
Result>OK
Command>get key3   after 1 second
Result>Hello
Command>get key3   after 5 seconds
Result>(nil)

View animation

This option behaves the same as the SETEX command.

Delete data after a given time: PX <milliseconds>

The PX milliseconds option deletes the data after the specified number of milliseconds.
Use it when you need a more precise expiration time.

Example

Command>set key3 Hello PX 5000
Result>OK
Command>get key3   after 1 second
Result>Hello
Command>get key3   after 5 seconds
Result>(nil)
This option behaves the same as the PSETEX command.

Delete data after a given timestamp (seconds): EXAT <timestamp>

The EXAT timestamp (seconds) option deletes the data after the specified time.
This is useful when storing data that has an expiration time.
Added in Redis 6.2.

Delete data after a given milliseconds-timestamp: PXAT <milliseconds-timestamp>

The PXAT milliseconds-timestamp option deletes the data after the specified time.
This is useful when storing data that has an expiration time.
Added in Redis 6.2.

Keep expire time: KEEPTTL

By default, SET clears the expire time. With the KEEPTTL option, the expire time is not cleared. This option was added for special cases such as the following.
When INCRBYFLOAT runs, it is rewritten as SET in AOF, and any previously set expire time would be cleared. KEEPTTL is used so that the expire time is not cleared in that case.

Example

Command>set key4 10 ex 100   AOF: SET key4 10, PEXPIREAT key4 1579954223443
Result>OK
Command>incrbyfloat key4 10   AOF: SET key 20 KEEPTTL
Result>20
Command>SHUTDOWN -> RESTART
Command>ttl key4     expire time is kept even after restart.
Result>25
In 6.0-r1, INCRBYFLOAT is rewritten as SET KEEPTTL, but the SET handling in aof.c feedAppendOnlyFile() that actually writes the command was not updated, so KEEPTTL is not written to AOF. SET key value KEEPTTL is also recorded in AOF as SET key value only.
This was fixed in 6.0.5 (released June 9, 2020).

Set a new value and return the old value: GET

This option was added in 6.2.
Prefer this option instead of the older GETSET command.

Example

Command>set key1 value1
Result>OK
Command>set key1 value2 get
Result>value1
Command>get key1
Result>value2

Syntax

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

  • Available since version 1.0.0.
  • The NX, XX, EX, and PX options were added in version 2.6.12.
  • The KEEPTTL option was added in version 6.0.
  • The EXAT, PXAT, and GET options were added in version 6.2.
  • Time complexity is O(1).
SQL Insert If you are using Redis Enterprise 7.3.0 or later, try INSERT.
Related commands SETNX, SETEX, PSETEX, TTL, PTTL, MSET, APPEND
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

<< STRINGS SET SETS >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.