Basic operations for Redis keys

Source: Internet
Author: User
Tags delete key time in milliseconds

What is Redis?
Redis Open Source, a NoSQL database built on memory data structures. It is often used for data storage, cache processing, and message processing.


What are the advantages of Redis?
1. Very high reading and writing ability
2. Rich data types
3. Atomic operation
4, support master-slave hot Standby


redis usage scenarios?

2, leaderboards/counters, such as some show-class projects, often there will be some of the top number of host rankings, there are some articles reading volume of technology, or Sina Weibo, such as the number of points like
3, as a message queue, such as celery can use Redis as the middleman
4, friend relationship, Weibo's friend relationship uses Redis to implement


Basic operation commands for Redis keys:
1. Keys pattern
Function: Query all the key names that match the matching pattern
Use of pattern matching patterns:
?: any one character
*: Any of the characters
[Ae]:a or E
[^ae]: except A and E
[one character in the A-e]:a-e range
[^A-E]: Except one character of A-E
[one character in the 1-11]:1-11 range
[^1-11]: Except one character of 1-11
Return value: Returns all the key names that match the matching pattern
Exp:keys *

2. exists key [key ...]
Function: Check whether the given key key (can be one or more) is present in Redis
Return value: Because key can not exist or can be repeated, return these key keys in the number of Redis, key can be repeated, the return value statistic quantity does not go heavy
Exp:exists key1 Key2 ...

3. SCAN cursor [MATCH pattern] [count Count]
Function: Each time the scan command is called, it will return the new cursor cursor and all keys listed in this execution command, and the user can use the new cursor cursor as the cursor parameter of the scan command for the next iteration to continue the previous iterative process. Until a new cursor of 0 is returned, all keys that match pattern matching patterns are finally listed. This is a much more cumbersome way to query key than using the keys pattern, but when you have a very large key, it is significantly more efficient to use the scan method.
Pattern usage: Consistent with pattern usage in the keys pattern command
Count parameter: Represents the number of keys to be listed for this scan, but the number of keys that are actually listed is not necessarily the value of the Count parameter, which is not accurate
Return value: Traverse all key data that conforms to pattern, these keys may be duplicated and need to be judged in the application of the client program
EXP:SACN 0
SACN 0 match key?
Scan 0 Match Key[1-8] Count 5

4, Randomkey
Function: Randomly returns a key in the Redis database
Return value: A key name
Exp:randomkey

5. Type key
Function: Query the type of a key key
Return value: If key exists, returns the representation of the data type stored on the key, with a total of string,list,set,zset,hash five different types. If key does not exist, none is returned.
Exp:type Key1

