redis data type and its use

Keywords: Redis Attribute JSON

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

Redis Chinese official website command document

1. string type data

  • set key value
  1. 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"
  1. Set multiple key values:
mset name tony habby computer age 22  //Set the key to name, hubby, age.
  1. 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 key value
  1. Append value to value:
append a1 test  //Add value to the key a1.
  • View key values
  1. 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.
  1. Get the values corresponding to multiple key s:
mget a1 a2 a3                                          

2. hash type

It is used to store the information of the object, the structure property of the object, and the value (string).

  • set key value
  1. Set individual properties:
hset a1 name tony  //Set the a1 key, the attribute is name, and the value is tony.
  1. Set multiple property values:
hmset a2 name tony age 42 //Set the a2 key, and the properties are name,age.
  • View key values
  1. Check how many attributes are contained in a hash key:
hkeys a1   //See how many properties are included in the a1 key
  1. Gets the value of a property in a key.
hget a2 name  //Gets the value of the property name in the a2 key.
  1. Gets the value of multiple properties.
hmget  a2 name age //Gets the value of the name and age properties in the a2 key.
  1. Get values for all properties:
hvals a2  //Gets all the values of all the properties of the a2 key.
Delete key values
  1. Delete the entire hash key and its value, and use the del command.
  2. To delete an attribute and its corresponding value:
hdel a2 name  //Delete the name attribute and its value of the a2 key.

3. [add: -] the key command is universal for various data types.

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. 

4.list type

The element type of the list is also String. Sort by insertion order.

Insert key value

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
To view elements in a list:

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
Modify key value:

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.
Delete the specified element:
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.
  1. 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;

  1. 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.

Type 5.set:

The element is String. The element is unique. It is an unordered collection.
There are no modifications to the collection.

  1. Additive elements
sadd a5 aa bb  //Add elements aa and bb to the set corresponding to the a5 key
  1. Get elements
smembers a5     //Gets all elements in the collection corresponding to the a5 key.
  1. Delete specified element
srem a5 aa   //Delete the aa element.

6.zset type

The element is of type String, unique and an ordered collection.
No modification.
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 ......
1. increase
zadd 		a6 		4 	aa 		5  bb 		3 	cc     //Add aa with a weight of 4 and bb with a weight of 5
2. get
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.

3. delete

Delete the specified element:

zrem  a6  cc  //Delete cc element
zremrangebyscore a6  4 7  
//Delete elements with weights between 4 and 7

Published 84 original articles, won praise 0, visited 1975
Private letter follow

Posted by no3dfx on Sat, 07 Mar 2020 17:52:59 -0800