In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the introduction and use of the key command in redis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
I. Overview:
After entering the redis-cli, we are usually more concerned about which key (ps: of course, you can also use other client tools), so we have to say the keys command.
The Redis key command is used to manage the key of redis.
Grammar
The basic syntax of the Redis key command is as follows:
Redis 127.0.0.1 6379 > COMMAND KEY_NAME
2. List of related commands:
The command prototype time complexity command describes the return value KEYS patternO (N) the N in the time complexity represents the number of Key in the database. Gets all Keys that matches the pattern parameter. It is important to note that the call to this command should be avoided in our normal operation, because for large databases, the command is very time-consuming and has a great impact on the performance of the Redis server. Pattern supports glob-style wildcard formats, such as * for any one or more characters,? Represents any character, and [abc] represents any letter in square brackets. A list of keys that match the pattern. The N in the DEL key [key...] O (N) time complexity represents the number of Key deleted. Delete the keys specified in the parameter from the database, and ignore it if the specified key does not exist. It is also important to note that if the data type associated with the specified Key is not a String type, but a container type such as List, Set, Hashes, and Sorted Set, the time complexity of the command to delete each key is O (M), where M represents the number of elements in the container. For String type Key, the time complexity is O (1). The actual number of Key deleted. EXISTS keyO (1) determines whether a specified key exists. 1 means it exists and 0 means it doesn't exist. MOVE key dbO (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. A successful move returns 1, otherwise 0. RENAME key newkeyO (1) renames the specified key, and if the two Keys commands in the parameter are the same, or the source Key does not exist, the command returns the relevant error message. If newKey already exists, it is overwritten directly. RENAMENX key newkeyO (1) if the new value does not exist, change the original value in the parameter to the new value. Other conditions are consistent with RENAME. 1 indicates that the modification was successful, otherwise 0. PERSIST keyO (1) if the Key has an expiration time, the command eliminates its expiration time so that the Key no longer has a timeout, but can be persisted. 1 indicates that the expiration time of the Key has been moved out, and 0 indicates that the Key does not exist or has no expiration time. EXPIRE key secondsO (1) this command sets the number of seconds of timeout for the Key specified in the parameter, after which 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, and 0 indicates that Key does not exist or cannot be set. EXPIREAT key timestampO (1) the logical function of this command is exactly the same as that of EXPIRE, except that the timeout specified by the command is an absolute time, not a relative time. The time parameter is in Unix timestamp format, that is, the number of seconds passed since January 1, 1970. 1 indicates that the timeout is set, and 0 indicates that Key does not exist or cannot be set. TTL keyO (1) gets the description of the timeout left by the key. Returns the remaining description, or-1 if the key does not exist or does not time out. RANDOMKEYO (1) randomly returns a Key from the currently open database. Returns a random key, or nil if the database is empty. TYPE keyO (1) gets the type of value associated with the specified key in the parameter, which is returned as a string. The returned strings are string, list, set, hash, and zset, and none is returned if key does not exist. SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern...]] The command [ASC | DESC] [ALPHA] [STORE destination] O (N+M*log (M)) is relatively complex, so we only give the most basic usage here. Interested netizens can refer to the official documentation of redis. Returns the sorted original list.
3. Examples of commands:
1 、 KEYS/RENAME/DEL/EXISTS/MOVE/RENAMENX:
# start the Redis client tool under the Shell command line. / > redis-cli # clears the currently selected database to facilitate understanding of the following examples. Redis 127.0.0.1 String 6379 > flushdb OK # add simulation data of type String. Redis 127.0.0.1 set mykey2 OK redis 6379 > set mykey2 "hello" OK # add simulation data of type Set. Redis 127.0.0.1 integer 6379 > sadd mysetkey 123 (integer) 3 # add simulation data of type Hash. Redis 127.0.0.1 key 6379 > hset mmtest username "stephen" (integer) 1 # gets all the key in the current database that conform to that schema according to the schema in the parameter. As you can see from the output, the command executes without distinguishing between the type of Value associated with the parameter. Redis 127.0.0.1 mykey 6379 > keys my* 1) "mysetkey" 2) "mykey" 3) "mykey2" # deleted two Keys. Redis 127.0.0.1 mykey 6379 > del mykey mykey2 (integer) 2 # check to see if the Key you just deleted still exists. From the returned result, the mykey has indeed been deleted. Redis 127.0.0.1 Key 6379 > exists mykey (integer) 0 # check the Key that has not been deleted to compare it with the above command result. Redis 127.0.0.1 mysetkey 6379 > exists mysetkey (integer) 1 # moves the mysetkey key from the current database into the database with ID 1, and the result shows that the move has been successful. Redis 127.0.0.1 integer 6379 > move mysetkey 1 (integer) 1 # Open the database with ID 1. Redis 127.0.0.1 OK 6379 > select 1 OK # check to see if the Key just moved exists. From the returned result, it already exists. Redis 127.0.0.1 redis 6379 [1] > exists mysetkey (integer) 1 # is reopening the default database with ID 0. Redis 127.0.0.1 Key 6379 [1] > select 0 OK # check to see if the Key you just removed no longer exists, and the result returned shows that it has been removed. Redis 127.0.0.1 redis 6379 > exists mysetkey (integer) 0 # prepare the new test data. Redis 127.0.0.1 redis 6379 > set mykey "hello" OK # rename mykey to mykey1 redis 127.0.1 hello 6379 > rename mykey mykey1 OK # since mykey has been renamed, getting again will return nil. Redis 127.0.0.1 redis 6379 > get mykey (nil) # is obtained by the new key name. Redis 127.0.0.1 hello 6379 > get mykey1 "hello" # because the mykey no longer exists, an error message is returned. Redis 127.0.0.1 redis 6379 > rename mykey mykey1 (error) ERR no such key # prepare the test for renamenx key redis 127.0.0.1 error 6379 > set oldkey "hello" OK redis 127.0.0.1 world OK # the command was not successfully executed because newkey already exists. Redis 127.0.0.1 newkey 6379 > renamenx oldkey newkey (integer) 0 # looks at the value of newkey and finds that it is also not overwritten by renamenx. Redis 127.0.0.1 world 6379 > get newkey "world"
2 、 PERSIST/EXPIRE/EXPIREAT/TTL:
# Test data prepared for the following example. Redis 127.0.0.1 hello 6379 > set mykey "hello" OK # sets the timeout of the key to 100 seconds. Redis 127.0.0.1 integer 6379 > expire mykey 100 (integer) 1 # use the ttl command to see how many seconds are left. Redis 127.0.0.1 integer 6379 > ttl mykey (integer) 97 # immediately executes the persist command, and the key that has timed out becomes the persisted key, which removes the timeout for this Key. The return value of redis 127.0.0.1 integer 6379 > persist mykey (integer) 1 # ttl tells us that the key has not timed out. Redis 127.0.0.1 expire 6379 > ttl mykey (integer)-1 # prepares the data for the following expire command. Redis 127.0.0.1 redis 6379 > del mykey (integer) 1 redis 127.0.1 redis 6379 > set mykey "hello" set mykey # sets the timeout of the key by 100 seconds. Redis 127.0.1 integer 6379 > expire mykey 100 (integer) 1 # use the ttl command to see how many seconds are left, and you can see from the results that there are 96 seconds left. The timeout for redis 127.0.0.1 integer 6379 > ttl mykey (integer) 96 # to update the key is 20 seconds, and the return value shows that the command was executed successfully. Redis 127.0.1 integer 6379 > expire mykey 20 (integer) 1 # confirm again with ttl, and you can see from the result that it has been updated. Redis 127.0.0.1 redis 6379 > ttl mykey (integer) 17 # immediately updates the value of the key to invalidate its timeout. Redis 127.0.1 world 6379 > set mykey "world" OK # you can see from the results of ttl that after the last command that modified the key was executed, the timeout of the key was also invalid. Redis 127.0.0. 1RV 6379 > ttl mykey (integer)-1
3 、 TYPE/RANDOMKEY/SORT:
# because the mm key does not exist in the database, the command returns none. The value of redis 127.0.0.1 redis 6379 > type mm none # mykey is of type string, so it returns string. Redis 127.0.0.1 set 6379 > type mykey string # prepares a key whose value is of type set. The key of redis 127.0.0.1 mysetkey 6379 > sadd mysetkey 12 (integer) 2 # mysetkey is set, so the string set is returned. Redis 127.0.0.1 redis 6379 > type mysetkey set # returns any key in the database. Redis 127.0.0.1 redis 6379 > randomkey "oldkey" # clear the currently open database. Redis 127.0.0.1 nil 6379 > flushdb OK # returns nil because there is no data. Redis 127.0.0.1 randomkey 6379 > randomkey (nil) so much for the introduction and use of key commands in redis. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.