Redis's five basic data types related knowledge and shell commands
- 1. Introduction
- 2. Basic shell commands of RedisKey
- Three, String data type shell command
-
- 1. String operations
- 2. Data operation
-
- 2.1 Increment by one
- 2.2 Decrement by one
- 2.3 Set the step size and specify the increment
- 2.4 Set the expiration time
- 2.5 Set only if it does not exist
- 2.6 Setting multiple values at the same time
- 2.7 Get multiple values at the same time
- 2.8 When there are multiple assignments, set it only if it does not exist
- 3. Object
- 4. Usage scenarios
- 4. List data type shell command
-
- 1 Introduction
- 2. List operation
-
- 2.1 Insert a value or multiple values into the head of the list (left)
- 2.2 Insert a value or multiple values at the end of the list (right)
- 2.3 Obtain specific values through intervals
- 2.4 Get all the values of the list
- 2.5 Remove the first element of the list
- 2.6 Remove the last element of a list
- 3. Subscript operation
-
- 3.1 Obtain a value in the list by subscript
- 3.2 Return the length of the list
- 3.3 Remove the specified number of values in the list set (exact match)
- 3.4 Intercept the specified length by subscript. (the list is changed, and only the intercepted elements are truncated)
- 3.5 Remove the last element of the list and move it to a new list
- 3.6 Replace the value of the specified subscript in the list with another value (update operation)
- 4. Insert operation
- 5. Pay attention
- Five, Set collection
- 6. Hash type data
-
- 1 Introduction
- 2. Hash basic operation
-
- 2.1 set a specific key-value
- 2.2 Get a field value
- 2.3 set multiple key-value values
- 2.4 Get multiple field values
- 2.5 Get all the data
- 2.6 Delete the key value specified by hash
- 2.7 Get the number of fields in the hash table
- 2.8 Determine whether the specified field in the hash exists
- 2.9 Only get all key values
- 2.10 Only get all value values
- 3. Data manipulation
- Seven, Zset (ordered collection)
-
- 1 Introduction
- 2. Basic operation
- 3. Sorting operation
-
- 3.1 Display all users, from small to large (sort by score if the minimum and maximum values of the range are not known)
- 3.2 Display all users, from largest to smallest
- 3.3 Show all users with grades
- 3.4 Display the ascending order of employees whose salary is less than 2500 (when a certain value is known as the maximum value of the range)
- 4. Set operation
1. Introduction
Quoting a paragraph on the Redis official website, Redis is an open source (BSD licensed), in-memory data structure storage system, which can be used as a database, cache, and message middleware. It supports many types of data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial ( geospatial) Index radius query. Redis has built-in replication (replication), LUA scripting (Lua scripting), LRU driver events (LRU eviction), transactions (transactions) and different levels of disk persistence (persistence), and through Redis Sentinel (Sentinel) and automatic partition (Cluster ) provides high availability (high availability)
2. Basic shell commands of RedisKey
1. View all keys
keys *
2. Assign a value to the specified key value
set key值 value值
For example
set name xg
3. Get the value of the key
get key值
For example
get name
4. Determine whether the current key exists
exists key值
For example
exists name
5. Remove the current key
move key值 1
For example
move name 1
6. Set the expiration time of the key, in seconds
expire key值 10
For example
expire age 10
7. View the remaining time of the current key
ttl key值
For example
ttl age
8. View the type of the current key
type key值
For example
type age
Three, String data type shell command
1. String operations
1.1 Append a string, if the current key does not exist, it is equivalent to setkey
append key值 "value值"
For example
append name zhangsan
或者
append name "zhangsan"
1.2 Get the length of the string
strlen key值
For example
strlen name
1.3 String scope
(1) Intercept the string from 0 to 3, closed interval [0, 3]
getrange key值 起始位置 终止位置
For example
getrange name 0 3
(2) Get the string
getrange key值 0 -1
For example
getrange name 0 -1
1.4 Replace the string starting at the specified position
setrange key值 2 字符串
For example
setrange name 2 wee
2. Data operation
2.1 Increment by one
incr key值
For example
incr age
2.2 Decrement by one
decr key值
For example
decr age
2.3 Set the step size and specify the increment
After use, the original value will be increased by 10
incrby key值 步长值
For example
incrby age 10
2.4 Set the expiration time
Set the key value to xxx, which will expire after 30 seconds
setex key值 时间值 "value值"
For example
setex name 30 "zhangsan"
2.5 Set only if it does not exist
If the key value does not exist, create the key, if it exists, the creation fails
setnx key值 "value值"
For example
setnx name "zhangsan"
2.6 Setting multiple values at the same time
mset key值1 value值1 key值2 value值2 key值3 value值3
For example
mset k1 v1 k2 v2 k3 v3
2.7 Get multiple values at the same time
mget key值1 key值2 key值3
For example
mget k1 k2 k3
2.8 When there are multiple assignments, set it only if it does not exist
msetnx is an atomic operation
msetnx key值1 value值1 key值2 value值2
For example
msetnx k1 v1 k2 v2
3. Object
3.1 Set a user:1 object value as a json string to save an object
set user:{
id}:{
filed}
For example
set user:1 {
name:zhangsan, age:3}
3.2 getset method
If there is a value, get the original value and set the new value
If there is no value, return nil
getset key值 value值
For example
getset name zhangsan
4. Usage scenarios
Counter
Counting the number of multiple units
Followers
Object cache storage
4. List data type shell command
1 Introduction
In redis, lists can be made into stacks, queues, and blocking queues.
And all list commands start with l, case insensitive
2. List operation
2.1 Insert a value or multiple values into the head of the list (left)
lpush 列表名 数据
For example
lpush list one
2.2 Insert a value or multiple values at the end of the list (right)
rpush 列表名 数据
For example
rpush list two
2.3 Obtain specific values through intervals
lrange 列表名 起始位置 结束位置
For example
lrange list 0 2
2.4 Get all the values of the list
lrange 列表名 0 -1
For example
lrange list 0 -1
2.5 Remove the first element of the list
lpop 列表名
For example
lpop list
2.6 Remove the last element of a list
rpop 列表名
For example
rpop list
3. Subscript operation
3.1 Obtain a value in the list by subscript
lindex 列表名 下标名
For example
lindex list 1
3.2 Return the length of the list
llen 列表名
For example
llen list
3.3 Remove the specified number of values in the list set (exact match)
lrem 列表名 个数 value值
For example
lrem list 1 one
3.4 Intercept the specified length by subscript. (the list is changed, and only the intercepted elements are truncated)
ltrim 列表名 起始位置 结束位置
For example
ltrim list 1 2
3.5 Remove the last element of the list and move it to a new list
rpoplpush 原列表名 新列表名
For example
rpoplpush list list2
3.6 Replace the value of the specified subscript in the list with another value (update operation)
Determine whether the table exists. If it does not exist, an error will be reported when updating. If it exists, update the value of the current subscript.
lset 列表名 下标名 value值
For example
lset list 0 name
4. Insert operation
Insert a specific value before or after a value in the list
linsert 列表名 before 已存在的值 即将插入的值
linsert 列表名 after 已存在的值 即将插入的值
For example
linsert list before one three
linsert list after one three
5. Pay attention
It is actually a linked list, before Node after, left, and right can insert values.
If the key value does not exist, create a new linked list.
If the key value exists, add content
. If all values are removed, an empty linked list also means that it does not exist
on both sides. Inserting or changing values has the highest efficiency, and intermediate elements are relatively less efficient.
Message queuing, message queue (lpush rpop)
stack (lpush lpop)
Five, Set collection
1. Set collection related operations
1.1 Add data to the set collection
sadd set集合名 数据
For example
sadd myset one
1.2 View all values of the set collection
smembers set集合名
For example
smembers myset
1.3 Determine whether a value is in the set collection
sismember set集合名 数据
For example
sismember myset one
1.4 Get the number of content elements in the set collection
scard set集合名
For example
scard myset
1.5 Remove the specified element in the set collection
srem set集合名 数据
For example
srem myset one
2. Random operation
2.1 Randomly extract an element
srandmember set集合名
For example
srandmember myset
2.2 Randomly extract a specified number of elements
srandmember set集合名 个数
For example
srandmember myset 2
2.3 Randomly delete some elements in the set collection
spop set集合名
For example
spop myset
3. Move a specified value to another set collection
smove 原set集合名 新set集合名 value值
For example
smove myset myset2 one
4. Collection operations
4.1 Difference (SDIFF)
sdiff set集合1 set集合2
For example
sdiff set1 set2
4.2 Intersection
sinter set集合名1 set集合名2
For example
sinter set1 set2
4.3 Union
sunion set集合名1 set集合名2
For example
sunion set1 set2
6. Hash type data
1 Introduction
Its data type is a map collection, and key-map (the value is a map collection) is not much different from the String type in essence, it is still a simple key-value
2. Hash basic operation
2.1 set a specific key-value
hset hash名称 key值 value值
For example
hset myhash field1 zhangsan
2.2 Get a field value
hget hash名称 key值
For example
hget myhash field1
2.3 set multiple key-value values
hmset hash名称 key值1 value值1 key值2 value值2
For example
hmset myhash x1 y1 x2 y2
2.4 Get multiple field values
hmget hash名称 key值1 key值2
For example
hmget myhash x1 x2
2.5 Get all the data
hgetall hash名称
For example
hgetall myhash
2.6 Delete the key value specified by hash
hdel hash名称 key值
For example
hdel myhash x1
2.7 Get the number of fields in the hash table
hlen hash名称
For example
hlen myhash
2.8 Determine whether the specified field in the hash exists
hexists hash名称 key值
For example
hexists myhash field1
2.9 Only get all key values
hkeys hash名称
For example
hkeys myhash
2.10 Only get all value values
hvals hash名称
For example
hvals myhash
3. Data manipulation
3.1 Increment by one
hincrby hash名称 key值 步长
For example
hincrby myhash num 1
3.2 Decrement by one
hincrby myhash num -1
3.3 Judging existence
The value can be set if it does not exist, and cannot be set if it exists
hsetnx hash表名 key值 value值
For example
hsetnx myhash name zhangsan
Seven, Zset (ordered collection)
1 Introduction
On the basis of set, a value for sorting is added.
2. Basic operation
2.1 Add a value
zadd zset集合名称 排序的标志数字 数据
For example
zadd myset 1 one
2.2 Add multiple values
zadd zset集合名称 排序的标志数字 数据2 排序的标志数字 数据3
For example
zadd myset 2 two 3 three
2.3 Check all values
zrange zset集合名称 起始位置 结束位置
For example
zrange myset 0 -1
3. Sorting operation
data preparation
zadd salary 2500 zhangsan
zadd salary 3000 lisi
zadd salary 1000 wnagwu
3.1 Display all users, from small to large (sort by score if the minimum and maximum values of the range are not known)
zrangebyscore zset集合名称 -inf +inf
For example
zrangebyscore salary -inf +inf
3.2 Display all users, from largest to smallest
zrevrange zset集合名称 0 -1
For example
zrevrange salary 0 -1
3.3 Show all users with grades
zrangebyscore zset集合名称 -inf +inf withscores
For example
zrangebyscore salary -inf +inf withscores
3.4 Display the ascending order of employees whose salary is less than 2500 (when a certain value is known as the maximum value of the range)
zrangebyscore zset集合名称 -inf 范围最大值 withscores
For example
zrangebyscore salary -inf 2500 withscores
4. Set operation
4.1 Remove the specified element in the sorted set
zrem zset集合名称 数据名
result
zrem salary lisi
4.2 Get the number in the ordered set
zcard zset集合名称
For example
zcard salary
4.3 Get the number of members in the specified interval
zcount zset集合名称 起始范围 结束范围
For example
zcount myset 1 3