Redis Foundation Key (Key)

Source: Internet
Author: User
Tags redis redis server
First, overview:

This section mainly describes the Redis command related to key. Learning these commands is a very important basis for learning Redis and a useful tool to fully tap the potential of redis.

The Redis key command is used to manage Redis keys. Grammar

The basic syntax for the Redis Key command is as follows:

Redis 127.0.0.1:6379> COMMAND Key_name
instance
Redis 127.0.0.1:6379> SET runoobkey redis
OK
redis 127.0.0.1:6379> DEL runoobkey
(integer) 1

In the example above, DEL is a command, and Runoobkey is a key. If the key is deleted successfully, the command outputs (integer) 1, otherwise output (integer) 0.

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 invocation of the command should be avoided as much as possible in our normal operations, because the command is time-consuming for large databases, and the performance impact on the Redis server is relatively high. Pattern supports Glob-style wildcard formats, such as * for any one or more characters,? represents any character, [ABC] denotes any letter in the square bracket.

A list of keys that match the pattern.

DEL key [key ...]

O (N)

N In time complexity indicates the number of keys deleted. The keys specified in the parameter from the database deletion, if the specified key does not exist, are ignored directly. It is also necessary to note that if the specified key is associated with a data type that is not a string type, but rather a container type such as list, set, hashes, and sorted Set, the command deletes the time complexity O (m) for each key, where m represents the number of elements in the container. For a string type key, its time complexity is O (1).

The number of keys that were actually deleted.

EXISTS Key

O (1)

Determines whether the specified key exists.

1 indicates existence, and 0 indicates that it does not exist.

Move Key db

O (1)

Moves the key keys 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.

The move successfully returns 1, otherwise 0.

RENAME Key Newkey

O (1)

Renames the specified key, if the command for the two keys in the parameter is the same, or if the source key does not exist, the command returns the associated error message. 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 a successful modification, otherwise 0.

PERSIST Key

O (1)

If the key has an expiration time, the command will eliminate its expiration time, so that the key no longer has a timeout, but can persist the storage.

1 indicates that the key's expiration time is removed, and 0 indicates that the key does not exist or has no expiration time.

EXPIRE key seconds

O (1)

This 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 timing 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 functionality of the command is exactly the same as expire, except that the timeout specified by the command is an absolute time, not a relative time. The time parameter is the UNIX timestamp format, that is, the number of seconds it flows from 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, if the key does not exist or does not have a time-out setting, returns-1.

Randomkey

O (1)

Returns a key randomly from the currently open database.

Returns a random key that returns nil if the database is empty.

TYPE Key

O (1)

Gets the type of the value associated with the specified key in the parameter, which is returned in the form of a string.

The string returned is string, list, set, hash, and Zset, and none is returned if the key does not exist.

SORT key [by mode] [LIMIT offset count] [get mode [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, interested netizens can refer to the official documents of Redis.

Returns the sorted original list.

Third, the command example:

1.keys/rename/del/exists/move/renamenx:

#在Shell命令行下启动Redis客户端工具.

/> REDIS-CLI

#清空当前选择的数据库 to facilitate an understanding of the following examples.

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

#根据参数中的模式 to get all the keys in the current database that match the pattern, as you can see from the output, the command does not distinguish between the value types associated with the key when executed.

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是否还存在, judging from the return result, MyKey has indeed been deleted.

Redis 127.0.0.1:6379> exists MyKey

(integer) 0

#查看一下没有删除的Key to compare to the results of the command above.

Redis 127.0.0.1:6379> exists Mysetkey

(integer) 1

#将当前数据库中的mysetkey键移入到ID为1的数据库中, you can see from the results that you have moved successfully.

Redis 127.0.0.1:6379> Move Mysetkey 1

(integer) 1

#打开ID为1的数据库.

Redis 127.0.0.1:6379> Select 1

Ok

#查看一下刚刚移动过来的Key是否存在, from the return of the result look already existed.

Redis 127.0.0.1:6379[1]> exists Mysetkey

(integer) 1

#在重新打开ID为0的缺省数据库.

Redis 127.0.0.1:6379[1]> Select 0

Ok

#查看一下刚刚移走的Key是否已经不存在, from the return result look has been removed.

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已经被重新命名, fetch again will return nil.

Redis 127.0.0.1:6379> Get MyKey

(nil)

#通过新的键名获取.

Redis 127.0.0.1:6379> Get Mykey1

"Hello"

#由于mykey已经不存在了, the error message is returned.

Redis 127.0.0.1:6379> Rename MyKey mykey1

(Error) ERR no such key

#为renamenx准备测试key

Redis 127.0.0.1:6379> set Oldkey "Hello"

Ok

Redis 127.0.0.1:6379> set Newkey "World"

Ok

#由于newkey已经存在, 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 100

(integer) 1

#通过ttl命令查看一下还剩下多少秒.

Redis 127.0.0.1:6379> TTL MyKey

(integer) 97

#立刻执行persist命令, the existing timeout key becomes a persisted key and the timeout for the 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 100

(integer) 1

#用ttl命令看一下当前还剩下多少秒, you can see from the results that there are 96 seconds left.

Redis 127.0.0.1:6379> TTL MyKey

(integer) 96

#重新更新该键的超时时间为20秒, you can see from the return value that the command was executed successfully.

Redis 127.0.0.1:6379> expire MyKey 20

(integer) 1

#再用ttl确认一下, it can be seen from the results that it was updated.

Redis 127.0.0.1:6379> TTL MyKey

(integer) 17

#立刻更新该键的值 so that its timeout is invalid.

Redis 127.0.0.1:6379> set MyKey "World"

Ok

#从ttl的结果可以看出, the timeout for the key is invalid after the last command that modified the key was executed.

Redis 127.0.0.1:6379> TTL MyKey

(integer)-1

3.type/randomkey/sort:

#由于mm键在数据库中不存在, so this command returns none.

Redis 127.0.0.1:6379> type mm

None

#mykey的值是字符串类型, so a string is returned.

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)

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.