Five common data types for Redis
1. String
Introduction
Strings are the most basic data type in Redis, and can store any type of data (such as text, numbers, binary data, etc.). The maximum length of each string can reach 512 MB.
Commonly used commands
SET key value
-
use: Set key
key
The value ofvalue
。 -
grammar:
SET key value
- Example:
SET username "john_doe"
GET key
-
use: Get key
key
value. -
grammar:
GET key
- Example:
GET username
DEL key
-
use: Delete the specified key
key
。 -
grammar:
DEL key
- Example:
DEL username
EXISTS key
-
use: Check key
key
Whether it exists. -
grammar:
EXISTS key
- Example:
EXISTS username
INCR key
-
use: Turn key
key
The value of 1 is added. If the key does not exist, it is initialized to 0. -
grammar:
INCR key
- Example:
INCR page_views
DECR key
-
use: Turn key
key
The value of 1 is reduced. If the key does not exist, it is initialized to 0. -
grammar:
DECR key
- Example:
DECR user_count
APPEND key value
-
use: Change the string value
value
Append to keykey
after the existing value. -
grammar:
APPEND key value
- Example:
APPEND username "_2023"
MSET key1 value1 key2 value2 …
- use: Set multiple key-value pairs at the same time.
-
grammar:
MSET key1 value1 key2 value2 ...
Example:
MSET key1 "value1" key2 "value2"
MGET key1 key2 …
- use: Get the values of multiple keys.
-
grammar:
MGET key1 key2 ...
- Example:
MGET key1 key2
SETEX key seconds value
-
use: Set key
key
The value ofvalue
, and inseconds
Expired in seconds. -
grammar:
SETEX key seconds value
- Example:
SETEX session:123 3600 "abc123" # set up1Expired after hours
Application scenarios
- Cache system: Used to store user session information, web page content, etc. to improve data access speed.
- counter: Implement simple counter functions, such as counting website visits, likes, etc.
- Token storage: Store user's identity token in the authentication system.
2. Hash
Introduction
Hash is a mapping that stores key-value pairs, suitable for storing objects. Each hash supports up to 2^32-1 fields, which are usually used to represent a complex object.
Commonly used commands
HSET key field value
-
use: is a hash table
key
Fields infield
Set valuevalue
。 -
grammar:
HSET key field value
- Example:
HSET user:1000 name "John Doe"
HGET key field
-
use: Get the hash table
key
Medium fieldsfield
value. -
grammar:
HGET key field
Example:
HGET user:1000 name
HGETALL key
-
use: Get the hash table
key
All fields in and their corresponding values. -
grammar:
HGETALL key
- Example:
HGETALL user:1000
HDEL key field
-
use: Delete the hash table
key
Fields infield
。 -
grammar:
HDEL key field
- Example:
HDEL user:1000 age
HINCRBY key field increment
-
use: Convert hash table
key
Medium fieldsfield
Add the value ofincrement
。 -
grammar:
HINCRBY key field increment
- Example:
HINCRBY user:1000 age 1
HKEYS key
-
use: Get the hash table
key
all field names in -
grammar:
HKEYS key
- Example:
HKEYS user:1000
HVALS key
-
use: Get the hash table
key
All field values in -
grammar:
HVALS key
- Example:
HVALS user:1000
HEXISTS key field
-
use: Check the hash table
key
Is there a field in itfield
。 -
grammar:
HEXISTS key field
- Example:
HEXISTS user:1000 name
HMSET key field1 value1 field2 value2 …
- use: Set the values of multiple fields at the same time.
-
grammar:
HMSET key field1 value1 field2 value2 ...
- Example:
HMSET user:1000 name "John Doe" age 30
HMGET key field1 field2 …
- use: Get the values of multiple fields in the hash table.
-
grammar:
HMGET key field1 field2 ...
- Example:
HMGET user:1000 name age
Application scenarios
- User information storage: Use hash to store multiple attributes of users (such as username, age, address, etc.) to facilitate quick query and update.
- Product Information Management: In the e-commerce system, the detailed information of the product (such as price, inventory, sales, etc.) is stored using hash.
3. List
Introduction
Lists are ordered collections of strings that support duplicate elements. The length of the list can reach 2^32-1 elements, which is suitable for implementing data structures such as queues and stacks.
Commonly used commands
LPUSH key value
-
use: Insert one or more values into the list
key
the head. -
grammar:
LPUSH key value [value ...]
- Example:
LPUSH tasks "Task 1"
RPUSH key value
-
use: Insert one or more values into the list
key
the tail of the -
grammar:
RPUSH key value [value ...]
- Example:
RPUSH tasks "Task 2"
LPOP key
-
use: Remove and return to the list
key
The first element. -
grammar:
LPOP key
- Example:
LPOP tasks
RPOP key
-
use: Remove and return to the list
key
The last element of . -
grammar:
RPOP key
- Example:
RPOP tasks
LRANGE key start stop
-
use: Get the list
key
Specify the elements within the range,start
andstop
is the index, and a negative number means the count starts from the end. -
grammar:
LRANGE key start stop
- Example:
LRANGE tasks 0 -1 # Get the entire task list
LTRIM key start stop
-
use: Trim the list
key
,reservestart
arrivestop
Elements within range. -
grammar:
LTRIM key start stop
- Example:
LTRIM tasks 0 4 # Only before5Elements
LINDEX key index
-
use: Get the list
key
Specify the index inindex
elements. -
grammar:
LINDEX key index
- Example:
LINDEX tasks 1 # Get the second task
LLEN key
-
use: Get the list
key
length. -
grammar:
LLEN key
- Example:
LLEN tasks
RPOPLPUSH source destination
-
use: Remove the list
source
The last element of the listdestination
the head. -
grammar:
RPOPLPUSH source destination
- Example:
RPOPLPUSH tasks completed_tasks
BRPOP key [key …] timeout
-
use: Block pop-up list
key
The last element is until timeout or a new element is available. -
grammar:
BRPOP key [key ...] timeout
- Example:
BRPOP tasks 5 # wait5Return in seconds
Application scenarios
- Message Queue: Use lists to implement task queues and support FIFO (first-in, first-out) logic.
- Time series data: Store user operation records or log information for chronological access.
4. Set
Introduction
Collections are unordered string collections that support adding, deleting and finding operations. Elements in the collection are unique, and repeated elements will be automatically ignored.
Commonly used commands
SADD key member [member …]
-
use: to the set
key
Add one or more members. -
grammar:
SADD key member [member ...]
- Example:
SADD myset "apple"
SREM key member [member …]
-
use: From the collection
key
Removes one or more members. -
grammar:
SREM key member [member ...]
- Example:
SREM myset "banana"
SMEMBERS key
-
use: Get the collection
key
All members in -
grammar:
SMEMBERS key
- Example:
SMEMBERS myset
SISMEMBER key member
-
use:Judge members
member
Is it in the collectionkey
middle. -
grammar:
SISMEMBER key member
- Example:
SISMEMBER myset "apple"
SINTER key1 key2 [key3 …]
- use: Returns the intersection of all sets given.
-
grammar:
SINTER key1 key2 [key3 ...]
- Example:
SINTER set1 set2
SUNION key1 key2 [key3 …]
- use: Returns the union of all collections given.
-
grammar:
SUNION key1 key2 [key3 ...]
- Example:
SUNION set1 set2
SDIFF key1 key2 [key3 …]
-
use: Return to the collection
key1
The difference between other sets. -
grammar:
SDIFF key1 key2 [key3 ...]
- Example:
SDIFF set1 set2
SPOP key [count]
-
use: Remove and return the collection
key
One or more random members in. -
grammar:
SPOP key [count]
- Example:
SPOP myset 2 # Randomly remove and return2Members
SCARD key
-
use: Get the collection
key
number of members. -
grammar:
SCARD key
- Example:
SCARD myset
SMOVE source destination member
-
use: to add members
member
From the collectionsource
Move to collectiondestination
。 -
grammar:
SMOVE source destination member
Example:
SMOVE myset other_set "apple"
Application scenarios
- Tag system: Use collections to store users’ interest tags to facilitate interest recommendations.
- Statistics of independent users: Statistics the number of independent visitors or active users of the website, and uses the deduplication feature of the collection.
5. Ordered Set
Introduction
An ordered set is a string collection where each element is associated with a score, and the elements in the set are ordered. Members are unique, but scores can be repeated.
Commonly used commands
ZADD key score member [score member …]
-
use: To orderly set
key
Add one or more members and their scores. -
grammar:
ZADD key score member [score member ...]
Example:
ZADD leaderboard 100 "player1"
ZRANGE key start stop [WITHSCORES]
-
use: Returns an ordered set
key
Members within the specified interval. -
grammar:
ZRANGE key start stop [WITHSCORES]
- Example:
ZRANGE leaderboard 0 -1 WITHSCORES # Get all players and their scores
ZREM key member [member …]
-
use: Remove ordered collections
key
One or more members of the -
grammar:
ZREM key member [member ...]
- Example:
ZREM leaderboard "player1"
ZRANK key member
-
use: Return to member
member
In an ordered collectionkey
Ranking in (start from 0). -
grammar:
ZRANK key member
- Example:
ZRANK leaderboard "player2"
ZCARD key
-
use: Returns an ordered set
key
number of members. -
grammar:
ZCARD key
- Example:
ZCARD leaderboard
ZSCORE key member
-
use: Get an ordered collection
key
Membersmember
score. -
grammar:
ZSCORE key member
Example:
ZSCORE leaderboard "player2"
ZREVRANGE key start stop [WITHSCORES]
-
use: Returns an ordered set in order from high to low
key
Members within the specified interval. -
grammar:
ZREVRANGE key start stop [WITHSCORES]
- Example:
ZREVRANGE leaderboard 0 -1 WITHSCORES # Get all players and their scores,From high to low by score
ZINCRBY key increment member
-
use: Ordered collection
key
Membersmember
Add the score ofincrement
。 -
grammar:
ZINCRBY key increment member
- Example:
ZINCRBY leaderboard 10 "player1" # player1Increased scores10
ZPOPMIN key [count]
-
use: Remove and return an ordered collection
key
One or more members with the lowest score. -
grammar:
ZPOPMIN key [count]
- Example:
ZPOPMIN leaderboard 1 # Remove and return the lowest score1Members
ZPOPMAX key [count]
-
use: Remove and return an ordered collection
key
One or more members with the highest score. -
grammar:
ZPOPMAX key [count]
- Example:
ZPOPMAX leaderboard 1 # Remove and return the highest score1Members
Application scenarios
- Ranking list: Score rankings used for games, popularity rankings on social media, etc.
- Time-sensitive data: For example, the real-time recommendation system dynamically updates the recommended content based on the user's behavior score.
Summarize
By learning more about Redis’s various data types and their commonly used commands, developers can more effectively take advantage of the power of Redis. In high concurrency and high performance application scenarios, correctly selecting and using Redis's data structures and commands will significantly improve the performance and scalability of your application. Hope this article helps you with Redis!
The above is personal experience. I hope you can give you a reference and I hope you can support me more.