This article will give you an in-depth understanding of the data structures in Redis and see the application scenarios of these data structures. I hope it will be helpful to you!
Redis data types and application scenarios
Redis is a Key-Value storage system written in ANSI C language.
The type of key is string. [Related recommendations: Redis video tutorial]
8 data types of value data types:
-
Common data types
- ##Uncommon data type
- bitmap bitmap type
- geo location type
- stream type
- NoteCommands in Redis ignore case, (set SET), keys do not ignore case (NAME name)
string string type
Redis’ String can express three types of values: string, integer, floating point number 100.01 is a six-digit string
Common commands
Command name | Command format | Command description |
set | set key value | Assignment |
get | get key | Get the value |
getset | getset key value | Get the value and assign the value |
mset | MSET key1 value1 key2 value2 .. keyN valueN | Set the values of multiple key to their corresponding values.
|
mget | MGET KEY1 KEY2 .. KEYN | Return the values of all (one or more) given keys |
EXPIRE | EXPIRE key seconds | Set the expiration time (seconds) of a key |
append | append key value | Append value to the end |
strlen | strlen key | Get the string length |
setnx | setnx key value | Assignment is used when value does not existset key value NX PX 3000 atomic operation, px sets the number of milliseconds
|
incr | incr key | Increment number |
incrby | incrby key increment | Increase the specified integer |
decr | decr key | Decrease the number |
decrby | decrby key decrement | Decrease the specified integer |
Application scenarios
1. Object cache
2. Single value cache
3. incr is used for optimistic locking incr: incremental number, which can be used to implement optimistic locking watch (transaction)
4. setnx is used for distributed locking when the value is not Use assignment when it exists and can be used to implement distributed locks
5, counters
6, and Web cluster session sharing
Examples of common methods
dockerRedis:0>keys *
dockerRedis:0>append testName 2
"1"
dockerRedis:0>exists testName
"1"
dockerRedis:0>append testName " 1234"
"6"
dockerRedis:0>get testName
"2 1234"
dockerRedis:0>set testName1 "testName1"
"OK"
dockerRedis:0>get testName1
"testName1"
dockerRedis:0>getset testName2 "testName2"
null
dockerRedis:0>get testName2
"testName2"
dockerRedis:0>strlen testName
"6"
dockerRedis:0>set incrTest "10"
"OK"
dockerRedis:0>incr incrTest
"11"
dockerRedis:0>get incrTest
"11"
dockerRedis:0>decr incrTest
"10"
dockerRedis:0>decrby incrTest 5
"5"
dockerRedis:0>mset set01 1 set02 2 set03 3
"OK"
dockerRedis:0>mget set01 set02 set03
1) "1"
2) "2"
3) "3"
Copy after login
list list type
The list type can store ordered and repeatable elements to get the elements near the head or tail The record is an extremely fast list with a maximum number of elements of 2^32-1 (4 billion)
Common commands
Command name |
Command format |
Command description |
##lpush | lpush key v1 v2 v3. .. | Insert list from the left side |
lpop | lpop key | Remove from the left side of the list |
rpush | rpush key v1 v2 v3 ... | Insert list from the right |
rpop | rpop key | Get it from the right side of the list |
lpushx | lpushx key value | Insert the value into the head of the list |
blpop | blpop key timeout | Take it from the left side of the list and block when the list is empty. You can set the maximum blocking time in seconds. |
llen | llen key | Get the number of elements in the list |
lrange | lrange key start end | Returns the elements in the specified range in the list. The range is specified by start and end |
##lset
lset key index value |
Set the element at the index position of the list to the value of value |
|
rpoplpush
rpoplpush key1 key2 |
Pop up from the right side of the key1 list And insert it to the left side of the key2 list |
|
rpushx
rpushx key |
Insert the value to the end of the list value |
|
brpop
blpop key |
Take it out from the right side of the list and block when the list is empty. You can set the maximum blocking timeout in seconds |
|
lindex
lindex key value |
Get the element whose subscript is index in the list, index starts from 0 index |
|
ltrim ltrim key start end |
Trim the list and only keep the start to end range end |
| ##brpoplpush
brpoplpush | Pop from the right side of the key1 list and insert it into the left side of the key2 list, which will block key1 key2 |
| linsert
linsert key BEFORE/AFTER pivot value | Insert value into the list before or after the value pivot |
|
Application scenario
1 , Stack (stack) = LPUSH LPOP
-
2, Queue (queue) = LPUSH RPOP
##3, Blocking MQ (blocking queue) = LPUSH BRPOP
4, user list, product list, comment list
set set type
Set: unordered, The maximum number of members in a unique element set is 2^32 - 1
Common commands
Command name
Command format
Command description |
|
| ##sadd
sadd key value1 value2 ....
Store elements into the collection key. If the element exists, it will be ignored. If the key does not exist, create a new one. |
| srem | srem key value1 value2 ....
Delete elements from the collection key |
| smembers | smembers key
Get all elements in the collection |
| spop | spop key count
Select count elements from the set key, and delete the elements from the key |
| srandmember | srandmember key count
Select count elements from the set key, and the elements will not be deleted from the key |
| scard | scard key
Get the number of elements in the collection key |
| sismember | sismember key member
Judge whether the member element exists in the collection key |
| sinter | sinter key1 key2 key3
Find the intersection of multiple sets |
| sdiff | sdiff key1 key2 key3
Find the difference of multiple sets |
| sunion | sunion key1 key2 key3
Find the difference of multiple sets Union |
|
Application Scenario
zset ordered set type
SortedSet(ZSet) Ordered set: The elements themselves are unordered and non-repeating.
Each element is associated with a score (score), which can be sorted by score. The score can be repeated.
Common commands
Command name | Command format | Command description |
##zadd
zadd key score1 member1 score2 member2 ... |
Add scored elements to the ordered set key |
|
zrem
zrem key mem1 mem2.... |
From Delete elements from the ordered set key |
|
zcard
zcard key |
Get the number of elements in the ordered set |
|
zcount
zcount key min max |
Returns the number of elements in the collection whose score value is in the [min,max] interval |
|
zincrby
zincrby key increment member |
is the score of the element member in the ordered set key plus increment |
| ##zscore
zscore key member | Return the score of the element member in the ordered set key |
| zrank
zrank key member | Get the score of the member in the set Ranking (from small to large by score) |
| zrange
zrange key start end | Get the ordered set key in positive order from start subscript to The element with stop subscript |
| zrevrank
zrevrank key member | Get the ranking of the member in the set (from large to small by score) |
| zrevrange
zrevrange key start end | Get the elements of the ordered set key from the start subscript to the stop subscript in reverse order |
|
Application scenarios
Click rankings, sales rankings, attention rankings
Redis hash is a mapping table of string type fields and values, which provides mapping of fields and field values. Each hash can store 2^32-1 key-value pairs (more than 4 billion).
Advantages
-
1. Similar data are classified and integrated for storage to facilitate data management
-
2. Compared with string operations, it consumes less memory and CPU.
-
3. Compared with string storage, it saves space.
Disadvantages
-
1. The expiration function cannot be used on fields, but can only be used on keys
-
2. The Redis cluster architecture is not suitable for large-scale use
Common commands
##Command name
Command format
Command description |
|
| ##hset
hset key field value
Storing the key value of a hash table key |
| hmset | hmset key field1 value1 field2 value2
In a hash table Store multiple key-value pairs in key |
| hget | hget key field
Check whether a field exists |
| hmget | hmget key field1 field2 ...
Get a field value |
| hsetnx | hsetnx key field value
Storage the key value of a non-existent hash table key |
| hexists | hexists key filed
Determine whether filed Exist |
| hgetall | hgetall key
Get multiple field values |
| hdel | hdel key field1 field2...
Delete the specified field |
| hincrby | hincrby key field increment
Specify the field since Increase increment |
| hlen | hlen key
Get the number of fields |
|
| Application scenarios
Object cache
- Shopping cart operation
- bitmap bitmap type Bitmap performs bit operations to represent the value or status corresponding to an element through a bit, and the key is the corresponding element itself. Bitmap itself will greatly save storage space.
Commonly used commands
Command name
Command formatCommand description |
|
| setbit
setbit key offset value
Set the bit value of key at offset (can only be 0 or 1). |
| getbit | getbit key offset
Get the bit value of key at offset |
##bitcount |
bitcount key |
Get the number of key bits that are 1
|
bitpos |
bitpos key value |
Return the first index value that is set as a bit value
|
bitop |
bitop and[or/xor/not] destkey key [key ...] |
Perform logical operations on multiple keys and store them in destkey
|
Application Scenario
- 1. Users check in every month, the user ID is key, and the date is used as offset 1 to indicate check-in
- 2. Statistics are active User, the date is the key, the user ID is offset 1, which means active
- 3. Query the user's online status, the date is the key, the user ID is offset 1, it means online
geolocation type
geo is used by Redis to process location information. Officially used in Redis3.2. Mainly using Z-order curve, Base32 encoding and geohash algorithm
Common commands
Command name |
Command format |
Command description |
|
|
|
##geoadd | geoadd key longitude latitude member name 1 longitude 1 latitude 1 member name 2 longitude 2 latitude 2 ... | Add geographic coordinates |
geopos | geopos key member name 1 member name 2... | Return member latitude and longitude |
geodist | geodist key member 1 member 2 unit | Calculate the distance between members |
georadiusbymember | georadiusbymember key member value unit count number asc [desc] | Find nearby members based on members |
geohash | geohash key member name 1 member name 2... | Return standard geohash string |
##Application scenario
1. Record geographical location
2. Calculate distance
3. Find "people nearby"
stream data flow type
stream is a new data structure after Redis5.0, used for persistent message queues .
Almost meets all the content of the message queue, including:
Serialization generation of message ID
- Message traversal
- Blocking of messages And non-blocking reading
- Group consumption of messages
- Processing of unfinished messages
- Message queue monitoring
- Each Stream has a unique The name, which is the key of Redis, is automatically created when the xadd command is used for the first time to append a message
Application scenarios
Usage of message queue
More For programming-related knowledge, please visit: Introduction to Programming! !
|
The above is the detailed content of In-depth analysis of the data structure in Redis and talk about application scenarios. For more information, please follow other related articles on the PHP Chinese website!