Introduction of common data types:
There are five data types used by Redis: String,hash,list,set,zset (sorted set).
1.String type
String is the simplest type, and a key corresponds to a value
The data of type string is 1G maximum.
A string type can be treated as an integer, allowing the "INCR" command family to operate (Incrby, DECR, Decrby), in which case the value of the integer is limited to 64-bit signed numbers.
The separate element types contained in list, set, and Zset are Redis string types.
2.List type
Linked list type, the main function is push, pop, get all values of a range and so on. The key can be understood as the name of the linked list.
In Redis, lists are the list of Redis strings, sorted by insert order. For example, using the Lpush command to insert an element in the list header, use the Rpush command to insert an element at the end of the list. When one of these two commands acts on an empty key, a new list is created.
The maximum length of a list is 2^32-1 elements.
3.Set type
Collections, similar to the concept of collections in mathematics. The key in the operation is understood as the name of the collection.
In Redis, set is an unordered collection of redis strings, and no duplicate elements are allowed.
The maximum number of elements for set is 2^32-1.
The operation of set in Redis also has the intersection, the set, the difference set and so on.
4.ZSet (Sorted Set) type
Zset is an upgraded version of Set, which adds a sequential attribute on the set, which can be specified when a modification element is added, and Zset automatically installs the specified value in the reorder order after each assignment. It can be understood as a table, a column of value, and a list of stored orders. The key in the operation is understood as the name of Zset.
The maximum number of elements for Zset is 2^32-1.
For an already ordered zset, you can still use the sort command by specifying the asc| The desc parameter to sort it.
5.hash type
Hash is the data type closest to the relational database structure, which can convert one object in a database or program into a hashmap stored in Redis.
Second, the specific operation order
Detailed help documentation can be found in: http://www.redis.cn/commands.html
1. Commands for the value operation
1 exists (key): Verify that a key exists2 3 del (key): Delete a key4 5 type (key): Return value Types6 7 keys (pattern): Returns all keys that satisfy the given pattern8 9 Randomkey: Randomly returns a key in key spaceTen One rename (oldname, newname): Rename the key from Oldname to newname, or delete the newname-represented key if newname exists A - dbsize: Returns the number of keys in the current database - the expire: Set the active time of a key (s) - - TTL: Get the active time of a key - + Select (index): Query by index (SELECT database) - + Move (Key, Dbindex): Transfers the key from the current database to the database with the Dbindex index A at FLUSHDB: Delete all keys in the currently selected database - -Flushall: Delete all keys in all databases
2. Commands for string manipulation
1 set (key, value): Assigns a value to a string named key in the database2 3 get (Key): Returns the value of a string named key in the database4 5 Getset (key, value): Sets the value of a key and gets the values before the setting6 7 mget (key1, Key2,..., key N): Returns multiple strings in the library (their names are key1,key2 ... ) of Value8 9 setnx (key, value): If a string with the name key does not exist, add a string to the library with the name key, valueTen One Setex (key, Time, value): Adds a string (named key, value) to the library and sets the expiration time in seconds A - Mset (Key1, value1, Key2, value2,... key N, value N): Assigning multiple strings at the same time, string assignment value I with Name key I - the msetnx (Key1, value1, Key2, value2,... key N, value N): If all strings with the name key I do not exist, a string is added to the library, the name key I is assigned value i
- - incr (Key): 1 operation with a string named key - + Incrby (Key, Integer): String with Name Key added integer - + DECR (key): string minus 1 operation with name Key A at Decrby (Key, Integer): string with the name key reduced by integer - - Append (key, value): String value with the name key appended value - -SUBSTR (key, start, end): Returns a substring of string with the name key, starting with 0, and the substring having a value of start<= V <= end
3. Commands for list operations
1 Rpush (key, value): Adds an element of value to the list at the end of the name Key2 3 Lpush (key, value): Adds an element of value to the list header with the name Key4 5 Llen (Key): Returns the length of the list named key6 7 Lrange (key, start, end): Returns the element between start and end in list named key (subscript starting from 0, same as below)8 9 LTrim (key, start, end): Intercepts the list named key, preserving the element between start and endTen One lindex (key, index): Returns the element of the index position in the list named key A - LSet (key, index, value): Assigns the element of the index position in the list named key to value - theLrem (Key, Count, value): Removes the count of the elements in the list with the name key as value. Count is 0, remove all elements that value is count>0, remove the count value to the element from beginning to end, count<0 remove the |count| from the tail headan element with a value of values. - - Lpop (Key): Returns and removes the first element in a list named key - + Rpop (Key): Returns and Deletes the tail element in the list named key - +Blpop (Key1, Key2,... key N, timeout): The block version of the Lpop command. That is, when timeout is 0 o'clock, if a list with the name key I does not exist or the list is empty, the command ends. If timeout>0, if this is the case, wait for timeout seconds, and if the problem is not resolved, then the key i+The 1 start list performs a pop operation. A at Brpop (key1, Key2,... key N, timeout): The block version of Rpop. Refer to the previous command. - -Rpoplpush (Srckey, Dstkey): Returns and removes the tail element of the list named Srckey, and adds the element to the head of the list named Dstkey
4. Commands for SET operations
1 Sadd (Key, member): Adds an element to a set named Key member2 3 Srem (Key, member): Removes the element in the set named Key member4 5 Spop (key): Randomly returns and deletes an element in a set named key6 7 Smove (Srckey, Dstkey, member): Moves the member element from a collection named Srckey to a collection named Dstkey8 9 SCard (Key): Returns the cardinality of a set named keyTen One Sismember (Key, member): Test whether the member is a set element with the name key A - sinter (key1, Key2,... key N): Intersection - the Sinterstore (Dstkey, Key1, Key2,... key N): Sets the intersection and saves the intersection to Dstkey - - sunion (key1, Key2,... key N): Seek and set - + Sunionstore (Dstkey, Key1, Key2,... key N): set and save the set to Dstkey - + Sdiff (key1, Key2,... key N): Differential set A at Sdiffstore (Dstkey, Key1, Key2,... key N): Differential set and save the difference set to Dstkey collection - - smembers (Key): Returns all elements of a set with the name key - -Srandmember (key): Randomly returns an element of a set with the name key
5. Commands for Zset (sorted set) operations
1 Zadd (key, Score, member): Adds an element member,score for sorting to a zset named key. If the element already exists, the order of the element is updated according to score. 2 3 Zrem (Key, member): Removes the element in Zset with the name Key member4 5 Zincrby (key, Increment, member): If the element member already exists in the Zset with the name key, the score of the element is incremented increment; otherwise, the element is added to the collection. The value of its score is increment6 7 Zrank (Key, member): Returns the rank (that is, index, starting from 0) of the member element in the Zset with the name key (the element has been sorted score from small to large), and returns "nil" if there are no member elements8 9 Zrevrank (Key, member): Returns the rank (that is, index, starting from 0) of the member element in the Zset with the name key (the element has been sorted by score from large to small), and returns "nil" if there is no member elementTen One Zrange (key, start, end): Returns all elements of index from start to end in Zset with the name key (the element has been sorted by score from small to large) A - Zrevrange (key, start, end): Returns all elements of index from start to end in Zset with the name key (the element has been sorted by score from large to small) - theZrangebyscore (Key, Min, max): Returns zset score >= min and score <= named keyall elements of Max - - Zcard (Key): Returns the cardinality of the Zset with the name Key - + Zscore (key, Element): Returns the score of element elements in Zset with the name Key - +Zremrangebyrank (Key, Min, max): Remove rank >= min and rank <= in Zset named keyall elements of Max A atZremrangebyscore (Key, Min, max): Delete zset score >= min and score <= named keyall elements of Max - -Zunionstore/zinterstore (Dstkeyn, Key1,..., KeyN, WEIGHTS W1,... WN, AGGREGATE sum| min| MAX): Sets and intersections of N Zset, and saves the last collection in Dstkeyn. For the score of each element in a collection, multiply the weight parameter for the aggregate operation before doing it. If weight is not provided, the default is 1. The default aggregate is sum, that is, the score of the elements in the result set is the value of the sum operation for all the corresponding elements of the collection, while Min and Max means that the score of the elements in the result set are the minimum and maximum values in the corresponding elements of all the collections.
6. Commands for hash operations
1Hset (Key, field, value): Adds an element to a hash named key field<->value2 3 Hget (Key, field): Returns the value of field corresponding to the hash named key4 5 hmget (Key, field1, ..., field N): Returns the value corresponding to field I in the hash named key6 7Hmset (Key, field1, value1,..., field N, Value N): Adds an element to a hash named key field i<->Value I8 9 Hincrby (Key, field, integer): Adds an integer to the value of field in the hash named keyTen One hexists (Key, field): A field with the key field in the hash named key A - Hdel (Key, field): Delete The field with the key field in the hash named key - the Hlen (Key): Returns the number of elements in a hash with the name Key - - Hkeys (Key): Returns all keys in a hash with the name Key - + hvals (Key): Returns the value corresponding to all keys in a hash with the name Key - +Hgetall (Key): Returns all keys (field) and their corresponding value in a hash with the name key
5. Redis Native command operation of different data types