Redis is the fastest-performing key-value database known.
1.key Related commands
Exites Key: Checks if the specified key exists 1 indicates presence 0 means not present
Del Key1,key2,key3 ...: Deletes the specified key, returns the number of deleted keys, and returns 0 if key does not exist.
Type key: Returns the kind of value given to the key. Returns none indicates that the key does not exist, string represents strings, list represents a linked list, set represents an unordered collection ...
Keys pattern: Returns all keys that match the specified pattern
Expire key seconds: Sets the expiration time for a given key
Randomkey: Returns a randomly selected key from the current database if the current database is empty returns an empty string
Rename Oldkey newkey: Rename key, overwrite if Newkey exists, return 1 for success, 0 fail. If it fails, it may oldkey not exist or be the same as Newkey.
Renamenx Oldkey Newkey: Ditto if Newkey exists return failure
TTL key: Returns the set expiration time of the remaining seconds of key, 1 means that key does not exist or the expiration time is not set
Move key Db-index: Moves the key from the current database to the specified database. Returns 1 success, 0 indicates that key does not exist or is already in the specified database
Case:
2. Data type
Redis supports a variety of data types, such as string,list,set,sorted Set,hash. Each of these data types has its own characteristics.
1.string type
The string type is binary safe, and you can save the picture and video file to a string, as defined below
struct sdshdr{ long len; Long Free ; Char buf[];}
BUF array: The entity of the string that holds the contents of the string
Len field: Record buf array size
Free field; record buf array How much space is there?
Because of the Len and free record string information, it is not necessary to use the nil character as the end for binary security. To improve the speed of your website, you can use string types to store some static file examples, Css,js, and so on.
String command:
Set key value: Sets the string value corresponding to key, returns 1 success, 0 failure
Setnx key value: If key does not exist then set if key already exists return 0
Get key: Gets the string value corresponding to key if key does not exist return nil
Getset key value: First get the value of key, then set the value of key, if key does not exist return nil
Mget Key1,key2 ... : Gets the value of more than one key at a time, if the corresponding key does not exist return nil
Mset key1 values1 key2 values2 ... : Set multiple keys at once successfully put back 1, failed returns 0 means none set
Msetnx key1 values1 key2 values2 ... : Set multiple keys at once, but do not overwrite existing
INCR key: Add 1 to the value corresponding to key and return the new value, note that the value of the key must be of type int or return an error if key does not exist set key to 1
DECR key: The value corresponding to key does-1, and returns the new value, note that the key must be of type int or return an error, if key does not exist then set key to 1
Incrby Key integer: The value corresponding to the key plus a specified integer, key does not exist then create a new one, and think the original value is 0
Decrby key Interger: Subtracts a specified integer from the value corresponding to key, creates a new one if the key does not exist, and considers the original value to be 0
Case:
3.list type
The list data type means that key corresponds to value as a doubly linked list structure, so the list type provides all the operations supported by the linked list. The list type is useful in Internet references, such as "List of my concerns" in Weibo, or all the reply IDs in the forum.
List command:
Lpush key string: Adds a string element to the list header corresponding to key, returns 1 successfully, fails to return 0
Rpush key string: Adds a string element to the list tail corresponding to key, returns 1 successfully, and returns 0
Llen key: Returns the length of the key corresponding to the list, if key does not exist return 0, if the key corresponding to the type is not a list return error.
Lrange Key Start end: Returns the element in the specified interval (start~~end), the following table starts at 0, negative values are calculated from the end of the linked list, 1 means the countdown first, key does not exist return empty column
LTrim Key start end: Intercept list within specified interval (start~~end) element, return 1,key no return error
LSet Key Index value: Sets the element in the list specified in the following table, returns 1,key successfully, or the following table does not have a return error.
Lrem Key count Value: Deletes the number of elements that match value from the list header (count positive) or tail (count negative), and returns the amount of the deleted element, which is 0 when count is deleted.
Lpop key: Removes and returns the deleted element from the list header. If the list for key does not exist or is null to return nil, if the value corresponding to the key is not a list return error
Rpop: Removes the element from the list tail and returns
Blpop key1 Key2 ... timeout: Scan from left to right key1 Key2, returns the Lpop operation for the first non-empty list and returns. If all lists are empty or nonexistent, blocking timeout seconds, timeout of 0 means that it is blocked. When blocking, if there is another client push speculation on any key in Key1...key2, the block is lifted and returned. If the timeout occurs, nil is returned
Brpop key. The Key2 timeout function is similar to Plpop, unlike the Blpop delete from the beginning and brpop from the tail
Case:
Use list to implement Message Queuing, minus database pressure. Message Queuing is similar to the implementation of the queue in life, each time a message arrives at the end of the queue, the message is not taken out. To have the list implement the message queue, first use the Rpush command bar message queue into the tail, and then use the Lpop command to send the message queue out of the head.
4.set type
The Set data type is an unordered collection that is implemented within Redis through Hashtable, where the time complexity of finding and deleting elements is O (1). The advantage of a set data type is that you can quickly find out whether an element exists and use it to record some data that cannot be duplicated. For example, when registering an account on a website, the user name cannot be duplicated, the user is registered using set record, and if the registered user already exists in set, the registration is refused.
Set command:
Sadd Key member: Adds a STRIMG element to the set set of key corresponding to the successful return 1, if the element returns 0 in the collection, the key corresponding set does not have a return error
Srem Key member: Removes the given element from the key corresponding set, returns 1 successfully, if the member does not exist in the collection or the key does not exist return 0 if key does not correspond to a set type return error
Spop key: Delete and return the key corresponding to the set of a random element, if set is empty or key corresponding set does not exist return nil
Srandmember key: The same spop randomly extracts an element, but does not delete the element
Smove Srckey Dstkey Member: Removes Srckey from the member corresponding set and adds it to the set of Dstkey pair, the entire operation is atomic. Successful return 1, if member does not exist in Srckey return 0, if key does not correspond to a set type, an error is returned
SCard key: Returns the number of SET elements if set is empty or key does not exist return 0
Sismember Key member: Determine if the member is in set, there is a return of 1, does not exist or the key corresponding set set does not exist return 0
Sinter Key1,key2,key3 ... : Returns the intersection of all given keys
Sinterstore dstkey Key1,key2 .... : Same as Sinter, at the same time, the intersection is stored in the dstkey corresponding set set
Sunion key1 Key2 ...: Returns the set of all keys
Sunionstore dstkey Key1,key2 .... : Same as Sunion, simultaneously and set to Dstkey corresponding set set
Sdiff key1 key2 Key3 ...: Returns the difference set for a given key
Sdiffstore dstkey key1 key2 key3 ...: Same as Sdiff, while saving the difference to dstkey corresponding set set
Smembers key returns all elements of the set corresponding to the key, the result is unordered
Case:
Set types can also be used to record certain things. For example, the voting system, each user can only vote once a day, then use set to record a user's vote, only a one-time as set key, then the user's ID as member. To see if a user voted today, just look at today's date as key to see if the user ID exists.
5.sorted set Type
The sorted set type is similar to the set type and is a collection of elements of type string, unlike the sorted set, which is an ordered set, and sorted set is sorted by an integer score of type Double. The sorted set is completed by skiplist (jumping table) and hashtable combination. Skiplist is responsible for sorting functions, while Hashtable is responsible for saving the data.
Because the sorted set is a sorted set, the set can do things sorted set can do. and sorted set can do things that the set cannot do, for example, using sorted set to build a queue with precedence, which is not possible with the list type. The commands supported by the sorted set type are shown in table 10-6.
Sorted SET Command:
Zadd Key Score Member: add element member to the collection, the element exists in the collection and corresponds to the new score
Zrem Key member: Delete the specified element, 1 indicates success, if the element does not exist return 0
Zincrby Key INCR member: Increase score value of corresponding member and reorder to return the updated score value
Zrank Key member: Returns the rank (subscript) of the specified element in the collection, and the elements in the collection are sorted by score from small to large
Zrevrank Key member: Ditto, but the elements in the collection are sorted by score from large to small
Zrange Key start end: The element that specifies the interval from the collection, and returns the structure sorted by score order
Zrevrange key start end: Ibid., return structure sorted in reverse order of score
Zrangebyscore key min Max: Returns the elements of score in a given interval in the collection
Zcount key min Max: Returns the number of score in a set in a given interval
Zcard key: Returns the number of elements in the collection
Zscore key element: Returns the corresponding score for the given element
Zremrangebyrank Key min Max: delete elements in the collection that rank in a given interval
Zremrangebyscore Key min Max: Removes elements from the collection score at a given interval
The sorted set type is useful in Web applications. For example, the ranking in the leaderboard by the number of ' top stickers ', by setting the sorted value to the score value of the sorted set, setting the specific data to the corresponding value, each time the user presses ' top sticker ' is, simply execute zadd command to modify the score value.
6.hash type
The hash type is a hashtable for each key, and the time complexity for adding deletes and modifications is 0 (1). Hash types are suitable for storing objects, such as user information objects. The user ID is used as key and the user information is saved to the hash type.
To save memory, Redis uses ZIPMAP to store data when creating a new hash type object. This zipmap is not a real Hashtable, adding, deleting, and modifying the time complexity of the operation is 0 (n), but is not memory compared to the normal hashtable,zipmap savings. If field or value exceeds a certain limit, Redis automatically replaces zipmap with normal hashtable storage internally. Modify the Hash-max-zipmap-entries and Hash-max-zipmap-value options for the configuration file to set both limits.
Hash command:
Hset key field value: Sets the values of the specified fields in the hash object corresponding to key. If the hash object for key does not exist, this hash object will be created. If the specified domain already exists, its value will be overridden
Hget key field: Returns the value associated with the Field field if the field does not exist or the key corresponding hash object does not exist, returns nil
Hmget Key filed1 ... Filedn: Returns the value that is associated with each specified field in the hash object stored in the key. Returns nil for fields that do not exist in the hash object
Hmset key filed1 value1 filed2 value2 ... : Sets the value of the specified field in the hash object that is stored in the key. This command will replicate the fields that already exist in the hash. If the hash object for key is not present, this hash object will be created
Hincrby key field Interger: Stores the value associated with the field field in the hash object that the key corresponds to, plus the value specified by Interger. If the hash for key does not exist, the hash object is established. If the field field does not exist or is not represented as an integer, set to 0 before performing the operation
Hexists key field: see if the specified field field exists
Hdel key field: Deletes the specified field field, returning 1. Returns 0 if the specified domain does not exist or the key does not exist
Hlen key: Returns the number of filed in key if key does not exist return 0
Hkeys key: Returns all filed names in the Hahs object corresponding to the key
Hvals key: Returns all values in the hash object for key
Hgetall key: Returns the value of all fields and associated values in the hash object that the key corresponds to. The return value, followed by each domain name, follows the associated value.
Case:
7.redis sorting
Redis supports sorting the list,set,sorted set type, the full format of the sort command is as follows:
Sort key [by pattern] [limit strt count] [get pattern] [ASC|DESC] [alpha] [store Dstkey]
1,sort Key Simple Sort
For example:
As you can see from the case, the default sort is ASC is ascending, but if the letters are sorted alphabetically, you can add alpha.
Case:
The case shows that if the number is not a letter, it will be an error if you do not use alpha.
[By pattern]
The sort command can be sorted by the value of the collection element itself, and the collection element content group can be synthesized as a new key by a given pattern (patern), sorted by the content of the new key, and returned with a sorted set of elements.
For example:
The pattern ' name* ' represents the use of the fill in the Key1 collection *, gets 3 new key name1 name2 Name3 then sorts the values of these 3 keys, and the result of the sort is key1 the order in which the collection is returned.
[Limit start Count]
Limit can be used to limit the number of returned results
For example:
Start subscript starting from 0, the limit option in the above example means to get 2 from 1 onwards
[Store Dstkey]
Using store to cache sorted results to a specified key can reduce CPU overhead
For example:
Relive Redis commands