Data structures for Redis

Source: Internet
Author: User

Tags: Picture set share judge cond structure 3.3 data structure Ted

Data structures for Redis:
    • Redis is a data structure for key-value, and each data is a key-value pair
    • The type of the key is a string
    • Note: Keys cannot be duplicated
The value of Redis is divided into five types:
    1. Strings string
    2. Hashed Hash
    3. Lists List
    4. Set Set
    5. Ordered set Zset
1. String type
    • The string type is the most basic type of data storage in Redis, which is binary safe in Redis, which means that the type can receive any type of data, such as JPEG image data or JSON object description information. The value of the string type in Redis can accommodate up to 512M of data length
1.1 Save

If the key set does not exist, it is added if the key set already exists or is modified

Example 1: Setting a key value
Set Key Value

Set the key to the name value of Java data


Example 2: Set the key to AA value for AA with an expiry time of 3 seconds of data
3 AA

Example 3: Set the key ' A1 ' value to ' Python ', set the key to ' A2 ' value for ' Java ' set to ' A3 ', value ' C '
Mset key1 value1 key2 value2 ....

    • Append value
Key Value
Example 4: Append the value ' hahaha ' to the A1 key


1.2 Get get: Gets the value according to the key, if this key does not exist, returns nil
Example 5: Get the value of the key ' A1 '


Get multiple values based on multiple keys
Mget key1 Key2 ...
Example 6: Get the value of A1, A2, A3


2-Key command
    • Lookup key parameters support regular expressions
Keys pattern
Example 1: See all the Keys


Example 2: See the key with a in the name


Determines whether the key exists, returns 1 if present, does not exist return 0
Exists Key1
Example 3: See if the key ' A1 ' exists

View the type of value corresponding to the key
Example 4: View the value type of the key A1, one of the five types supported by Redis

Delete keys and their corresponding values
Del key1 Key2 ...
Example 5: Delete Keys A1, A2, A3


Set expiration, in seconds if no expiration time is specified, persists until it is deleted by using Del
Key seconds
Example 6: Setting the key ' A1 ' expires at 3 seconds
' A1 ' 3


View the effective time, in seconds
Example 7: Viewing the expiry time of a BB


3. Hash type
    • Hash is used to store objects, the structure of objects is properties, values, using Redis storage object information
    • The type of the value is string
3.1 Add, modify set individual properties
Key field value
Example 1: Setting the key for user's property name is Jack


Example 2: Setting the property of the key U2 name is Tom, attribute age is 11


3.2 Get
    • Get
Gets all properties of the specified key
Example 3: Get all properties of U2


Gets the value of a property
Key Field
Example 4: Get the value of the key U2 property ' name '


Get the values of multiple properties

key field1 field2 ...
Example 5: Get the value of the key U2 property ' name ', ' age '


Get the value of all properties
Example 6: Get the value of all properties of key ' U2 '


3.3 Delete
    • Delete entire hash key and value, use del command
    • Delete attribute, property corresponding value will be deleted together
Hdel key1 field1 Field2

Example 7: Delete the attribute ' age ' of the key ' U2 '

Hdel U2 Age


4. List type
    • The element property of the list is string
    • Sort by Insert Order
4.1 Increased insert data Lpush (left) inserted from left
Key value1 value2
Example 1: Adding Data A, B, C to the left of the list of keys ' A1 '

Inserting data from the right
key value1 value2 ...
Example 2: Adding data 0, 1 to the right of the list of keys ' A1 '

Inserts a new element before or after the specified element
key before or after existing element new element
Example 3: Add ' 3 ' before the element ' B ' in the list of keys ' A1 '

4.2. Get
    • Returns the element within the specified range of the list
      • Start, stop pseudo-element subscript index
      • The index starts from the left and the first element is 0
      • The index can be negative, indicating that the technique is started from the tail, such as 1 for the last element
Key start stop
Example 4: Get a list of all values with the key ' A1 '
0 - 1


Sets the element at the specified index position
    • The index starts from the left and the first element is 0
    • The index can be a negative number, which means that the trailing start count, such as 1, represents the last element
Key Index value
Example 5: The value of the element labeled 1 in the list with the Modifier key ' A1 ' is ' Z '


4.3 delete
    • Delete the specified element
      • Removes the element in the list that has a value of count number before
      • Count >0: Remove from head to tail
      • Count<0: Remove from tail to head
      • Count=0: Remove All
Key Count Value
Example 6.1: Append elements ' A ', ' B ', ' A ', ' B ', ' A ', ' B ' to the list ' a ', and delete two ' B ' from the right of the list

5 set Type
    • Unordered collection
    • element is of type string
    • Elements are unique, non-repeating
    • Description: No modification action for collection
5.1 Increaseadding elements
Key Member1 member2
Example 1: Adding elements ' Alex ', ' Egon ', ' Wusir ' to the collection of key ' A3 '


5.2. Get back all elements in Example 2: Get all the elements in the key ' A3 ' collection
Smember A3

5.3. Delete the specified element
Example 3: Delete the key ' A3 ' in the collection of elements ' Wusir '


6, Zset
    • Sorted set, ordered set
    • element is of type string
    • Elements are unique, non-repeating
    • Each element is associated with a double type of score, representing weights, and the elements are sorted from small to large by weight
    • Description: No modification action
6.1 Increase  
key score1 member1 score2 member2 ...
Example 1: Add elements ' jing ', ' Eva ', ' Jin ' to the collection of key ' A4 ', respectively, set weights of 4, 5, 3,
4 5 6 Jin

6.2 Get
    • Returns the element of the specified range
    • Start, stop is the subscript index of the element
    • The index can start from the left and the first element is 0
    • The index can be a negative number, which represents the count starting at the tail. If 1 means the last element
Key start stop


Returns the elements of the score value between Min and Max
Key min Max
Example 3: Get the member of the key ' A4 ' in the collection of weights between 4 and 6


Returns the score value of the member member
Key Member
Example 4: Get the weight of the element in the collection of key ' A4 ' as Eva


6.3 Delete Deletes the specified element
Key Member1member2
Example 5: Delete the element ' Eva ' in the collection ' A4 ' to remove weights in the specified range of elements
Key min Max
Example 6: Deleting an element in the collection ' A4 ' with weights between 5 and 6




Data structures for Redis

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: