Table of contents
Redis data type_String
Introduction
String is the most basic type of Redis, and a key corresponds to a value. String is binary safe, which means that String can contain any data, such as a serialized object or an image. String can hold up to 512M data.
Common commands
set
Used to set the value of the given key. If the key already stores another value, set overwrites the old value, regardless of the type.
Grammar format:
set key value
Example:
127.0.0.1:6379> set k1 v1
OK
get
Used to get the value of the specified key. Returns nil if key does not exist.
Grammar format:
get key
Example:
127.0.0.1:6379> get k1
"v1"
append
Append the given value to the end of the original value of key.
Grammar format:
append key value
Example:
127.0.0.1:6379> APPEND k1 k1
(integer) 4
127.0.0.1:6379> APPEND k1 k2
(integer) 6
Notice:
- If key already exists and is a string, the append command appends value to the end of key's original value.
- If the key does not exist, append simply sets the given key to the value, just like doing set key value.
strlen
Get the length of the string value stored by the specified key. Returns an error when the key does not store a string value.
Grammar format:
strlen key
Example:
127.0.0.1:6379> strlen k1
(integer) 6
sevenx
Set a value and an expiration time in time seconds for the specified key. If the key already exists, the setex command will replace the old value and set the expiration time.
Grammar format:
setex key time value
Example:
#向Redis中设置一个k1的键值对并且10秒后过期
127.0.0.1:6379> setex k1 10 v1
OK
setnx
Only set the value of the key if the key does not exist
Grammar format:
setnx key value
Example:
127.0.0.1:6379> setnx k1 v1
(integer) 0
127.0.0.1:6379> setnx k4 v4
(integer) 1
anxious
Get the value within the specified range, similar to the relationship between.....and
Grammar format:
getrange key start end
Example:
127.0.0.1:6379> set k5 abcd123xxx
OK
127.0.0.1:6379> getrange k5 2 4
"cd1"
setrange
Get the value within the specified range, similar to the relationship between.....and
Grammatical structures:
setrange key offset value
Example:
127.0.0.1:6379> set k6 abcd1234
OK
127.0.0.1:6379> setrange k6 1 xxx
(integer) 8
127.0.0.1:6379> get k6
"axxx1234"
incr
Increment the numeric value stored in key by one.
Grammar format:
incr key
Example:
#因为Redis中不存在k1,所以先初始化为0,再递增,值为1
127.0.0.1:6379> incr k1
(integer) 1
# incr k1 存在k1,递增后k1的值为2
127.0.0.1:6379> incr k1
(integer) 2
# 如果value不是数字就会报错
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> INCR k2
(error) ERR value is not an integer or out of range
Notice:
If the key does not exist, the value of the key will be initialized to 0 first, and then the incr operation will be performed.
If the value of string type cannot be represented as a number or other types, then return an error.
Dec
Decrements the numeric value stored in key by one.
Grammar format:
decr key
Example:
127.0.0.1:6379> decr k1
(integer) 1
127.0.0.1:6379> decr k1
(integer) 0
127.0.0.1:6379> decr k1
(integer) -1
127.0.0.1:6379> decr k1
(integer) -2
#如果
set k2 v2
decr k2 因为k2不为数值,Redis返回一个错误
Notice:
If the key does not exist, the value of the key will be initialized to 0 first, and then the decr operation will be performed.
If the value of string type cannot be represented as a number or other types, then return an error.
incrby/decrby key step
Increase or decrease the digital value stored in the key according to the step.
127.0.0.1:6379> incrby k1 10
(integer) 20
Notice:
If the key does not exist, the value of the key will be initialized to 0 first, and then the incrby/decrby command will be executed.
If the value of string type cannot be represented as a number or other types, then return an error.
mset
Set one or more key-value at the same time.
Grammar format:
mset key1 value1 key2 value2
Example:
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3
OK
mget
Returns all (one or more) values for the given key.
Grammar format:
mget key1 key2
Example:
127.0.0.1:6379> mget k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
Notice:
If a key does not exist in the given key, then this key returns the special value nil.
getset
Set the given key value as value, and return the old value of the key (old value), in a simple sentence (get first and then set immediately).
Grammar format:
getset key value
Example:
127.0.0.1:6379> getset k1 wcc
"v1"
127.0.0.1:6379> get k1
"wcc"
scenes to be used
- value can be a number in addition to a string.
- counter
- Count the number of multiple units
- Number of fans
- object cache storage
- distributed lock
real-time learning feedback
1. How to set the value of a given key in the String data type of Redis technology.
A get
B append
C set
D mget
2. In the Redis technology String data type, how to set the value of the key only when the key does not exist.
A get
B append
Csetnx _
D mget
Redis data type_List
Introduction
List is simply a list of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list. The bottom layer is a doubly linked list, which has extremely high performance for two-stage operations, and the performance of nodes in the middle through index operations is poor.
A List can contain at most $2^{32}-1$ elements (more than 4 billion elements per list).
Common commands
lpush/rpush
Insert one or more values from left (head)/right (tail).
Grammatical structures:
lpush/rpush key1 value1 value2 value3……
Example:
#从左边放入v1 v2 v3
127.0.0.1:6379> lpush k1 v1 v2 v3
(integer) 3
#从右边放入v4 v5 v6
127.0.0.1:6379> rpush k1 v4 v5 v6
(integer) 6
lrange
Returns the elements between start and end in the key list (including start and end). Where 0 represents the first element of the list and -1 represents the last element.
Grammatical structures:
lrange key start end
Example:
#取出列表里前3个值,结果为v3 v2 v1
127.0.0.1:6379> lrange k1 0 2
#取出列表里全部值,结果为v3 v2 v1 v4 v5 v6
127.0.0.1:6379> lrange k1 0 -1
lpop/rpop
Remove and return the first or last value.
Grammar format:
lpop/rpop key
Example:
lpop k1 removes v3 from the list and returns all values in the current list v2 v1 v4 v5 v6
rpop k1 removes v6 from the list and returns, all values of the current list v2 v1 v4 v5
Note: The key is in the value, but the key is dead.
lindex
Get the value at index position of the list (starting from the left).
Grammatical structures:
lindex key index
Example:
lindex k1 0
curtain
Get the length of the list.
Grammatical structures:
llen key
Example:
127.0.0.1:6379> llen k1
(integer) 6
lrem
Delete count elements that are the same as value from the left.
Grammatical structures:
lrem key count value
Example:
#从左边开始删除k1列表中2个v1元素
lrem k1 2 v1
lensed
Insert a new value before/after value in the list (starting from the left).
Grammatical structures:
linsert key before/after value newvalue
Example:
linsert k1 before v1 v5 在v1前面插入一个v5
set
Set the value at index index to value
Grammatical structures:
lset key index value
Example:
lset key index value
scenes to be used
- message queue
- leaderboard
- latest list
real-time learning feedback
1. How to insert one or more values into the List data type of Redis technology.
A lpush/rpush
B llen
C lset
D lrem
2. How to remove and return the first value or the last value in the List data type of Redis technology.
A lpush/rpush
B llen
C lset
D lpop/rpop
Redis data type_Set
Introduction
Similar to List, it is a list function, but Set is automatically sorted. When you need to store a list of data and do not want duplicate data, Set is a good choice.
Set is an unordered collection of String type. Its underlying layer is actually a hash table whose value is null, so the time complexity of adding, deleting, and searching is O(1).
Common commands
sadd
Add one or more elements to the collection key, existing elements will be ignored.
Grammatical structures:
sadd key value1 value2……
Example:
#向集合中添加值,最终只有v1 v2 v3 v4 v5 v6
127.0.0.1:6379> sadd k1 v1 v2 v2 v3 v4 v5 v6
smembers
Get all elements of this collection.
Grammatical structures:
smembers key
Example:
127.0.0.1:6379> smembers k1
sismember
Determine whether the set key contains a value element, and return 1 if there is one, otherwise return 0.
Grammatical structures:
sismember key value
Example:
sismember k1 v1
scard
Returns the number of elements in the collection.
Grammatical structures:
scard key
Example:
scard k1
luck
Delete one or more member elements in the collection, the non-existing member elements will be ignored.
Grammatical structures:
srem key value1 value2……
Example:
# 删除v1 v2
srem k1 v1 v2
spop
Randomly removes an element from the collection and returns that element.
Grammatical structures:
spop key
Example:
spop k1 随机删除一个元素,并返回
srandmember
Randomly remove count elements from the collection, but will not delete them.
Grammatical structures:
srandmember key count
Example:
#随机取出集合中的2个元素
srandmember k1 2
I'm sorry
Move the value element from the sourcekey collection to the destinationkey collection.
Grammatical structures:
smove sourcekey destinationkey value
Example:
smove k1 k2 v5 将元素v5从集合k1中移动到集合k2
Note: If the sourcekey collection does not exist or does not contain the specified value element, the move command does nothing and just returns 0.
sinter
Returns the intersection elements of two collections.
Grammatical structures:
sinter key1 key2
Example:
sinter key1 key2
sunion
Returns the union elements of two collections.
Grammatical structures:
sunion key1 key2
Example:
sunion k1 k2
sdiff
Returns the difference set elements of two sets (in key1, not including key2)
Grammatical structures:
sdiff key1 key2
Example:
sdiff k1 k2
scenes to be used
- Black and white list
- random display
- friends
- follow people
- fan
- collection of interested people
real-time learning feedback
1. Add one or more elements to the set key in the Set data type of Redis technology.
A spop
B sinter
C sdiff
D sadd
2. How to retrieve all elements of the set in the Redis technology Set data type.
A spop
B smembers
C sdiff
D sadd
Redis data type_Hash
Introduction
Hash is a collection of key-value pairs. Hash is a mapping table of field (field) and value (value) of String type, and hash is especially suitable for storing objects.
Common commands
hset
Assign value to the field in the key collection.
Grammatical structures:
hset key field value
Example:
127.0.0.1:6379> hset user name xiaoton (integer) 1 127.0.0.1:6379> hset user age 3 (integer) 1
hget
From the key hash, get the value of the field field.
Grammatical structures:
hget key field
Example:
127.0.0.1:6379> hget user name "xiaoton"
hmset
Set hash fields and values in batches.
Grammatical structures:
hmset key field1 value1 field2 value2……
Example:
127.0.0.1:6379> hmset user1 name xiaoton age 15 OK
hexists
Determine whether the field exists in the specified key
Grammatical structures:
hexists key field
Example:
127.0.0.1:6379> hexists user1 name (integer) 1 127.0.0.1:6379> hexists user1 xxx (integer) 0
hkeys
Get all the fields in this hash.
Grammatical structures:
hkeys key
Example:
127.0.0.1:6379> hkeys user1 1) "name" 2) "age"
hvals key
Get all the values in this hash.
Grammatical structures:
hvals key
Example:
127.0.0.1:6379> hvals user1 1) "xiaoton" 2) "15"
hincrby
Add increment to the value of the field field in the key of the hash table.
Grammatical structures:
hincrby key field increment
Example:
127.0.0.1:6379> hincrby user1 age 10 (integer) 25
hdel
Delete one or more specified fields in the hash table key, non-existing fields will be ignored.
Grammatical structures:
hdel key field1 field2……
Example:
127.0.0.1:6379> hdel user1 age (integer) 1
hsetnx
Assign a value to a field that does not exist in the key hash table.
Grammatical structures:
hsetnx key field value
Example:
127.0.0.1:6379> hsetnx user1 age 10 (integer) 1
scenes to be used
1. Shopping cart
2. Store objects
real-time learning feedback
1. How to assign values to the fields in the key collection in the Hash data type of Redis technology.
A hdel
B hsetnx
C hexists
D hset
2. How to extract the value of the field field from the key hash in the Redis technology Hash data type.
A hdel
B hsetnx
C hexists
D hget
Redis data type_Zset
Introduction
Zset is very similar to Set and is a collection of Strings with no repeated elements. The difference is that each element of Zset is associated with a score (score), which is used to sort the elements in the set from low score to high score. The elements of a set are unique, but fractions can be repeated.
Common commands
zadd
Add one or more elements (value) and scores (score) to the ordered set key.
Grammatical structures:
zadd key score1 value1 score2 value2……
Example:
zadd k1 100 java 200 c++ 300 python 400 php
zrange
Returns the elements between the index start and the index end in the key collection (including start and end).
Grammatical structures:
zrange key start end [withscores]
Example:
zrange k1 0 -1 返回集合中所有元素 zrange k1 0 -1 withscores 返回集合中所有元素,并携带元素分数
zrangebyscore
Return the elements between the score minscore and the score maxscore in the key collection (including minscore and maxscore). The positions of the elements are sorted by increasing fractional values (from small to large).
Grammatical structures:
zrangebyscore key minscore maxscore [withscores]
Example:
zrangebyscore k1 200 400 返回200-400分之间的元素递增排序
zincrby
Add the value of increment to the score of element value.
Grammatical structures:
zincrby key increment value
Example:
zincrby k1 50 java 给java元素加上50分
age
Delete the element of value under this collection.
Grammatical structures:
zrem k1 php 删除php
zcount
Count the number of elements in the set in the range of minscore to maxscore.
Grammatical structures:
zcount key minscore maxscore
Example:
zcount k1 100 300 统计100分到300分中间元素的个数
zrank
Returns the rank of value in the collection, starting from 0.
Grammatical structures:
zrank key value
Example:
zrank k1 c++ 返回c++排名
real-time learning feedback
1. How to add one or more elements (value) and scores (score) to the ordered set key in the Redis technology Zset data type.
A zadd
B zrange
C zrangebyscore
D zcount
2. How to return the elements between the index start and the index end in the key collection in the Zset data type of Redis technology.
A zadd
B zrange
C zrangebyscore
D zcount