Steps of setting key in redis


The redis set command is used to set the value of the given key. If the key already stores other values, set overwrites the old values and ignores the type.

The basic syntax of the redis set command is as follows:


Return value:

Before redis 2.6.12, the set command always returned OK.

From redis2.6.12, set returns OK when the setting operation is completed successfully


Create a key in redis and set the value.

#Set keys that do not exist


redis> SET key "value"


redis> GET key



#Set the existing key


redis> SET key "new-value"



redis> GET key


Redis key command is used to manage redis keys.

The basic syntax of redis key command is as follows:



redis> SET runoobkey redis


redis> DEL runoobkey(integer) 1

In the above example, del is a command and runoobkey is a key. If the key is deleted successfully, output (integer) 1 after command execution, otherwise output (integer) 0

Basic commands related to redis key:

1、DEL key

This command is used to delete the key when it exists.

2 、DUMP key

Serializes the given key and returns the serialized value.

3、EXISTS key

Check whether the given key exists.

4、EXPIRE key seconds

Sets the expiration time in seconds for the given key.

5、EXPIREAT key timestamp

The function of expire is similar to that of expire, which is used to set the expiration time for the key. The difference is that the time parameter accepted by the expire command is UNIX timestamp.

6 、PEXPIRE key milliseconds

Set the expiration time of the key in milliseconds.

7、PEXPIREAT key milliseconds-timestamp

Set the UNIX timestamp of the key expiration time in milliseconds

8、KEYS pattern

Find all keys that match the given pattern.

9、MOVE key db

Move the key of the current database to the given database db.

10、PERSIST key

Remove the expiration time of the key, and the key will be persisted.

11、PTTL key

Returns the remaining expiration time of the key in milliseconds.

12、TTL key

Returns the TTL (time to live) of a given key in seconds.


Returns a key randomly from the current database.

14、RENAME key newkey

Modify the name of key

15、RENAMENX key newkey

Only if the newkey does not exist, change the name of the key to newkey.

16、TYPE key

Returns the type of value the key stores.