C#redis Common key operations

Source: Internet
Author: User
Tags redis server

First, foreplay

In the first few blogs of the series, the main focus is 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.
In this blog, we will, as always, give a detailed list of all relevant commands and a typical example to facilitate our current learning and future review.

Second, the theory

command prototype Time complexity command description return value
KEYS pattern o (n) pattern supports Glob-style wildcard formats, such as * for any one or more characters, for any character, and [ABC] to denote any letter in square brackets. matches the list of keys for 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.

Third, actual combat

127.0.0.1:6379>Flushdbok127.0.0.1:6379>SetMyKey Abcok127.0.0.1:6379>Setmykey1 Hellook127.0.0.1:6379>Sadd Mysetkey a b C (integer)3127.0.0.1:6379>hset mmset username Cuiyanwei (integer)1127.0.0.1:6379> Keys my*1)"Mykey1"2)"Mysetkey"3)"MyKey"127.0.0.1:6379> Keys m*1)"Mykey1"2)"Mysetkey"3)"Mmset"4)"MyKey"127.0.0.1:6379>del mmset mykey (integer)2127.0.0.1:6379> Keys m*1)"Mykey1"2)"Mysetkey"127.0.0.1:6379>exists MyKey (integer)0127.0.0.1:6379>exists mykey1 (integer)1127.0.0.1:6379> Move Mykey11(integer)1127.0.0.1:6379>Select 1OK127.0.0.1:6379[1]>exists mykey1 (integer)1127.0.0.1:6379[1]>Select 0OK127.0.0.1:6379>exists mykey1 (integer)0127.0.0.1:6379>Rename Mykey1 mykey (Error) ERR no such key127.0.0.1:6379> Keys m*1)"Mysetkey"127.0.0.1:6379>Rename Mysetkey mykeyok127.0.0.1:6379>Rename MyKey mysetkeyok127.0.0.1:6379>SetMyKey Hellook127.0.0.1:6379> Keys m*1)"Mysetkey"2)"MyKey"127.0.0.1:6379>Rename MyKey newkeyok127.0.0.1:6379>GetMyKey (Nil)127.0.0.1:6379>GetNewkey"Hello"127.0.0.1:6379>SetOldkey Worldok127.0.0.1:6379>renamenx newkey oldkey (integer)0127.0.0.1:6379>GetNewkey"Hello"127.0.0.1:6379>GetOldkey" World"127.0.0.1:6379> Expire Newkey -(integer)1127.0.0.1:6379>ttl mykey (integer)-2127.0.0.1:6379>ttl newkey (integer) the127.0.0.1:6379>persist Newkey (integer)1127.0.0.1:6379>ttl newkey (integer)-1127.0.0.1:6379> Expire Oldkey -(integer)1127.0.0.1:6379>ttl oldkey (integer) the127.0.0.1:6379>SetOldkey Abcok127.0.0.1:6379>ttl oldkey (integer)-1127.0.0.1:6379>type Cuinone127.0.0.1:6379>type Newkeystring127.0.0.1:6379>Sadd Mysetkey a b C (integer)0127.0.0.1:6379> Sadd Setkey1 2 3(integer)3127.0.0.1:6379>type SetkeySet127.0.0.1:6379>Randomkey"Newkey"127.0.0.1:6379>

C#redis Common key operations

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.