redis is the data structure of key value. Each data is a key value pair.
The key type is string and cannot be repeated. There are five types of value:
1. String String type is the most basic data type in Redis, which is stored in binary form, so it can accept data of any format. The maximum length that can be accommodated is 512M. Image data, json object description. 2. hash hash 3. list / / can only store strings 4. the set / / element is unique and can only be a string 5. Ordered set zset / / the element is unique and ordered. It can only be a string
- Set single key value
Set key value: add if there is no key, and modify if there is one.
set name tony //Set key as name and value as "tony"
- Set multiple key values:
mset name tony habby computer age 22 //Set the key to name, hubby, age.
- Set the expiration time of key value:
setex age 10 24 //Set the key to age and the value to 24. It will expire in 10 seconds.
- Append value to value:
append a1 test //Add value to the key a1.
- View the corresponding value of key:
get name //get key gets the value of the key. If the key does not exist, nil is returned.
- Get the values corresponding to multiple key s:
mget a1 a2 a3
It is used to store the information of the object, the structure property of the object, and the value (string).
- Set individual properties:
hset a1 name tony //Set the a1 key, the attribute is name, and the value is tony.
- Set multiple property values:
hmset a2 name tony age 42 //Set the a2 key, and the properties are name,age.
- Check how many attributes are contained in a hash key:
hkeys a1 //See how many properties are included in the a1 key
- Gets the value of a property in a key.
hget a2 name //Gets the value of the property name in the a2 key.
- Gets the value of multiple properties.
hmget a2 name age //Gets the value of the name and age properties in the a2 key.
- Get values for all properties:
hvals a2 //Gets all the values of all the properties of the a2 key.
- Delete the entire hash key and its value, and use the del command.
- To delete an attribute and its corresponding value:
hdel a2 name //Delete the name attribute and its value of the a2 key.
keys * //View all keys. Support regular keys a* exists a1 //Check whether the a1 key exists. Return 1 exists, return 0 does not exist type a1 //Check the value type of the key. del a1 a2 //Delete a1,a2 keys and their value s expire a1 10 //There is no expiration time set. Use expire to set the expiration time for the key. ttl a1 //Check the effective time of the key.
The element type of the list is also String. Sort by insertion order.
Insert data from the left:
Insert data from the left in the reverse order of the actual list.
lpush a3 a,b,c //Insert data a,b,c from the left side of the list of a3 keys. ---->[c,b,a]
Insert data from right:
Insert in the same order as in the list.
rpush a3 11 22 //Insert data 11 and 22 from the a3 key
Insert data before the specified element:
linsert a3 before b 3 //Add an element B in front of the element B in the a3 key. ---->[....3,b...]
Insert data after the specified element:
linsert a3 after b 4 //Insert an element 4 after the b element
Negative numbers are supported, - 1 for the last element.
lrange a3 0 2 //See the list of elements in the a3 key from subscript 0 to subscript 2. lrange a3 0 -1 //Get all elements of the list with key a3
Set the element value for the specified index (subscript) location:
lset a3 1 zzz //Set or modify the element of the index position of list 1 in the a3 key to zzz.
lrem key count value //count indicates the number of times to remove, and positive and negative indicate the direction to remove. Value indicates the value to remove.
- Count > 0: remove from head to tail.
2 means two are removed from the head.
Remove element a twice from the head:
lrem a4 2 a;
- Remove 2 this a element from the tail
Count < 0: remove from tail to head.
-2 means to remove 2 from the tail.
lrem a4 -2 a
3. Remove all elements:
count = 0: remove all.
The element is String. The element is unique. It is an unordered collection.
There are no modifications to the collection.
- Additive elements
sadd a5 aa bb //Add elements aa and bb to the set corresponding to the a5 key
- Get elements
smembers a5 //Gets all elements in the collection corresponding to the a5 key.
- Delete specified element
srem a5 aa //Delete the aa element.
The element is of type String, unique and an ordered collection.
Each element is associated with a score of double type, which represents the weight and arranges the elements from small to large.
zadd key score1 member1 score2 member2 ......
zadd a6 4 aa 5 bb 3 cc //Add aa with a weight of 4 and bb with a weight of 5
zrange a6 0 -1 //Gets all elements in the collection of key a6. zrange a6 0 2 //Gets all element values with an index of 0 to 2. zrangebyscore a6 3 8 //Gets a value with a weight between 3 and 8.
zscore a6 cc //See what the weight of the cc element is.
Delete the specified element:
zrem a6 cc //Delete cc element zremrangebyscore a6 4 7 //Delete elements with weights between 4 and 7