Redis Key Related commands

Source: Internet
Author: User

In essence, Redis is a key---Value database. Here I first introduce the relevant commands for the key in Redis,

Note: Key is a string store, but cannot use spaces or "\ n", and value can use any string. (Reason: \ n is the terminator of the command, the space is the delimiter of the command and the argument)

 The following key-related commands and some instances  

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 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.

Use more frequently, to a few instances:

1. Show all keys (shown in matching mode) Note that the keys

127.0.0.1:6379> Keys *1)"13326832F534F02D5829AD1A78C1EBBF" 2)" Age" 3)"d58b02fe801a4313be49ab4d29f5d687" 4)"e68178c7159ff3ccd394cdab52a639d2" 5)"key_project_107".....

2. Whether a key exists

Redis 127.0.0.1:6379> exists Mouge

3. Remove one that can

Redis 127.0.0.1:6379> del Mouge

4. Set the expiration time (in seconds) of a key:

Redis 127.0.0.1:6379> expire Mouge 10

5. Remove the expiry time for a given key:

Redis 127.0.0.1:6379[1]> expire Age 300

(integer) 1

Redis 127.0.0.1:6379[1]> TTL age

(integer) 294

Redis 127.0.0.1:6379[1]> persist Age

(integer) 1

Redis 127.0.0.1:6379[1]> TTL age

(integer)-1

6. Rename Key:

Redis 127.0.0.1:6379[1]> Keys *

1) Mouge

Redis 127.0.0.1:6379[1]> Rename Mouge mouge_2

Ok

Redis 127.0.0.1:6379[1]> Keys *

1) mouge_2

Mouge successfully changed our name to Mouge_2.

9. Type

Type of return value:

Redis 127.0.0.1:6379> Type Addr

String

Redis 127.0.0.1:6379> Type Myzset2

Zset

Redis 127.0.0.1:6379> Type MyList

List

Redis 127.0.0.1:6379>

This method can be very simple to determine the value of the type.

Redis Key Related commands

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.