Steps of setting key in redis

Time:2019-12-1

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:


redis 127.0.0.1:6379> SET KEY_NAME VALUE

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

Example:

Create a key in redis and set the value.

#Set keys that do not exist

 

redis 127.0.0.1:6379> SET key "value"

OK 

redis 127.0.0.1:6379> GET key

"value"

 

#Set the existing key

 

redis 127.0.0.1:6379> SET key "new-value"

OK

 

redis 127.0.0.1:6379> GET key

"new-value"

Redis key command is used to manage redis keys.

The basic syntax of redis key command is as follows:


redis 127.0.0.1:6379> COMMAND KEY_NAME

Example:


redis 127.0.0.1:6379> SET runoobkey redis

OK

redis 127.0.0.1:6379> 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.

13、RANDOMKEY

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.