Redis can store mappings that can store keys between 5 different data structure types.
The five structure types are: string (string), list, set (set), hash (hash), Zset (ordered collection).
1. String type
The string data structure is a simple key-value type , and Redis (in most cases) does not understand or parse its meaning when using string, whether using JSON, XML, or
Plain text appears to be the same for Redis, just a string that can be used only for strlen, append, etc., and cannot be further manipulated for its contents.
Its basic operations commands are set, GET, strlen, GetRange, append: In most cases, where a pure number is stored in a string, Redis can
Strings as numbers for further operations, including DECR, Decrby, INCR, Incrby, and Incrbyfloat.
1) Assignment: SET key value. such as set Hello world2) Take the value: GET key. such as Get hello. Back to the world3) Self-increment: INCR key. Is the auto_increment of MySQL. Each time you execute INCR key, the value of the key will be +1. If key does not exist, first create a 0 and then +1, returns 1.
An error if the value is not an integer. The operation is an atomic operation. 4) Self-subtraction: decr key. Reduces the value of the specified key by 1. For example, Decr num, which is num-1 5), N:incrby key increment is used to add increment to the value of the specified key. such as Incrby Num 5 is num+5 6) N:decrby key increment is used to reduce the value of the specified key increment. If Decrby Num 5 is num-5 7) Add floating point number: Incrbyfloat key increment. 8) Append to Tail: APPEND key value. such as set Test:key 123 append Test:key 456get Test:key is 123456 .9) Gets the length: STRLEN key. 10assign values to multiple keys at the same time: MSET title This is the title description This is the content that describes the contents. 11gets the value of multiple keys at the same time: MGET title description Content12) bit operation acquisition: Getbit key offset. If character A is stored in Redis as 01100001 (ASCII is 98), then Getbit key 2 is 1,get key 0 is 0. 13) Bit operation setting: Setbit key offset value. If character A is stored as 01100001 (ASCII 98) in Redis, then setbit key 6 0,setbit key 51
So get key gets the B. Because the extracted binary is 01100010. 14) bit operation statistics: Bitcount key [start] [End]:bitcount key is used to get the value of key in the number of binary is 1. The Bitcount key start end is used to
The binary of the substring between the start and end of the value of key is counted as the number of 1 (well-wound). 15) Bit operations: bitop operation Resultkey Key1 Key2. Operation is the operation of a bitwise operation with And,or,xor,not. Resultkey is the arithmetic structure
stored in this key, Key1 and Key2 are key to participate in the operation, the key to participate in the operation can specify more than one.
2. List Type
The Redis-to-list (linked-list) architecture makes it unique in the world of key-value storage, where a listing structure can store multiple strings in an orderly fashion .
when using list, value is a string array that, when manipulating this set of strings, can use pop and push operations like a stack, but both ends of the stack can be manipulated, or it can be manipulated as an array with an index parameter. List operation commands are slightly miscellaneous, mainly divided into two categories: L start with R, L for left or list, to do some of the actions from the end of the list, or some side-independent operation; R stands for right, doing some action from the left side of the list.
1) Insert to head: Lpush key value1 value2 .... Returns the added list length. 2) insert into the tail: Rpush key value1 value2 .... Returns the added list length. 3) Eject from the head: Lpop key. Returns the value of the element that was ejected. This action deletes the first element of the key list before returning it. 4) pops up from the tail: Rpop key. Returns the value of the element that was ejected. 5) Number of list elements: Llen key. Key does not exist return 0. 6) Get the list of sub-lists: Lrange start end. Returns a list of the first start to end elements. Contains start and end. Negative indexes are supported. -1 indicates the last element,-2 means reciprocal.
A second element. 7) Delete the specified value from the list: Lrem key, Count value. Delete key in this list, all values are elements of value, and only the count is removed. If there is count+1, then keep the last one.
Count does not exist or is 0, delete all. If count is greater than 0, the count is removed from the beginning to the end of count, and if count is less than 0, the number of ends from the tail is deleted. 8gets the specified index value: LINDEX key index. such as Lindex Key 0 is the first element of the list. Index can be a negative number. 9) Set the index and value: LSET key index value. This action simply modifies the specified key and specifies the value of index. If index does not exist, an error occurs. 10) preserve the fragment and delete the other: LTRIM key start end. Preserves all elements between start and end, including start and end. All other deletions. 11) Inserting elements into the list: Linsert key before/After value1 value2. Traversal from the head of the list, found to stop when the value is value1, insert value2, according to before
or after it is inserted in front of the value1 or behind. 12) Move one element of a list to another list: Rpoplpush list1 list2. Delete the right-hand element of the list list1 and insert the element to the left of the list list2. Atomic operation.
3. Set type set
Both the collection set and list of Redis can store multiple strings, but the list can store multiple identical strings, and the collection ensures that each string that it stores itself is different by using a hash table. Redis stores elements in an unordered manner.
Collection types are designed to facilitate manipulation and operation of multiple collections. Each element in the collection is different and has no sequential concept, and each element is and can only be a string. The common operation is to insert, delete, judge the collection and so on. The time complexity of the tail O (1). Intersection, set, and difference operations can be performed. The storage of a collection type in Redis is a hash list with a null value (these hashes have only keys but no key-related values).
1 2 3 4 5 6 7 8 9 collection of key Storekey. Sinnerstore and Sunionstore are similar. 10) Gets the element in the collection: Srandmember key [Count]. The parameter count is optional, if count does not exist, then one. Count is more than 0, then count elements are not duplicated.
Count is less than 0, it is altogether |count| 11) An element appears: SPOP key. An element is popped from the collection and deleted, returning the value of the element.
4. Hash type Hash
Redis hashes can store mappings between multiple key-value pairs , and as with strings, the stored redis is stored as a dictionary (associative array), and a key corresponds to a value. In a string type, value can only be a string. Then in the hash type, also called the hashing type, value corresponds to a dictionary (associative array). It can be understood that the value of the key corresponding to the hash type/hash type of Redisis a two-dimensional array. However, the value of the field can only be a string. This means that only two-dimensional arrays cannot have more dimensions.
the hash value can be either a string or a numeric value , and the user can perform self-increment and self-decrement operations on the numeric values of the hash store.
1) Assignment: Hset key field value. such as Hset user name Lane. Hset User Age 232) Take the value: Hget key field. For example, Hget user name, Get Lane. 3) Assign values to multiple fields of the same key: Hmset key field1 value1 field2 value2 ...4) Multiple fields of the same key value: Hmget key field1 fields2 ...5) Get all fields of key and all values: Hgetall key. If Hgetall user gets the name Lane age 23. Each return is a separate row. 6) field exists: Hexists key field. There is a return of 1, there is no return 07) Assign a value when the field does not exist: Hsetnx key field value. If the field below the key is not present, a field is established and the value is values. If field fields exist, do not execute the
Any action. Its effect is equal to Hexists +Hset. But the advantage of this command is atomic manipulation. High concurrency is not afraid. 8) self-increment n:hincreby key field increment. The self-increment type of the same string is no longer elaborated. 9) Delete field: DEL key field1 field2 ... Deletes one or more fields of the specified key. 10get field name only: Hkeys key. Similar to Hgetall, but only gets the field name and does not get the field value. 11Gets the field value only: Hvals key. Similar to Hgetall, but only gets the field value and does not get the field name. 12) Get the number of fields: Hlen key.
5. Ordered set type sorted set
Ordered collections and hashes are used to store key-value pairs: The key of an ordered set is called a member (member), each member is different, and the value of an ordered set is called a score (score), the score must be a floating-point number, and the ordered set is the only one within Redis that can access the element according to the member. You can also access the structure of the element based on the score and the sort of score.
The collection type is unordered, and each element is unique. Then the ordered set is ordered and each element is unique. the difference between an ordered collection type and a collection type is that an ordered collection is equipped with an attribute for each element: fractions. ordered collections are sorted by fractions. ordered collections are implemented using hash lists and jump tables . As a result, the operation of intermediate elements is fast compared to the list. The time complexity of the tail O (log (N)). an ordered collection type in a Redis data type is more resource-intensive than a list type in a Redis data type.
1) added: Zadd key Sorce1 value1 sorce2 value2 .... 2) Gets the score: Zscore key value. Gets the fractional value of the element in the ordered collection of keys. 3) Get a list of elements ranked in a range: Zranfge key start Stop [Withscore]. Gets a list of elements ranked between start and end, containing start and end2 elements.
One row per element. If there is a withscore parameter, a row of element values, one row of fractions. The time complexity is O (logn+m). If the score is the same, 0<0<a<z<a<Z. 4gets the element with the specified fractional range: Zrangebyscore key min Max [withscore] [LIMIT offset count]. Gets a list of elements that are fractions between min and Max. With two ends.
One row per element. If there is a withscore parameter, a row of element values, one row of fractions. If Min is greater than Max, the order is reversed. 5) Add a score to an element: Zincrby key increment value. The value of the specified ordinal collection is the fraction of the element of the number +increment. The score after the return value has changed. 6gets the number of elements in the collection: Zcard key. 7gets the number of elements in the specified fraction range: Zcount key min Max. 8Delete one or more elements: Zrem key value1 value2 ...9) Removes the element according to the rank range: Zremrangebyrank key start end. Deletes the elements that are ranked in start and end. 10Delete the element according to the fractional range: Zremrangebyscore key min Max. 11) Gets the element rank (positive order): Zrank key value. Gets the small-to-large rank of value in the collection. 12) Get the element rank (reverse): Zrevrank key value. Gets the value in the collection from the large to the small rank. 13) Intersection of ordered sets: Zinterstore storekey key1 Key2 ... [WEIGHTS weight [weight ...] [AGGREGATE sum| min| MAX]. Used to calculate the intersection of multiple collections,
The results are stored in Storekey. The return value is the number of elements in the Storekey. Aggregate is sum, the fraction of each element of the Storekey collection is the set score that participates in the calculation and. Min is the reference
The minimum value with the calculated score. Max is the maximum number of participating compute scores. WEIGHTS sets the weights for each collection, such as WEIGHTS 1 0.1. So each element of the set A is a fraction of a, set B, each
Element fraction *0.114) The set of ordered sets: Zunionstore storekey key1 kye2 ... [WEIGHTS weight [weight ...] [AGGREGATE sum| min| MAX]
5 types of data structures for Redis