First, overview:
In the first few blogs of the series, the main focus is on commands related to Redis data types, such as String, List, Set, hashes, and Sorted-set. These commands all have one thing in common, that is, all operations are for the value associated with the key. This blog will focus on the Redis commands associated with key. Learning these commands is a very important foundation for learning Redis and is a powerful tool to fully tap the potential of redis.
In this blog, we will, as always, give a detailed list of all relevant commands and a typical example to facilitate our current learning and future review.
Ii. List of related commands:
Command prototypes |
Complexity of Time |
Command description |
return value |
KEYS pattern |
O (N) |
N In time complexity indicates the number of keys in the database. Gets all keys that match the pattern parameter. It should be explained that the call to the command should be avoided as much as possible in our normal operation because the command is time consuming for large databases, and the performance hit on the Redis server is relatively large. Pattern supports Glob-style wildcard format, such as * denotes any one or more characters,? denotes any character, [ABC] denotes any letter in square brackets |
A list of keys that match the pattern. |
DEL key [Key ...] |
O (N) |
N in time complexity means the number of keys deleted. Deletes the keys specified in the parameters from the database and ignores them directly if the specified key does not exist. Another point to note is that if the specified key is associated with a data type other than string type, but a container type such as list, set, hashes, and sorted set, the command deletes each key with a time complexity of O (M), where M represents the number of elements in the container. For a string type key, the time complexity is O (1). |
The number of keys that were actually deleted. |
EXISTS Key |
O (1) |
Determines whether the specified key exists. |
1 means presence, 0 means no. |
MOVE Key db |
O (1) |
Moves the key 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 does nothing and returns 0. |
Move successfully returns 1, otherwise 0. |
RENAME Key Newkey |
O (1) |
Renames the specified key, and returns the associated error message if the command for the two keys in the parameter is the same, or if the source key does not exist. If the newkey already exists, it is overwritten directly. |
|
Renamenx Key Newkey |
O (1) |
If the new value does not exist, the original value in the parameter is modified to the new value. Other conditions are consistent with rename. |
1 indicates that the modification succeeded, otherwise 0. |
PERSIST Key |
O (1) |
If the key has an expiration time, the command removes its expiration time so that the key no longer has a timeout, but can persist the store. |
1 means that the expiration time of key is removed and 0 indicates that the key does not exist or has no expiration time |
EXPIRE Key seconds |
O (1) |
The command sets the number of seconds to timeout for the key specified in the parameter, and after that time, the key is automatically deleted. If the key is modified before the time-out, the timeout associated with the key is removed. |
1 indicates that the timeout is set, and 0 indicates that the key does not exist or cannot be set. |
Expireat key Timestamp |
O (1) |
The logical function of the command is exactly the same as the expire, except that the time-out specified by the command is absolute, not relative. The time parameter is in the UNIX timestamp format, which is the number of seconds that flow through the beginning of January 1, 1970. |
1 indicates that the timeout is set, and 0 indicates that the key does not exist or cannot be set. |
TTL Key |
O (1) |
Gets the timeout description remaining for the key. |
Returns the remaining description, or 1 if the key does not exist or does not have a time-out setting. |
Randomkey |
O (1) |
Returns a key randomly from the currently open database. |
Returns the random key if the database is empty and returns nil. |
TYPE Key |
O (1) |
Gets the type of the value associated with the key specified in the parameter, which is returned in the form of a string. |
The strings returned are string, list, set, hash, and zset, if key does not exist return none |
SORT key [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 just give the most basic usage here, and interested netizens can refer to the official Redis documentation. |
Returns the original list after sorting. |
Examples of commands:
1. Keys/rename/del/exists/move/renamenx:
#在Shell命令行下启动Redis客户端工具. /> Redis-cli #清空当前选择的数据库 to facilitate understanding of the following example. Redis 127.0.0.1:6379> flushdb OK #添加String类型的模拟数据. Redis 127.0.0.1:6379> set MyKey 2 OK redis 127.0.0.1:6379> set Mykey2 "Hello" OK #添加Set类型的模拟数据. Redis 127.0.0.1:6379> sadd Mysetkey 1 2 3 (integer) 3 #添加Hash类型的模拟数据. Redis 127.0.0.1:6379> hset mmtest username "Stephen" (integer) 1 #根据参数中的模式, gets all the keys in the current database that conform to the pattern, as shown in the output, that the command does not block at execution time The value type associated with the key. Redis 127.0.0.1:6379> keys my* 1) "Mysetkey" 2) "MyKey" 3) "Mykey2" #删除了两个Keys. Redis 127.0.0.1:6379> del mykey mykey2 (integer) 2 #查看一下刚刚删除的Key是否还存在, from the returned results, MyKey has indeed been deleted. Redis 127.0.0.1:6379> exists mykey (integer) 0 #查看一下没有删除的Key to compare with the above command results. Redis 127.0.0.1:6379> exists mysetkey (integer) 1 #将当前数据库中的mysetkey键移入到ID为1的数据库中, from the results you can see that the move has been successful. Redis 127.0.0.1:6379> Move mysetkey 1 (integer) 1 #打开ID为1的数据库. Redis 127.0.0.1:6379> Select 1 OK #查Look at the presence of the key that has just been moved, and it already exists from the return result. Redis 127.0.0.1:6379[1]> exists mysetkey (integer) 1 #在重新打开ID为0的缺省数据库. Redis 127.0.0.1:6379[1]> Select 0 OK #查看一下刚刚移走的Key是否已经不存在, removed from the returned results. Redis 127.0.0.1:6379> exists mysetkey (integer) 0 #准备新的测试数据. Redis 127.0.0.1:6379> set MyKey "Hello" OK #将mykey改名为mykey1 redis 127.0.0.1:6379> rename MyKey mykey1 OK #由于mykey已经被重新命名, fetching again will return nil. Redis 127.0.0.1:6379> get MyKey (nil) #通过新的键名获取. Redis 127.0.0.1:6379> get Mykey1 "Hello" #由于mykey已经不存在了, so returns an error message. Redis 127.0.0.1:6379> Rename MyKey mykey1 (Error) ERR no such key #为renamenx准备测试key Redis 127.0.0.1:6379> s ET oldkey "Hello" OK redis 127.0.0.1:6379> set Newkey "World" OK #由于newkey已经存在, so the command failed to execute successfully. Redis 127.0.0.1:6379> renamenx oldkey newkey (integer) 0 #查看newkey的值 and found it was not covered by Renamenx. Redis 127.0.0.1:6379> get Newkey "World"
2. Persist/expire/expireat/ttl:
#为后面的示例准备的测试数据. Redis 127.0.0.1:6379> set MyKey "Hello" OK #将该键的超时设置为100秒. Redis 127.0.0.1:6379> expire MyKey (integer) 1 #通过ttl命令查看一下还剩下多少秒. Redis 127.0.0.1:6379> ttl mykey (integer) #立刻执行persist命令, the time-out key becomes a persistent key, and the timeout for that key is removed. Redis 127.0.0.1:6379> persist MyKey (integer) 1 #ttl的返回值告诉我们, the key has not timed out. Redis 127.0.0.1:6379> ttl mykey (integer)-1 #为后面的expire命令准备数据. Redis 127.0.0.1:6379> del mykey (integer) 1 redis 127.0.0.1:6379> set MyKey "Hello" OK #设置该键的超时被100秒. Redis 127.0.0.1:6379> expire MyKey (integer) 1 #用ttl命令看一下当前还剩下多少秒, from the results you can see that there are 96 seconds left. Redis 127.0.0.1:6379> ttl mykey (integer) #重新更新该键的超时时间为20秒, you can see from the return value that the command executed successfully. Redis 127.0.0.1:6379> expire MyKey (integer) 1 #再用ttl确认一下, as you can see from the results it was updated. Redis 127.0.0.1:6379> ttl mykey (integer) #立刻更新该键的值 so that its timeout is invalid. Redis 127.0.0.1:6379> set MyKey "World" OK #从ttl的结果可以看出, when the last command to modify the key executes, the time-out of the key is also invalid. Redis 127.0.0.1:6379> ttl mykey (integer)-1
3. Type/randomkey/sort:
#由于mm键在数据库中不存在, so the command returns none. redis 127.0.0.1:6379> type mm none #mykey的值是字符串类型, so returns a string. redis 127.0.0.1:6379> type MyKey string #准备一个值是set类型的键. Redis 127.0.0.1:6379> sadd mysetkey 1 2 (integer) 2 #mysetkey的键是set, so the string set is returned. redis 127.0.0.1:6379> type Mysetkey set #返回数据库中的任意键. redis 127.0.0.1:6379> randomkey "Oldkey" #清空当前打开的数据库. Redis 127.0.0.1:6379> flushdb OK #由于没有数据了, so return nil. Redis 127.0.0.1:6379> Randomkey (nil)
The above is the Redis tutorial (vii): Key operation command detailed content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!