Operation of REDIS data storage type directly on a Redis-installed Linux machine-operation on key

Source: Internet
Author: User
Tags redis server

First, overview: The previous blogs focused 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.  ii. List of related commands: Command prototype time complexity command description return valueKEYS 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 the wildcard format of Glob-style, 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 moved out, 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 to 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#清空当前选择的数据库 in order to understand the following example. Redis 127.0.0.1:6379> flushdbOK#添加String类型的模拟数据. Redis 127.0.0.1:6379> set MyKey 2OKRedis 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 you can see from the output, that the command does not distinguish between the value type associated with a key when it executes. 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 to the result of the above command. Redis 127.0.0.1:6379> exists mysetkey(integer) 1#将当前数据库中的mysetkey键移入到ID为1的数据库中, the results can be seen to have moved successfully. Redis 127.0.0.1:6379> Move Mysetkey 1(integer) 1#打开ID为1的数据库. redis 127.0.0.1:6379> Select 1OK#查看一下刚刚移动过来的Key是否存在, from the return of the results see already exist. Redis 127.0.0.1:6379[1]> exists mysetkey(integer) 1#在重新打开ID为0的缺省数据库. redis 127.0.0.1:6379[1]> Select 0OK#查看一下刚刚移走的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改名为mykey1redis 127.0.0.1:6379> rename MyKey mykey1OK#由于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 the error message is returned. redis 127.0.0.1:6379> rename MyKey mykey1(Error) ERR no such key#为renamenx准备测试keyRedis 127.0.0.1:6379> set Oldkey "Hello"OKRedis 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 that 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 persisted 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) 1Redis 127.0.0.1:6379> set MyKey "Hello"OK#设置该键的超时为100秒. Redis 127.0.0.1:6379> expire MyKey(integer) 1#用ttl命令看一下当前还剩下多少秒, you can see that there are 96 seconds left in the results. 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确认一下, you can see from the results that 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的结果可以看出, after the last command that modified the key was executed, the timeout for that key was not valid. 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 mmNone#mykey的值是字符串类型, so a string is returned. redis 127.0.0.1:6379> Type MyKeystring#准备一个值是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 MysetkeySet#返回数据库中的任意键. Redis 127.0.0.1:6379> Randomkey"Oldkey"#清空当前打开的数据库. Redis 127.0.0.1:6379> flushdbOK#由于没有数据了, so return nil. Redis 127.0.0.1:6379> Randomkey(nil)

Operation of REDIS data storage type directly on a Redis-installed Linux machine-operation on key

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.