Redis-cli command Summary

Source: Internet
Author: User
Redis provides a variety of commands to operate databases and data types. These commands can be used on Linux terminals. Redis-cli command Summary 1. connection operation-related command quit: close connection auth: Simple Password Authentication 2. value operation command exists (key ): check whether a key exists.

Redis provides a variety of commands to operate databases and data types. These commands can be used on Linux terminals. Redis-cli command Summary 1. connection operation-related command quit: close connection auth: Simple Password Authentication 2. value operation command exists (key ): check whether a key exists.

Redis provides a variety of commands to operate databases and data types. These commands can be used on Linux terminals.

Redis-cli command Summary

1. Connection operation-related commands

Quit: disconnect)

Auth: Simple Password Authentication

2. commands for value operations

Exists (key): Check whether a key exists.

Del (key): deletes a key.

Type (key): type of the returned value

Keys (pattern): returns all keys that meet the given pattern.

Randomkey: returns a random key of the key space.

Rename (oldname, newname): rename the key from oldname to newname. If newname exists, delete the key represented by newname.

Dbsize: returns the number of keys in the current database.

Expire: Set the activity time of a key (s)

Ttl: obtains the activity time of a key.

Select (index): query by index

Move (key, dbindex): transfers the key in the current database to a database with a dbindex index.

Flushdb: delete all keys in the selected database.

Flushall: delete all keys in all databases

3. String operation commands

Set (key, value): assign value to the string named key in the database

Get (key): returns the value of a string named key in the database.

Getset (key, value): Assign the last value to the string named key.

Mget (key1, key2 ,..., Key N): returns multiple strings in the database (their names are key1, key2 ...) Value

Setnx (key, value): If there is no string named key, add a string to the database, with the name key and value

Setex (key, time, value): to add a string (Name: key, value: value) to the database, and set the expiration time

Mset (key1, value1, key2, value2 ,... Key N, value N): assign values to multiple strings at the same time.

Msetnx (key1, value1, key2, value2 ,... Key N, value N): If none of the strings whose names are key I exist, add a string to the database,

The value of name key I is value I.

Incr (key): Add 1 to a string named key

Incrby (key, integer): Add integer to the string named key

Decr (key): string minus 1 operation whose name is key

Decrby (key, integer): reduce integer in the string with the name of key

Append (key, value): append the value of string with the name of key

Substr (key, start, end): returns the substring of the value of the string named key.

4. List Operation commands

Rpush (key, value): adds an element with the value at the end of the list named key.

Lpush (key, value): adds an element with value to the list header named key.

Llen (key): returns the length of the list named key.

Lrange (key, start, end): returns the element between start and end in the list named key (subscript starts from 0, the same below)

Ltrim (key, start, end): truncates the list named key and retains the elements between start and end.

Lindex (key, index): returns the index position element of the list named key.

Lset (key, index, value): assign a value to the element at the index position in the list named key

Lrem (key, count, value): deletes the count elements with the value in the list named key.

If the value of count is 0, all elements with the value are deleted. If the value of count is greater than 0, all elements with the value are deleted from the beginning to the end, count <0 from the end to the header | count | elements with a value.

Lpop (key): returns and deletes the first element rpop (key) in the list named key: returns and deletes the end element in the list named key.

Blpop (key1, key2 ,... Key N, timeout): The block version of The lpop command.

That is, when the timeout value is 0, the command ends if the list named key I does not exist or the list is empty.

If the timeout value is greater than 0, wait for timeout seconds in the case of the preceding situation. If the problem persists, perform the pop operation on the list starting with keyi + 1.

Brpop (key1, key2 ,... Key N, timeout): rpop block version. Refer to the previous command.

Rpoplpush (srckey, dstkey): returns and deletes the end element of the list named srckey, and adds this element to the header of the list named dstkey.

5. Set operation commands

Sadd (key, member): add the element member to the set named key.

Srem (key, member): deletes the element member from the set named key.

Spop (key): Randomly returns and deletes an element in a set named key.

Smove (srckey, dstkey, member): Move the member element from the set named srckey to the set named dstkey.

