Redis study note 7 (key operation)

Source: Internet
Author: User
Redis study note 7 (key operation) I. Overview: In the first several blogs of this series, commands related to Redis data types are mainly described, such as String, List, Set, Hashes, and Sorted-Set. These commands have one thing in common, that is, all operations are for the Value associated with the Key. This blog will focus on Key-related R

Redis study note 7 (key operation) I. Overview: In the first several blogs of this series, commands related to Redis data types are mainly described, such as String, List, Set, Hashes, and Sorted-Set. These commands have one thing in common, that is, all operations are for the Value associated with the Key. This blog will focus on Key-related R

Redis study note 7 (key operation)

I. Overview:

In the previous blogs of this series, we mainly talked about commands related to Redis data types, such as String, List, Set, Hashes, and Sorted-Set. These commands have one thing in common, that is, all operations are for the Value associated with the Key. This blog will focus on Key-related Redis commands. Learning these commands is a very important foundation for learning Redis and a powerful tool to fully tap the potential of Redis.

In this blog, we will continue to provide a detailed list and typical examples of all relevant commands, so that we can learn and view them in the future.

Ii. Related command list:

Command prototype time complexity command description Return Value

In KEYS pattern O (N) time complexity, N represents the number of KEYS in the database. Obtain all the Keys that match the pattern parameter. It should be noted that we should try to avoid calling this command during normal operations, because this command is very time-consuming for large databases, the performance of the Redis server is also a big blow. Pattern supports the wildcard format of glob-style. For example, * indicates any one or more characters ,? Represents any character, and [abc] represents any letter in square brackets. The list of keys in the matching mode.

In DEL key [key...] O (N) time complexity, N indicates the number of deleted keys. If the specified key does not exist, the specified keys are ignored. It should also be noted that, if the data type associated with the specified Key is not String type, but the container type such as List, Set, Hashes, and Sorted Set, the time complexity of deleting each key is O (M). M indicates the number of elements in the container. For keys of the String type, the time complexity is O (1 ). The number of actually deleted keys.

EXISTS key O (1) determines whether the specified key EXISTS. 1 indicates yes, and 0 indicates no.

MOVE key db O (1) MOVE the Key specified in the current database to the database specified in the parameter. If the Key already exists in the target database or does not exist in the current database, the command will not perform any operation and return 0. 1 is returned if the image is moved successfully. Otherwise, 0 is returned.

RENAME key newkey O (1) is renamed for the specified Key. If the two Keys commands in the parameter are the same or the source key does not exist, this command will return related error information. If newKey already exists, it will be overwritten directly.

RENAMENX key newkey O (1) if the new value does not exist, change the original value in the parameter to a new value. Other conditions are the same as RENAME. 1 indicates that the modification is successful, otherwise 0.

PERSIST key O (1) if the Key has an expiration time, the command will remove the expiration time so that the Key no longer times out, but can be stored persistently. 1 indicates that the Key expiration time is removed, and 0 indicates that the Key does not exist or there is no expiration time.

EXPIRE key seconds O (1) this command sets the time-out seconds for the Key specified in the parameter. After this time expires, the Key is automatically deleted. If the Key is modified before the timeout, the timeout associated with the Key is removed. 1 indicates that the timeout is set, 0 indicates that the Key does not exist, or cannot be set.

EXPIREAT key timestamp O (1) the logic function of this command is exactly the same as that of EXPIRE. The only difference is that the time-out period specified by this command is absolute time rather than relative time. This time parameter is in Unix timestamp format, that is, the number of seconds that flow from January 1, January 1, 1970. 1 indicates that the timeout is set, 0 indicates that the Key does not exist, or cannot be set.

TTL key O (1) gets the time-out description of the key. Returns the remaining description. If the key does not exist or there is no timeout setting,-1 is returned.

Randomkey o (1) returns a random Key from the currently opened database. The random key returned. If the database is null, nil is returned.

TYPE key O (1) gets the TYPE of the value associated with the specified key in the parameter. This command will be returned in string format. The returned string is string, list, set, hash, and zset. If the key does not exist, none is returned.

SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern...] [ASC | DESC] [ALPHA] [STORE destination] O (N + M * log (M) is relatively complicated, therefore, we only provide the most basic usage here. If you are interested, you can refer to the official redis documentation. Returns the sorted original list.

Iii. Command example:

1. KEYS/RENAME/DEL/EXISTS/MOVE/RENAMENX:

# Start the Redis client tool under the Shell command line.

/> Redis-cli

# Clear the selected database to facilitate understanding of the following example.

Redis 127.0.0.1: 6379> flushdb

OK

# Add analog data of the String type.

Redis 127.0.0.1: 6379> set mykey 2

OK

Redis 127.0.0.1: 6379> set mykey2 "hello"

OK

# Add Set-type analog data.

Redis 127.0.0.1: 6379> sadd mysetkey 1 2 3

(Integer) 3

# Add Hash-type simulated data.

Redis 127.0.0.1: 6379> hset mmtest username "stephen"

(Integer) 1

# Obtain all keys in the current database that match the mode according to the mode in the parameter. The output shows that the command does not distinguish the Value type associated with the key during execution.

Redis 127.0.0.1: 6379> keys my *

1) "mysetkey"

