1.String
Common commands: set,get,strlen,exists,decr,incr,setex
Application scenario: generally used in scenarios that need to be counted, such as the number of visits by users and the number of likes forwarded by hot articles
127.0.0.1:6379> set key value #Set a value of type key value OK 127.0.0.1:6379> get key # Obtain the corresponding value according to the key "value" 127.0.0.1:6379> exists key # Determine whether a key exists (integer) 1 127.0.0.1:6379> strlen key # Returns the length of the string value stored by the key. (integer) 5 127.0.0.1:6379> del key # Delete the value corresponding to a key (integer) 1 127.0.0.1:6379> get key (nil)
Batch settings:
127.0.0.1:6379> mset key1 value1 key2 value2 # Batch setting of key value type values OK 127.0.0.1:6379> mget key1 key2 # Get the value s corresponding to multiple key s in batch 1) "value1" 2) "value2"
Counter (can be used when the content of the string is an integer):
127.0.0.1:6379> set number 1 OK 127.0.0.1:6379> incr number # Increment the numeric value stored in the key by one (integer) 2 127.0.0.1:6379> get number "2" 127.0.0.1:6379> decr number # Subtract the numeric value stored in the key by one (integer) 1 127.0.0.1:6379> get number "1"
Set expiration:
127.0.0.1:6379> expire key 60 # Data expires after 60s (integer) 1 127.0.0.1:6379> setex key 60 value # Data expires after 60s (setex:[set] + [ex]pire) OK 127.0.0.1:6379> ttl key # How long will the data expire (integer) 56
2.List
List is a linked list. It is easy to insert and delete data elements, and can flexibly adjust the length of the linked list
Common commands: rpush,lpop,lpush,rpop,lrange, len, etc.
Application scenario: publish and subscribe or message queue
Queue implementation through rpush/lpop:
127.0.0.1:6379> rpush myList value1 # Add an element to the header (right) of the list (integer) 1 127.0.0.1:6379> rpush myList value2 value3 # Add multiple elements to the header (rightmost) of the list (integer) 3 127.0.0.1:6379> lpop myList # Take out the tail (leftmost) element of the list "value1" 127.0.0.1:6379> lrange myList 0 1 # View the list of corresponding subscripts. 0 is start and 1 is end 1) "value2" 2) "value3" 127.0.0.1:6379> lrange myList 0 -1 # View all elements in the list, - 1 means the last one 1) "value2" 2) "value3"
Realize stack through rpush/rpop:
127.0.0.1:6379> rpush myList2 value1 value2 value3 (integer) 3 127.0.0.1:6379> rpop myList2 # Take out the header (rightmost) element of the list "value3"
View the list elements corresponding to the subscript range through lrange:
127.0.0.1:6379> rpush myList value1 value2 value3 (integer) 3 127.0.0.1:6379> lrange myList 0 1 # View the list of corresponding subscripts. 0 is start and 1 is end 1) "value1" 2) "value2" 127.0.0.1:6379> lrange myList 0 -1 # View all elements in the list, - 1 means the last one 1) "value1" 2) "value2" 3) "value3"
With lrange command, you can realize paging query based on list
Check the length of the linked list through llen:
127.0.0.1:6379> llen myList (integer) 3
3.hash
hash is similar to jdk1 The internal implementation of HashMap before 8 is also similar (array + linked list). Especially suitable for storing objects
Common commands: hset,hmset,hexists,hget,hgetall,hkeys,hvals
Application scenario: storage of object data in the system.
127.0.0.1:6379> hmset userInfoKey name "guide" description "dev" age "24" OK 127.0.0.1:6379> hexists userInfoKey name # Check whether the field specified in the value corresponding to the key exists. (integer) 1 127.0.0.1:6379> hget userInfoKey name # Gets the value of the specified field stored in the hash table. "guide" 127.0.0.1:6379> hget userInfoKey age "24" 127.0.0.1:6379> hgetall userInfoKey # Gets all fields and values of the specified key in the hash table 1) "name" 2) "guide" 3) "description" 4) "dev" 5) "age" 6) "24" 127.0.0.1:6379> hkeys userInfoKey # Get key list 1) "name" 2) "description" 3) "age" 127.0.0.1:6379> hvals userInfoKey # Get value list 1) "guide" 2) "dev" 3) "24" 127.0.0.1:6379> hset userInfoKey name "GuideGeGe" # Modify the corresponding value of a field 127.0.0.1:6379> hget userInfoKey name "GuideGeGe"
4.set
The set type in redis is an unordered set, and the elements in the set have no order. The operations of intersection, union and difference set can be easily realized based on set. For example, you can save all the followers of a user in a collection and all their fans in a collection. Redis can easily realize functions such as common concern, common fans and common preferences. This process is also the process of finding intersection.
Common commands: Sadd, spop, smembers, sismber, scard, sinterstore, Sunion
Application scenario: the data to be stored cannot be repeated, and the intersection and union of multiple data sources need to be obtained
127.0.0.1:6379> sadd mySet value1 value2 # Add elements (integer) 2 127.0.0.1:6379> sadd mySet value1 # Duplicate elements are not allowed (integer) 0 127.0.0.1:6379> smembers mySet # View all elements in set 1) "value1" 2) "value2" 127.0.0.1:6379> scard mySet # View the length of the set (integer) 2 127.0.0.1:6379> sismember mySet value1 # Check whether an element exists in the set. Only a single element can be received (integer) 1 127.0.0.1:6379> sadd mySet2 value2 value3 (integer) 2 127.0.0.1:6379> sinterstore mySet3 mySet mySet2 # Get the intersection of mySet and mySet2 and store it in mySet3 (integer) 1 127.0.0.1:6379> smembers mySet3 1) "value2"
5.sorted set
Compared with set, sorted set adds a weight parameter score, so that the elements in the set can be arranged orderly according to score
Common commands: zadd,zcard,zscore,zrange,zrevrange,zrem
Application scenario: a scenario where data needs to be sorted according to a certain weight. For example, in the live broadcasting system, the real-time ranking information includes the list of online users in the live broadcasting room, various gift ranking lists, bullet screen messages (which can be understood as message ranking lists according to the message dimension) and other information.
127.0.0.1:6379> zadd myZset 3.0 value1 # Add elements to sorted set with 3.0 as weight (integer) 1 127.0.0.1:6379> zadd myZset 2.0 value2 1.0 value3 # Add multiple elements at a time (integer) 2 127.0.0.1:6379> zcard myZset # View the number of elements in the sorted set (integer) 3 127.0.0.1:6379> zscore myZset value1 # View the weight of a value "3" 127.0.0.1:6379> zrange myZset 0 -1 # Sequentially output the elements of a certain range. 0 - 1 means to output all elements 1) "value3" 2) "value2" 3) "value1" 127.0.0.1:6379> zrange myZset 0 1 # Sequentially output the elements of a certain range. 0 is start and 1 is stop 1) "value3" 2) "value2" 127.0.0.1:6379> zrevrange myZset 0 1 # Output the elements of a range in reverse order. 0 is start and 1 is stop 1) "value1" 2) "value2"
6.bitmap
bitmap stores consecutive binary numbers (0 and 1)
Common commands: setbit, getbit, bitcount, bitop
Application scenario: suitable for saving status information (such as check-in, login...)
Usage scenario 1: user behavior analysis many websites need to study the content you like in order to analyze your preferences.
# Record that you liked little sister 001 127.0.0.1:6379> setbit beauty_girl_001 uid 1
Usage scenario 2: Statistics of active users
Use time as the key, and then the user ID is offset. If it is active on the current day, it is set to 1
Then, if I calculate the active users of a certain day / month / year (it is agreed for the time being that only one day online within the statistical time is called active), please give the next redis command
# Perform bit operation on one or more string keys that save binary bits, and save the results to destkey. # BITOP command supports any one of the four operations: AND, OR, NOT AND XOR BITOP operation destkey key [key ...]
Initialization data:
127.0.0.1:6379> setbit 20210308 1 1 (integer) 0 127.0.0.1:6379> setbit 20210308 2 1 (integer) 0 127.0.0.1:6379> setbit 20210309 1 1 (integer) 0
Statistics of total active users from 20210308 to 20210309: 1
127.0.0.1:6379> bitop and desk1 20210308 20210309 (integer) 1 127.0.0.1:6379> bitcount desk1 (integer) 1
Statistics of online active users from 20210308 to 20210309: 2
127.0.0.1:6379> bitop or desk2 20210308 20210309 (integer) 1 127.0.0.1:6379> bitcount desk2 (integer) 2
Usage scenario 3: user online status
For obtaining or counting users' online status, using bitmap is a space-saving and efficient method.
Only one key is required, and then the user ID is offset. If it is online, it is set to 1, and if it is not online, it is set to 0.