Redis series of data structures

Redis data types and application scenarios

Redis is a key-value storage system written in ANSI C language. Key is of type string. Value Data type Eight data types are available

  • Common data types

    • String String type

    • List List type

    • Set set type

    • Sortedset (zset) Type of an ordered set

    • Hash type

  • Unusual data types

    • Bitmap Type of the bitmap

    • Geo Indicates the location type

    • The stream type

  • Pay attention to

    Redis command is case insensitive, (set set), key is case insensitive (NAME NAME)

String String type

Redis String can represent three value types: String, integer, and floating point 100.01 is a six-bit String

Common commands

Name of the command The command format Command description
set set key value The assignment
get get key The values
getset getset key value Evaluate and assign
append append key value Append to the tail
strlen strlen key Get the length of the string
incr incr key Increasing Numbers
incrby incrby key increment Increments the specified integer
decr decr key Decreasing Numbers
decrby decrby key decrement Decrement the specified integer
setnx setnx key value Assignment is used when value does not exist

Set key value NX PX 3000 atomic operation, PX set the number of milliseconds
mset MSET key1 value1 key2 value2 .. keyN valueN Set up multiplekeyThe values of are their corresponding values.
mget MGET KEY1 KEY2 .. KEYN Returns the values of all (one or more) given keys

Application scenarios

  • 1. Keys and commands are strings

  • 2. Plain assignment

  • 3, INCR for optimistic lock INCR: incremental number, can be used to implement optimistic lock watch(transaction)

  • 4, setnx for distributed lock when the value does not exist, using assignment, can be used to achieve distributed lock

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
"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 the code

List List type

List List types can store ordered, repeatable elements retrieving records near the head or tail is extremely fast The number of elements in a list is up to 2^32-1 (4 billion)

Common commands

Name of the command The command format Command description
lpush lpush key v1 v2 v3 … Insert the list from the left
lpop lpop key From the left side of the list
rpush rpush key v1 v2 v3 … Insert the list from the right
rpop rpop key Take it from the right side of the list
lpushx lpushx key value Inserts the value into the list header
blpop blpop key timeout From the left side of the list. Blocks when the list is empty. You can set the maximum blocking time in seconds
llen llen key Gets the number of elements in the list
lrange lrange key start end Returns the element in the list for the interval specified by start and end
lset lset key index value Set the element at index in the list to the value of value
rpoplpush rpoplpush key1 key2 Pops from the right side of the KEY1 list and inserts into the left side of the KEY2 list
rpushx rpushx key Inserts the value at the end of the list value
brpop blpop key Block when the list is empty. You can set the maximum block time (timeout) in seconds
  lindex lindex key Gets the index element in the list with index starting at 0 index
ltrim ltrim key start Trim the list, leaving only the start to end interval end
brpoplpush brpoplpush Popping from the right of the KEY1 list and inserting it to the left of the Key2 list blocks key1 key2
linsert linsert key BEFORE/AFTER pivot value Inserts a value into the list before or after the value pivot

Application scenarios

List orders can be used as stacks and queues

2. Can be used for all kinds of lists, such as user lists, product lists, review lists, etc.

Examples of common Methods

Set set type

Set: The largest number of members in an unordered, unique Set of elements is 2^ 32-1

Common commands

Name of the command The command format Command description
sadd sadd key mem1 mem2 …. Adds a new member to the collection
srem srem key mem1 mem2 …. Deletes the specified member from the collection
smembers smembers key Gets all the elements in the collection
spop spop key Returns a random element in the collection and deletes the element
srandmember srandmember key Returns a random element in the collection without deleting it
scard scard key Gets the number of elements in the collection
sismember sismember key member Determines whether the element is in the collection
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 union of multiple sets

Application scenarios

Applies to data structures that cannot be repeated and do not require sequence, such as following users, and can be randomly raffled through SPOP

Examples of common Methods

Zset Ordered collection type

SortedSet(ZSet) Ordered set: the element itself is unordered and not repeated. Each element is associated with a score (score), which can be sorted by score and repeated

Common commands

Name of the command The command format Command description
zadd zadd key score1 member1 score2 member2 … Adds a new member to an ordered collection
zrem zrem key mem1 mem2 …. Deletes the specified member from an ordered collection
zcard zcard key Gets the number of elements in an ordered collection
  zcount zcount key min max
zincrby zincrby key increment member Add increment to member score of set
zscore zscore key member Add increment to member score of set
zrank zrank key member Get the ranking of members in the set (from small to large)
zrange zrange key start end Gets the specified interval members of the set in fractional ascending order
zrevrank zrevrank key member Get the ranking of members in the set (from highest to lowest)
 zrevrange   zrevrange key start end Gets the specified interval members of the collection, decreasing by fraction

Application scenarios

Since it can be sorted by score, it is suitable for various leaderboards. For example: click leaderboard, sales leaderboard, attention leaderboard and so on

Examples of common Methods

Hash type

Redis Hash is a mapping table of fields and values of type string that provides mapping of fields and field values. Each hash can store 2^ 32-1 key-value pairs (more than 4 billion).

Common commands

Name of the command The command format Command description
hset hset key field value Assignment, new or modified without distinction
hmset hmset key field1 value1 field2 value2
hsetnx hsetnx key field value Batch assignment
hexists hexists key filed Field assignment, if filed exists, the field is not operated
hget hget key field Check whether a field exists
hmget hmget key field1 field2 … Gets a field value
hgetall hgetall key Gets multiple field values
hdel hdel key field1 field2… Delete a specified field
hincrby hincrby key field increment Increment specifies that the field is incremented
hlen hlen key Get the number of fields

Application scenarios

Storage of objects, mapping of table data

Examples of common Methods

Bitmap Type of the bitmap

A bitmap is a bit operation that uses a bit to represent the value or state of an element, and the key is the element itself. Bitmaps themselves are huge storage savings.

Common commands

Name of the command The command format Command description
setbit setbit key offset value Set the key’s bit at offset (0 or 1 only).
getbit getbit key offset Get the key’s bit at offset
bitcount bitcount key Get the number of 1 bits for key
bitpos bitpos key value Returns the first index set to a bit value
bitop bitop and[or/xor/not] destkey key [key …] Perform logical calculation on multiple keys and store them in destkeys

Application scenarios

  • 1. The user checks in every month. The user ID is key, and the date is offset 1
  • 2. Count active users. If the date is key and the user ID is offset 1, active users are counted
  • 3. Query the online status of the user. If the date is key and the user ID is offset 1, the user is online

Examples of common Methods

Geo Indicates the location type

Geo is used by Redis to process location information. Officially used in Redis3.2. It utilizes z-order curves, Base32 encoding, and geohash algorithms

Common commands

Name of the command The 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… Returns the latitude and longitude of the member
geodist Geodist Key Member 1 Member 2 units Calculates the distance between members
georadiusbymember Georadiusbymember Key Count AsC [desc] Find nearby members by member
geohash Geohash Key Member Name 1 Member Name 2… Returns the standard GeoHash string

Application scenarios

2. Calculate the distance. 3.

Examples of common Methods

Stream Indicates the data flow type

Stream is a data structure added after Redis5.0 for a persistent message queue.

Almost everything a message queue has, including:

  • Serialized generation of message IDS
  • Message traversal
  • Blocking and non-blocking reads of messages
  • Grouping consumption of messages
  • Processing of incomplete messages
  • Message queue monitoring

Each Stream has a unique name, which is the key of Redis, created automatically when the xadd directive is used to append messages for the first time

Common commands

Application scenarios

Use of message queues

Examples of common Methods