Redis data type

brief introduction

Redis is one of the most popular NoSQL databases. It is an open-source key value pair storage database written in ANSI C, containing a variety of data structures, supporting network, memory based and optional persistence.

How to install Redis?

Generally, the data types that Redis can store are String, Hash, List, SortedSet (zSet), Set.

I String -- string key value

String is the most commonly used type in Redis. It is a sequence composed of bytes. It is binary safe in Redis, which means that this type can accept data in any format. The maximum length of 512MB data can be accommodated.


//Add element
//ex:Second expiration time,nx:The key cannot be set successfully until it does not exist,xx The key can only be set successfully when it exists
set key value [ex seconds] [px milliseconds] [nx|xx]

//Get value
get key

//Batch setting value
mset key value [key value ...]

//addition    Corresponding key The value increases by 1. Without this key Value, automatically create and assign 1 to you
//corresponding key If the value is not a number, an error will be reported((error) ERR value is not an integer or out of range)
incr key

//subtraction    Corresponding key The value decreases by 1 if there is no such value key Value, which is automatically created and assigned to you-1
//corresponding key If the value is not a number, an error will be reported((error) ERR value is not an integer or out of range)
decr key

//obtain key corresponding value String length of
strlen key

//Return to original value
getset key value

//Sets the character at the specified position
setrange key offeset value
 set redis pest
 setrange redis 0 b
 get redis
//Get partial string
getrange key start end 

II Hash -- key field value

So there are multiple maps for a key. The Hash in Redis can be regarded as a map container with String key and String value. Multiple key values can be stored on one key. Each Hash can store 4294697295 key value pairs.


//Set value
hset key field value

//Get value
hget key field

//delete field
hdel key field [field ...]

//calculation field number
hlen key

//Batch setting or obtaining field-value
hmset key field value [field value ...]
hmget key field [field ...]

//judge field Does it exist
hexists key field

//Get all field
hkeys key

//Get all value
hvals key

//Get all field-value
hgetall key

//Self increasing in some form( Int) increment Incremental
//corresponding value Must be int,Otherwise, an error is reported((error) ERR hash value is not an integer)
hincrby key field increment

//Self increasing in some form( Float) increment Incremental
//corresponding value Must be float,Otherwise, an error is reported((error) ERR hash value is not a valid float)
hincrbyfloat key field increment

//calculation value String length of
hstrlen key field

III Set set

Redis collections are unordered and cannot be repeated. The unordered data here cannot be repeated. As with lists, it is efficient to insert and delete and determine whether an element exists. The greatest advantage of Set is that it can perform intersection union difference Set operation. The maximum number of elements a Set can contain is 4294967295.


//Add element
sadd key element ...

//Delete element
srem key element [element ...]
//Returns the number of successfully deleted elements//Calculate the number of elements
scard key

//Determine whether the element is in the collection
sismember key element

//Returns a specified number of elements from a collection at random
srandmember key [count]

//Pop up elements randomly from the collection (it can be understood as randomly deleting elements)
spop key [count]

//Get all elements
smembers key

//Intersection of multiple sets
sinter key [key ...]

//Union of multiple sets
sunion key [key ...]

//Find the difference set of multiple sets (front) key corresponding value Than the back key corresponding value (extra value)
sdiff key [key ...]

//Save the results of intersection, union and difference sets (save the results as a new one) set (medium)
sinterstore destination key [key ...]
suionstore destination key [key ...]
sdiffstore destination key [key ...]

IV List list

Redis's List allows users to push and pop up elements from both ends of the sequence. The List is an orderly and repeatable sequence composed of multiple string values. It is a linked List structure. Therefore, the time complexity of adding elements to both ends of the List is 0 (1). The closer to the two ends of the List, the faster the speed of obtaining elements. This means that even for a List with tens of millions of elements, it is faster to get 10 records at the head or tail. The maximum number of elements a List can contain is 4294967295.


//Insert element from right
rpush key value [value ...]

//Insert element from left
lpush key value [value ...]

//Insert an element before or after an element
linsert key before|after pivot value

//Gets the list of elements within the specified range
lrange key start end

//Gets the element of the index subscript specified in the list
lindex key index

//Get list length
llen key

//Pop up elements from the left side of the list (that is, delete and return the popped value)
lpop key

//Pop up from the right side of the list (i.e. delete, return the popped value)
rpop key

//Delete the specified element,count>0 From left to right, count<0 Right to left,count=0,Delete all
//value If you specify the value to be deleted, there will be duplicate values, so you should judge whether to find the deleted value from the left or the right, or delete all
lrem key count value

//Trim list by index range
ltrim key start end

V SortedSet(zSet)

There is order and cannot be repeated. The index here is unique, but the data can be repeated. Similar to set, it is a collection of strings, and duplicate members are not allowed to appear in a set. The difference between them is that each member in the ordered set will have a score associated with it. Redis uses the score to sort the members in the set from small to large. Although members in an ordered set must be unique, scores can be repeated.


//Add member
zadd key score member

//Calculate the number of members
zcard key

//Calculate a member's score,If the member does not exist,Then return nil
zscore key member

//Calculate the ranking of members,zrank Is the score from low to high,zrevrank From high to low
zrank key member
zrevrank key member

//Delete member
zrem key member

//Increase members' scores
zincrby key increment member

//Returns the members of the specified ranking range
//Return from low to high
zrange key start end [withscores]
//Return from high to low
zrevrange key start end [withscores]

//Returns the members of the specified score range
zrangebyscore key min max [withscores] [limit offset count]
zrevrangebyscore key max min [withscores] [limit offset count]

//Returns the number of members in the specified score range
zcount key min max

//Deletes ascending elements within the specified ranking
zremrangebyrank key start end

//Delete members of the specified score range
zremrangebyscore key min max

//Find the intersection of two sets( destination: Save the result as a new zSet (first name)
zinterstore destination numkeys key [key ...] [weights weight [weight ...]]
[aggregate sum|min|max]

//Union set( destination: Save the result as a new zSet (first name)
zunionstore destination numkeys key [key ...] [weights weight [weight ...]]
[aggregate sum|min|max]


Tags: Redis

Posted by max101 on Thu, 14 Apr 2022 06:42:15 +0930