Redis common commands
Command learning website: http://doc.redisfans.com/index.html
1 String type
Assignment syntax: SET key value
127.0.0.1:6379> set k1 zhangsan
OK
Value syntax: GET key
127.0.0.1:6379> set k1 zhangsan
OK
Set multiple keys syntax: MSET key value [key value …]
127.0.0.1:6379> mset k2 lisi k3 wangwu
OK
Syntax for obtaining multiple keys: MGET key [key...]
127.0.0.1:6379> mget k2 k3
1) "lisi"
2) "wangwu"
Delete syntax: DEL key
127.0.0.1:6379> del k3
(integer) 1
127.0.0.1:6379> get k3
(nil)
2 Increment and decrement of string numbers
Increment number: When the stored string is an integer, Redis provides a practical command INCR, which increments the current key value and replaces the incremented value.
Increment number syntax: INCR key
Decrement numeric syntax: DECR key
Increase the specified integer syntax: INCRBY key increment
Reduce the specified integer syntax: DECRBY key decrement
Increase
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> incr num
(integer) 2
Decreasing
127.0.0.1:6379> decr num
(integer) 1
Specify the step size: (understand)
127.0.0.1:6379> incrby num2 2
(integer) 2
127.0.0.1:6379> incrby num2 3
(integer) 5
127.0.0.1:6379> decrby num2 2
(integer) 3
127.0.0.1:6379> decrby num2 1
(integer) 2
3 Hash
Hash is called the hash type, and it provides a mapping between fields and field values. The field value can only be a string type, and other types such as hash type and collection type are not supported, which is equivalent to storage in object format
Assignment syntax: HSET key field value
- Set a field value. The HSET command does not distinguish between insert and update operations. The HSET command returns 1 when performing an insert operation, and returns 0 when performing an update operation.
127.0.0.1:6379> hset user1 username zhangsan
(integer) 1
127.0.0.1:6379 > hset user1 username lisi
(integer) 0
Value syntax: HGET key field
127.0.0.1:6379> hget user1 username
"lisi"
Set multiple field syntax: HMSET key field value [field value …]
127.0.0.1:6379> hmset user1 password 123 age 20
OK
Syntax for taking multiple values: HMGET key field [field …]
127.0.0.1:6379> hmget user1 password age
1) "123"
2) "20"
Get all field values syntax: HGETALL key
127.0.0.1:6379> hgetall user1
1) "username"
2) "lisi"
3) "password"
4) "123"
5) "age"
6) "20"
Delete field syntax: HDEL key field [field …]
127.0.0.1:6379> hdel user1 username
(integer) 1
127.0.0.1:6379> hgetall user1
1) "password"
2) "123"
3) "age"
4) "20"
Can support partial modification
4. Queue List
Redis's list uses a linked list to store data, and features: fast addition and deletion, slow query (Linkedlist). This queue is ordered.
Add an element to the left of the list: LPUSH key value [value...]
Pop elements from the left of the list: LPOP key (temporary storage, after popping, clear from the queue)
127.0.0.1:6379> lpush alist a1 a2 123
(integer) 3
127.0.0.1:6379> lpop alist
"123"
127.0.0.1:6379> lpop alist
"a2"
Add an element to the right of the list: RPUSH key value [value …]
Pop elements from the right side of the list: RPOP key
127.0.0.1:6379> rpush blist a1 a2 345
(integer) 3
127.0.0.1:6379> rpop blist
"345"
Get the number of list elements: LLEN key
127.0.0.1:6379> llen blist
(integer) 1
View list syntax: LRANGE key start stop
127.0.0.1:6379> lrange blist 0 3
1) "a2"
- All elements before start and stop (including the elements at both ends) will be returned. The index starts from 0 and can be a negative number, such as: "-1" represents the last element
Example: LPUSH comment1'{"id":1,"name":"产品","date":1430295077289}'
Temporary storage. First in, first out. Use a doubly linked list:
1. Go in on the left, go on the right
127.0.0.1:6379> lpush stulist stu1
(integer) 1
127.0.0.1:6379> lpush stulist stu2
(integer) 2
127.0.0.1:6379> lpush stulist stu3
(integer) 3
127.0.0.1:6379> lpush stulist stu4
(integer) 4
127.0.0.1:6379> lpush stulist stu4
(integer) 5
127.0.0.1:6379> lpush stulist stu5
(integer) 6
127.0.0.1:6379> rpop stulist
"stu1"
127.0.0.1:6379> rpop stulist
"stu2"
127.0.0.1:6379> rpop stulist
"stu3"
127.0.0.1:6379> rpop stulist
"stu4
2. Go in on the right and go on the left.
127.0.0.1:6379> rpush clist stu1
(integer) 1
127.0.0.1:6379> rpush clist stu2
(integer) 2
127.0.0.1:6379> rpush clist stu3
(integer) 3
127.0.0.1:6379> rpush clist stu4
(integer) 4
127.0.0.1:6379> rpush clist stu5
(integer) 5
127.0.0.1:6379> lpop clist
"stu1"
127.0.0.1:6379> lpop clist
"stu2"
5 Set
Set collection type: unordered, non-repeatable
Add element syntax: SADD key member [member …]
Delete element syntax: SREM key member [member …]
Get all the elements in the collection: smembers key
127.0.0.1:6379> sadd ulist user1
(integer) 1
127.0.0.1:6379> sadd ulist user2
(integer) 1
127.0.0.1:6379> sadd ulist user3
(integer) 1
127.0.0.1:6379> smembers ulist
1) "user2"
2) "user3"
3) "user1"
127.0.0.1:6379> srem ulist user2
(integer) 1
127.0.0.1:6379> smembers ulist
1) "user3"
2) "user1"
Determine whether the element is in the collection: SISMEMBER key member
127.0.0.1:6379> smembers ulist
1) "user3"
2) "user1"
127.0.0.1:6379> sismember ulist user2
(integer) 0
127.0.0.1:6379> sismember ulist user1
(integer) 1
6 Zset ordered set
Sortedset, also called zset, is an ordered set, sortable, but unique. The difference between Sortedset and set is that a score is added to the elements in the set, and then sorted by this score.
Add element: ZADD key score member [score member …]
- Add an element and the element's score to the ordered set. If the element already exists, it will replace the original score with the new score.
127.0.0.1:6379> zadd num1 20 stu1 30 stu2 40 stu3
(integer) 3
Add mixed scores (you can use student grades, sales volume, etc. to make scores to facilitate calculation and sorting):
Get a list of elements ranked in a certain range and return in descending order of element scores
语法:ZREVRANGE key start stop [WITHSCORES]
127.0.0.1:6379> zadd num1 10 stu4
(integer) 1
127.0.0.1:6379> zrevrange num1 0 4
1) "stu3"
2) "stu2"
3) "stu1"
4) "stu4"
Get the score of the element: ZSCORE key member
127.0.0.1:6379> zscore num1 stu2
"30"
Delete element ZREM key member [member …]
127.0.0.1:6379> zrem num1 stu2
(integer) 1
127.0.0.1:6379> zrevrange num1 0 4
1) "stu3"
2) "stu1"
3) "stu4"
To get the score of the element, you can add the WITHSCORES parameter to the end of the command
127.0.0.1:6379> zrevrange num1 0 4 withscores
1) "stu3"
2) "40"
3) "stu1"
4) "20"
5) "stu4"
6) "10"
Application: product sales; student rankings, etc.
Add or subtract points to a certain attribute, use negative numbers when subtracting points:
127.0.0.1:6379> zincrby num1 2 stu1
"22"
Example:
The sales volume of product number 1001 is 9, and the sales volume of product number 1002 is 10
ZADD sellsort 9 1001 10 1002
Product number 1001's sales increase by 1
ZINCRBY sellsort 1 1001
The top 3 in the product sales ranking queue:
zrevrange sellsort 0 2 withscores
Top 3 student rankings:
zrevrange stus 0 2 withscores
7 HyoperLogLog command
HyperLogLog is an algorithm that uses randomization to provide an approximate value of the number of unique elements in the set with a small amount of memory.
HyperLogLog can accept multiple elements as input and give an estimate of the cardinality of the input elements:
- Cardinality: The number of different elements in the set. For example, the base of {'apple','banana','cherry','banana','apple'} is 3.
- Estimated value: The base number given by the algorithm is not accurate, it may be slightly more or less than the actual value, but it will be controlled within a reasonable range.
The advantage of HyperLogLog is that even if the number or volume of input elements is very very large, the space required to calculate the base is always fixed and small.
In Redis, each HyperLogLog key only needs 12 KB of memory to calculate the base of close to 2^64 different elements. This is in sharp contrast to a collection where more elements consume more memory when calculating cardinality.
However, because HyperLogLog only calculates the cardinality based on the input element, and does not store the input element itself, HyperLogLog cannot return the input elements like a collection.
Some basic commands related to HyperLogLog.
Example:
redis 127.0.0.1:6379> PFADD mykey "redis"
1) (integer) 1
redis 127.0.0.1:6379> PFADD mykey "java"
1) (integer) 1
redis 127.0.0.1:6379> PFADD mykey "mysql"
1) (integer) 1
redis 127.0.0.1:6379> PFCOUNT mykey
(integer) 3
8 Other commands
(1) keys return all keys that meet the given pattern
keys user* //查询以user开头的key
keys * //查询所有的key
(2) exists confirms whether a key exists, and returns 1 if it exists
127.0.0.1:6379> exists num2 //语法:exists key
(integer) 1
127.0.0.1:6379> exists num23
(integer) 0
(3) del delete a key
127.0.0.1:6379> del num1 //语法: del key 删除存在的key返回1,不存在的key返回0
(integer) 1
127.0.0.1:6379> del num23
(integer) 0
(4) rename重命名key:rename oldkey newkey
127.0.0.1:6379> rename k1 k11
OK
127.0.0.1:6379> keys *
1) "ulist"
2) "k2"
3) "user1"
4) "num2"
5) "clist"
6) "k11"
(5) type The type of return value: type key
127.0.0.1:6379> type ulist
set
127.0.0.1:6379> type k11
string
127.0.0.1:6379> type alist
list
Set key lifetime: cached data generally needs to set lifetime, that is, data is destroyed after expiration.
(6) EXPIRE key seconds
-
Set the survival time of the key (unit: seconds) how many seconds after the key will be automatically deleted
-
TTL key View the remaining survival time of the key
-
PERSIST key clear survival time
127.0.0.1:6379> set a1 123
OK
127.0.0.1:6379> get a1
“123”
127.0.0.1:6379> expire a1 60
(integer) 1
127.0.0.1:6379> ttl a1
(integer) 56
127.0.0.1:6379> ttl a1
(integer) 51
127.0.0.1:6379> ttl a1
(integer) 47
(7) Get server information and statistics: info
(8) Delete all keys in the currently selected database: flushdb
(9) Delete all keys in all databases: flushall
9 Redis multiple databases
A redis instance key includes multiple databases. The client can specify which database to connect to a redis instance, just like creating multiple databases in a mysql, and the client specifies which database to connect to when connecting.
A redis instance can provide up to 16 databases, with subscripts from 0-15. The client connects to database No. 0 by default. You can also choose which database to connect to by selecting. Connect to database No. 1 as follows:
Switch to below 0 database
Move the key data to database No. 1: move key database number