Redis learning Manual (key operation command)

Source: Internet
Author: User
Tags redis server

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
KeysPattern O (N) N in time complexity indicates 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.
DelKey [Key...] O (N) N in time complexity 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.
ExistsKey O (1) Determines whether the specified key exists. 1 indicates yes, and 0 indicates no.
MoveKey DB O (1)
Move the specified key 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.
RenameKey newkey O (1) Rename the specified key. If the two keys in the parameter have the same command or the source key does not exist, the command returns the relevant error message. If newkey already exists, it will be overwritten directly.  
RenamenxKey newkey O (1) If the new value does not exist, the original value in the parameter is changed to a new value. Other conditions are the same as rename. 1 indicates that the modification is successful, otherwise 0.
PersistKey O (1)
If the key has an expiration time, this command will eliminate 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.
ExpireKey seconds O (1) This command sets the time-out seconds for the key specified in the parameter. After this time is exceeded, 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.
ExpireatKey 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. 
TTLKey O (1) Gets the description of the time-out remaining for 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.
TypeKey O (1) Gets the type of the key associated value specified in the parameter. This command is returned in string format. The returned string is string, list, set, hash, and zset. If the key does not exist, none is returned.
SortKey [by pattern] [limit offset count] [get pattern [get pattern...] [ASC | DESC] [Alpha] [store destination] O (N + M * log (m )) This command is relatively complex, so we only provide the most basic usage here. Interested users 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)

Related Article

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.