2) "mykey"

3) "mykey2"

# Two Keys are deleted.

Redis 127.0.0.1: 6379> del mykey mykey2

(Integer) 2

# Check whether the deleted Key still exists. From the returned results, the mykey is deleted.

Redis 127.0.0.1: 6379> exists mykey

(Integer) 0

# Check the Key that has not been deleted and compare it with the preceding command results.

Redis 127.0.0.1: 6379> exists mysetkey

(Integer) 1

# Move the mysetkey in the current database to the database with ID 1. The result shows that the database has been moved successfully.

Redis 127.0.0.1: 6379> move mysetkey 1

(Integer) 1

# Open the database with ID 1.

Redis 127.0.0.1: 6379> select 1

OK

# Check whether the Key you just moved exists. The returned result shows that the Key already exists.

Redis 127.0.0.1: 6379 [1]> exists mysetkey

(Integer) 1

# Re-open the default database with ID 0.

Redis 127.0.0.1: 6379 [1]> select 0

OK

# Check whether the removed Key does not exist. Check whether the Key has been removed from the returned result.

Redis 127.0.0.1: 6379> exists mysetkey

(Integer) 0

# Prepare new test data.

Redis 127.0.0.1: 6379> set mykey "hello"

OK

# Rename mykey to mykey1

Redis 127.0.0.1: 6379> rename mykey mykey1

OK

# Because the mykey has been renamed, nil will be returned if the key is obtained again.

Redis 127.0.0.1: 6379> get mykey

(Nil)

# Obtain it using the new key name.

Redis 127.0.0.1: 6379> get mykey1

"Hello"

# The error message is returned because the mykey does not exist.

Redis 127.0.0.1: 6379> rename mykey mykey1

(Error) ERR no such key

# Prepare a test key for renamenx

Redis 127.0.0.1: 6379> set oldkey "hello"

OK

Redis 127.0.0.1: 6379> set newkey "world"

OK

# Because newkey already exists, this command cannot be successfully executed.

Redis 127.0.0.1: 6379> renamenx oldkey newkey

(Integer) 0

# Check the value of newkey and find that it is not overwritten by renamenx.

Redis 127.0.0.1: 6379> get newkey

"World"

2. PERSIST/EXPIRE/EXPIREAT/TTL:

# Test data prepared for the following example.

Redis 127.0.0.1: 6379> set mykey "hello"

OK

# Set the timeout value of the key to 100 seconds.

Redis 127.0.0.1: 6379> expire mykey 100

(Integer) 1

# Use the ttl command to check the remaining seconds.

Redis 127.0.0.1: 6379> ttl mykey

(Integer) 97

# Execute the persist command immediately, and the expired Key becomes a persistent Key, removing the timeout value of the Key.

Redis 127.0.0.1: 6379> persist mykey

(Integer) 1

# The ttl return value indicates that the key has not timed out.

Redis 127.0.0.1: 6379> ttl mykey

(Integer)-1

# Prepare data for the following expire command.

Redis 127.0.0.1: 6379> del mykey

(Integer) 1

Redis 127.0.0.1: 6379> set mykey "hello"

OK

# Set the timeout value for this key to 100 seconds.

Redis 127.0.0.1: 6379> expire mykey 100

(Integer) 1

# Use the ttl command to check the remaining seconds. The result shows that there are still 96 seconds left.

Redis 127.0.0.1: 6379> ttl mykey

(Integer) 96

# The timeout time for re-updating the key is 20 seconds. The returned value shows that the command is successfully executed.

Redis 127.0.0.1: 6379> expire mykey 20

(Integer) 1

# Use ttl to confirm the update.

Redis 127.0.0.1: 6379> ttl mykey

(Integer) 17

# Update the value of the key immediately to make the timeout invalid.

Redis 127.0.0.1: 6379> set mykey "world"

OK

# From the ttl result, we can see that after the last command to modify the key is executed, the time-out of the key is also invalid.

Redis 127.0.0.1: 6379> ttl mykey

(Integer)-1

3. TYPE/RANDOMKEY/SORT:

# Because the mm key does not exist in the database, this command returns none.

Redis 127.0.0.1: 6379> type mm

None

# If the value of mykey is of the string type, a string is returned.

Redis 127.0.0.1: 6379> type mykey

String

# Prepare a set key.

Redis 127.0.0.1: 6379> sadd mysetkey 1 2

(Integer) 2

# The Key of mysetkey is set, so the string set is returned.

Redis 127.0.0.1: 6379> type mysetkey

Set

# Return any key in the database.

Redis 127.0.0.1: 6379> randomkey

"Oldkey"

# Clear the currently opened database.

Redis 127.0.0.1: 6379> flushdb

OK

# Because no data exists, nil is returned.

Redis 127.0.0.1: 6379> randomkey

(Nil)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.