6. Object subcommand [arguments [arguments ...]
Function: To view the Redis object of a given key from the inside (the specific object that the key corresponds to)
Subcommand Sub-command:
Object RefCount key: Returns the number of references to the object corresponding to the current key
Object Idletime key: Returns the idle time of the object corresponding to the current key
Object encoding key: Returns the data type of the object that the current key corresponds to, such as ' int ', ' quicklist ', ' embstr ', and so on
Return value: Refcount/idletime Returns a number, encoding returns the encoding type of the Redis object
Note: If an object's encoding is ' int ', and the value of this object is between 0-9999, then his refcount reference count will always be 2147483647, while other types or data that are not within that range will return the specific number of references.
Exp:object RefCount Key1
Object Encoding Key1
Object Idletime Key1

7. Rename Key Newkey
Function: Rename key to Newkey, if Newkey already exists, the data pointed by key will overwrite existing Newkey data
Return value: 1, success will return OK
2. If the renamed key does not exist in Redis, an error will be found.
Exp:rename username Name

8. Renamenx Key Newkey
Function: Rename key to Newkey when and only if Newkey is not present
Return value: 1, old name key does not exist in Redis, will error
2. The renaming succeeds returns 1

3. The new name Newkey is already present in Redis and returns 0
Exp:renamenx username Name

9. Touch key [key ...]
Function: Touch key (can be multiple), the last access time of the key is changed to the current time, of course, the idle time of this object will become 0.
Return value: Because key can not exist or can be repeated, the return value is the actual touch to the number of keys (key exists and repeat does not go back to the heavy quantity)
Exp:
Touch username
Object Idletime Username

10. del key [key ...]
Function: Delete key, can be multiple, belong to blocking delete
Return value: If key does not exist, returns zero, if more than one key, returns the number of keys actually deleted, compared to the return value of exists and touch, and if key repeats, returns when the value of the return is calculated
Exp:del username
Del key1 key2 username

11. Unlink key [key ...]
Function: Delete key, can be multiple, belong to non-blocking delete, use same as Del, but more efficient than Del
Return value: If key does not exist, returns zero, if more than one key, returns the number of keys actually deleted, compared to the return value of exists and touch, and if key repeats, returns when the value of the return is calculated
Exp:unlink username
Unlink Key1 Key2 username

12. Expire Key seconds
Function: Sets the number of seconds after the given key expires (if the given key has already set an expiration time, change the Expiration Time action). If the set seconds is less than 0 equals 0, the key will be deleted immediately.
Return value: 1, set to return successfully 1
2. Otherwise the key does not exist or the setting fails to return 0
Exp:expire age 60

13. Expireat Key Timestamp
Function: Set a specific expiration point for the key, the time point is the UNIX timestamp
Return value: 1, Success returns 1
2, when the key does not exist or can not set the expiration time to return 0
Exp:expireat username 1600000000

14. Pexpire Key milliseconds
Function: Consistent with expire, but is worth the value of milliseconds
Return value: 1, set to return successfully 1
2, when the key does not exist or can not set the expiration time to return 0
Exp:pexpire Age 60000

15. Pexpireat Key Milliseconds-timestamp
Function: Consistent with pexpire function, except set timestamp to millisecond level
Return value: 1, Success returns 1
2, when the key does not exist or can not set the expiration time to return 0
Exp:pexpireat username 1600000000000

16. TTL key
Function: Returns the remaining expiration time of key in seconds
Return value: 1, when key does not exist, return-2
2. When key exists but no expiration time is set, return-1
3, otherwise, in seconds, returns the remaining expiration time of key
Exp:ttl username

17. Pttl Key
function: Same as TTL, except that the remaining expiration time returned is the value in milliseconds

18. Persist Key
Function: Removes the lifetime of a given key and converts the key to a persistent
Return value: 1, removal successful return 1
2. Key does not exist or the expiration time is not set to return 0
Exp:persist Age

19. Dump Key
Function: Serializes the given key and returns the serialized result, facilitates the transfer of operations, and the serialized result does not contain any expiration time related information
Return value: 1, if successful returns the serialized result (a specially formatted string)
2. Return NIL If key does not exist
Exp:dump username

20. Restore key TTL serialized-value [REPLACE]
Function: Deserializes the given serialized string, associates the resulting result with the given key, and sets the expiration time to be the TTL in milliseconds (if the TTL is 0, the newly generated key is permanently valid). If the key already exists, the original key will be overwritten if replace is used, and an error will be made if replace is not used.
Return value: 1, returns OK if successful
2, the given key already exists, but does not use the Replace parameter, will error

21. Sort key [by pattern] [LIMIT offset count] [get pattern [get pattern ...]] [asc| DESC] [ALPHA] [STORE destination]
Function: Returns or saves the sorted result of a given list, collection, ordered collection. Sorting defaults to numbers as objects, values are interpreted as double-precision floating-point numbers, and then compared
Return value: 1, the store parameter is not used, returns the result of the sorted list form
2. If the store parameter is used, the sorting result is saved to the given destination key and the number of result elements is returned. If the destination is a key that already exists, the contents of the original key will be overwritten
Exp:lpush nums 1 4 21 3 21 43 20
Sort Nums Store Newkey
Sort nums desc Store Newkey Limit 2 3

22. Move Key DB
Function: Data migration between different libraries in the same instance, moving the key of the current database into the specified db. Move has no effect if the key is present in the target database, or if the key is not present in the current database. Note that this operation is not a copy of the move.
Return value: Returns 1 if the move is successful. If there is no move effect, 0 is returned
Exp:move Username 1

23. Migrate Host Port key| Destination-db timeout [COPY] [REPLACE] [KEYS key]
Functions: Data migration between multiple instances, moving key atomicity from the current instance to the specified database of the target instance, and once the transfer is successful, the key is guaranteed to appear on the target instance's specified database, and the key in the current library is deleted. The timeout parameter is the time-out time in milliseconds and the delivery failure if the timeout occurs. If the key is in the specified library of the target instance, then if replace is used, the key on the target instance will be overwritten or the delivery will fail. Because the operation is atomic, he blocks the two instances being migrated until the following time, the migration succeeds, the migration fails, and the migration times out. Note that if you use the copy parameter, it will be copied, and the key in the original instance will be preserved. If you don't use copy, you're moving.
If you want to move more than one key, the key parameter can pass a placeholder and then add the keys parameter to

Exp:migrate 192.168.12.133 6379 username 0 Copy Replace

Migrate 192.168.12.133 6379 "" 0 copy replace Key1 key2 Key3

Basic operations for Redis keys

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.