Scard (key): returns the base number of a set named key.

Sismember (key, member): Tests whether member is an element of a set named key.

Sinter (key1, key2 ,... Key N): calculates the intersection.

Sinterstore (dstkey, key1, key2 ,... Key N): calculates and saves the intersection to the dstkey set.

Sunion (key1, key2 ,... Key N): returns the union.

Sunionstore (dstkey, key1, key2 ,... Key N): calculates the Union set and saves the Union set to the dstkey set.

Sdiff (key1, key2 ,... Key N): returns the difference set.

Sdiffstore (dstkey, key1, key2 ,... Key N): calculates the difference set and saves the difference set to the dstkey set.

Smembers (key): returns all elements of a set named key.

Srandmember (key): Random return of an element of the set named key

6. commands for zset (sorted set) Operations

Zadd (key, score, member): add the element member to the zset named key. score is used for sorting. If the element already exists, the sequence of the element is updated based on the score.

Zrem (key, member): deletes the element member in the zset named key.

Zincrby (key, increment, member): If the element member already exists in the zset named key, the score of this element is added with increment;

Otherwise, add this element to the set. The score value is increment.

Zrank (key, member): returns the rank (index, starting from 0) of the member element in the zset (the element has been sorted by score in ascending order) with the key name ),

If no member element exists, "nil" is returned"

Zrevrank (key, member): return the rank (index, starting from 0) of the member element in the zset (the element has been sorted by score in descending order) with the key name ),

If no member element exists, "nil" is returned"

Zrange (key, start, end): returns all the elements of index from start to end in the zset with the name of key (the elements have been sorted by score in ascending order ).

Zrevrange (key, start, end): returns all the elements of index from start to end in the zset with the name of key (the elements have been sorted by score in ascending order ).

Zrangebyscore (key, min, max): returns all the elements of the zset whose names are key, score> = min and score <= max.

Zcard (key): returns the zset base zscore (key, element) of the zset whose name is key.

Score zremrangebyrank (key, min, max): delete all elements whose rank is> = min and rank is <= max in a zset named key.

Zremrangebyscore (key, min, max): delete all the elements of a zset named key with score> = min and score <= max

Zunionstore/zinterstore (dstkeyN, key1 ,..., KeyN, WEIGHTS w1 ,... WN, aggregate sum | MIN | MAX): returns the Union and intersection of N zsets,

And save the final set in dstkeyN. For the score of each element in the Set, the score must be multiplied by the WEIGHT parameter before the AGGREGATE operation.

If WEIGHT is not provided, the default value is 1. The default AGGREGATE value is SUM, that is, the score of the element in the result set is the SUM calculation value for all the corresponding elements in the Set, while MIN and MAX refer,

The score of an element in the result set is the minimum and maximum values of all elements in the set.

7. commands for Hash operations

Hset (key, field, value): Add an element field to the hash named key <-> value

Hget (key, field): returns the value of the field in the hash with the key name.

Hmet (key, field1 ,..., Field N): return the value corresponding to field I in the hash with the key name.

Hmset (key, field1, value1 ,..., Field N, value N): Add field I to the hash with the key name <-> value I

Hincrby (key, field, integer): adds an integer to the field value in the hash named key.

Hexists (key, field): indicates whether the hash with the key as the field exists.

Hdel (key, field): Delete the field in the hash key named key.

Hlen (key): returns the number of elements in the hash key.

Hkeys (key): returns all keys in the hash key

Hvals (key): return the value corresponding to all keys in the hash with the key name.

Hgetall (key): returns all the keys (fields) and their corresponding values in the hash key.

8. Persistence

Save: synchronize and save data to the disk.

Bgsave: asynchronously saves data to the disk.

Lastsave: returns the Unix timestamp from which data is successfully saved to the disk.

Shundown: synchronize and save the data to the disk, and then close the service.

9. Remote Service Control

Info: provides server information and statistics.

Monitor: Real-time dump of received requests

Slaveof: changes the replication policy settings

Config: configure the Redis server at runtime

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.

List of related commands:
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.
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.