Get the App
SLTechnology News&Howtos  ›  Database  › 

Setting method and steps of key in redis

Shulou Source: shulou.com Published: 2022-06-01 04:24:48 09月30日 Update

The Redis SET command sets the value of a given key. If key already stores other values, SET overwrites the old values, regardless of type.

The basic syntax for redis SET commands is as follows:

Redis 127.0.0.1 6379 > SET KEY_NAME VALUE

Return value:

In previous versions of Redis2.6.12, the SET command always returned OK.

Starting from the Redis2.6.12 version, SET returns OK only when the setting operation is completed successfully.

Example:

Create a key in redis and set the value.

# set redis 127.0.0.1 for keys that do not exist > SET key "value" OK redis 127.0.0.1 for OK redis 6379 > GET key "value" # for keys that do not exist, set redis 127.0.0.1 for OK redis 6379 > SET key "new-value" OK redis 127.0.0.1 for GET key "new-value"

The Redis key command is used to manage the keys of redis.

The basic syntax of the Redis command is as follows:

Redis 127.0.0.1 6379 > COMMAND KEY_NAME

Example:

Redis 127.0.0.1 SET runoobkey redisOKredis 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 the command is executed, otherwise (integer) 0

Basic commands related to the Redis key:

1 、 DEL key

This command is used to delete the key when the key 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 a given key.

5 、 EXPIREAT key timestamp

EXPIREAT is similar to EXPIRE in that it is used to set the expiration time for key. The difference is that the time parameter accepted by the EXPIREAT command is the UNIX timestamp (unix timestamp).

6 、 PEXPIRE key milliseconds

Sets the expiration time of key in milliseconds.

7 、 PEXPIREAT key milliseconds-timestamp

Sets the timestamp (unix timestamp) of key expiration time in milliseconds

8 、 KEYS pattern

Find all key that match the given pattern (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 key, and key will persist.

11 、 PTTL key

Returns the remaining expiration time of the key in milliseconds.

12 、 TTL key

Returns the remaining time to live (TTL, time to live) for a given key in seconds.

13 、 RANDOMKEY

A key is randomly returned from the current database.

14 、 RENAME key newkey

Modify the name of key

15 、 RENAMENX key newkey

Rename key to newkey only if newkey does not exist.

16 、 TYPE key

Returns the type of value stored by key.

Tags: Command time instance data database success unit sequence version type syntax remainder output different function parameter name schema creation storage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft Linux Apple MySQL