General Key Value Operation
keys to view key values: Regular matching key:? * [] 127.0.0.1:6379> keys * 1) "name" 2) "guo" 127.0.0.1:6379> keys g* 1) "guo"
Note: keys are generally not recommended in production environments, which can cause performance problems and can be stored in other ways. Time complexity O(N),N is the number of key s in the database
Random key random key 127.0.0.1:6379> randomkey "guo" 127.0.0.1:6379> randomkey "name" Determine whether the key exists 127.0.0.1:6379> exists name (integer) 1 127.0.0.1:6379> exists age (integer) 0 127.0.0.1:6379> Delete key del key 127.0.0.1:6379> del name (integer) 1 127.0.0.1:6379> exists name (integer) 0 Rename key new key; rename overrides the original key value 127.0.0.1:6379> keys * 1) "age" 2) "name" 127.0.0.1:6379> rename age name OK 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> get name "shuai" 127.0.0.1:6379> renamenx key new key If the original value exists, the modification does not take effect. select num to switch different redis databases The default redis has 16 server databases 16 move key dbnum is used to move different keys to different databases. If the key value already exists in the target database, the move fails. ttl key: Look at the expiry date of the key, - 1 means permanent, but expires, and returns - 2 for nonexistent keys. 127.0.0.1:6379> keys * 1) "age" 2) "name" 127.0.0.1:6379> ttl name (integer) -1 127.0.0.1:6379> ttl age (integer) 75 127.0.0.1:6379> ttl lin (integer) -2 127.0.0.1:6379> pttl key checks the validity of the key in units of milliseconds. The expire key integer value, in seconds, sets the validity of the key: 127.0.0.1:6379> expire name 1000 (integer) 1 127.0.0.1:6379> ttl name (integer) 996 127.0.0.1:6379> persist key: Make a key permanent; pexpire site 9000; use milliseconds as a unit; set key is worth validity; persist key sets a key to permanent validity.
Character string
String stores three types of values: 1, byte string; 2, integer; 3, floating point number.
Set value set key value ex num|px num nx|xx
set key value ex Seconds|px Msec
127.0.0.1:6379> set site www.baidu.com ex 10000
OK
127.0.0.1:6379> ttl site
(integer) 9996
127.0.0.1:6379> set web test px 10000
OK
127.0.0.1:6379> ttl web
(integer) 7
127.0.0.1:6379>
nx:Indicates that if it exists, it will not operate.
xx:Represents an operation if it exists.
mset Set multiple key values at one time:
127.0.0.1:6379> mset a aman b bold c controller d diamond
OK
127.0.0.1:6379> keys *
1) "site"
2) "a"
3) "c"
4) "b"
5) "name"
6) "d"
127.0.0.1:6379>
get keyGet values
mget keya keyb keyc Get multiple values at a time.
setrange Modify the value of a key offset, if not enough\x00 Fill it up.
127.0.0.1:6379> mget a b c
1) "aman"
2) "bold"
3) "controller"
127.0.0.1:6379> setrange a 10 ??
(integer) 12
127.0.0.1:6379> setrange c 5 **
(integer) 10
127.0.0.1:6379> mget a c
1) "aman\x00\x00\x00\x00\x00\x00??"
2) "contr**ler"
127.0.0.1:6379>
//The corresponding is getrange. key start end
127.0.0.1:6379> mget a c
1) "aman\x00\x00\x00\x00\x00\x00??"
2) "contr**ler"
127.0.0.1:6379> getrange c 4 20
"r**ler"
127.0.0.1:6379>
append key value Add content to the end of the string.
127.0.0.1:6379> append c linjunbin
(integer) 19
127.0.0.1:6379> get c
"contr**lerlinjunbin"
127.0.0.1:6379>
getset key newvalue Gets and returns the old value and sets the new value.
127.0.0.1:6379> set status sleep
OK
127.0.0.1:6379> getset status sleeping
"sleep"
127.0.0.1:6379> get status
"sleeping"
127.0.0.1:6379>
incr key Self increment1,Second kill and ticket robbery.
>>> conn.get('key');
>>> conn.incr('key');
1
>>> conn.get('key');
'1'
decr keySelf decrement1
127.0.0.1:6379> incr num
(integer) 101
127.0.0.1:6379> get num
"101"
127.0.0.1:6379> decr num
(integer) 100
127.0.0.1:6379> get num
"100"
incrby key number How much increase?
decrby key number How much less
incrbyfloat key number How many floating-point numbers are added?
//Do the operation in place:
setbit key offset value Set up offset Returns the old value on the bit corresponding to the value on the binary bit. If offset Too large
getbit key offset View the binary digit value of a string.
127.0.0.1:6379> getbit num 0
(integer) 0
127.0.0.1:6379> getbit num 1
(integer) 0
127.0.0.1:6379> getbit num 0
(integer) 0
127.0.0.1:6379> getbit num 1
(integer) 0
127.0.0.1:6379> getbit num 2
(integer) 1
127.0.0.1:6379> getbit num 3
(integer) 1
127.0.0.1:6379> getbit num 4
(integer) 0
127.0.0.1:6379> getbit num 5
(integer) 0
127.0.0.1:6379> getbit num 6
(integer) 0
127.0.0.1:6379> getbit num 7
(integer) 1
127.0.0.1:6379> getbit num 8
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379> get num
"100"
127.0.0.1:6379> setbit num 2 0
(integer) 1
127.0.0.1:6379> get num
"\x1100"
127.0.0.1:6379> setbit num 2 1
(integer) 0
127.0.0.1:6379> get num
"100"
bitcount key [start end]Statistical string binary digits.
bitop op dest-key keyname[keyname,keyname]Perform inclusion on one or more binary bit stringsand,or,xor,notOperation.
list
redis lists allow users to push or pop up elements from both sides of a sequence, retrieve list elements, and perform various common list operations.
Element 1===> Element 2===> Element 3====> Element 4
lpush: Insert elements to the left of a list. Rpush list name value: Insert an element to the right of a list. Lrange list name start end: Remove a range element from the list. lpop,rpop pops up elements in the list to the left or right. 127.0.0.1:6379> lpush character utf8 (integer) 1 127.0.0.1:6379> rpush character gbk (integer) 2 127.0.0.1:6379> rpush character unicode (integer) 3 127.0.0.1:6379> lrange character 0 10 1) "utf8" 2) "gbk" 3) "unicode" 127.0.0.1:6379> 127.0.0.1:6379> rpop character "unicode" 127.0.0.1:6379> lrange character 0 10 1) "utf8" 2) "gbk" 127.0.0.1:6379> rpop character "gbk" 127.0.0.1:6379> lrange character 0 10 1) "utf8" 127.0.0.1:6379> lindex list-name offset returns offset elements of offsets in the list. 127.0.0.1:6379> lindex character 0 "utf8" 127.0.0.1:6379> lindex character 1 "gbk" 127.0.0.1:6379> lindex character 2 "ascii" 127.0.0.1:6379> ltrim list-name start end prunes the list 127.0.0.1:6379> lrange character 0 12 1) "utf8" 2) "gbk" 3) "ascii" 4) "gb2312" 5) "utf16" 6) "utf" 7) "32" 127.0.0.1:6379> ltrim character 0 2 OK 127.0.0.1:6379> lrange character 0 12 1) "utf8" 2) "gbk" 3) "ascii" 127.0.0.1:6379> llen list-name View List Length 127.0.0.1:6379> llen character (integer) 9 127.0.0.1:6379> linsert list-name after|before search-val insert-value lookup inserts value before or after finding a value search-value, and once found, it will not end up looking down. 127.0.0.1:6379> linsert character after b binsert (integer) 10 127.0.0.1:6379> lrange character 0 -1 1) "utf8" 2) "gbk" 3) "ascii" 4) "a" 5) "b" 6) "binsert" 7) "c" 8) "d" 9) "e" 10) "f" 127.0.0.1:6379> rpoplpush source dest takes the tail of the source and puts it at the head of the dest and returns the unit value. Scenario: task+bak double linked list completes the security queue. task list ====== pop-up ====> bak list insertion Task lists assume that you have tasks a,b,c,d to do, and bak lists indicate tasks being done. Business logic: Use two linked lists to complete the security queue. 1:Rpoplpush task bak 2: Receive the return value and do business processing 3: If successful, rpop bak cleanup task. If unsuccessful, next time take task from bak table. This will not result in the loss of tasks that did not succeed. One more waiting process: blpop listname [listname] timeout pops up the leftmost element from the first non-empty list, or blocks within seconds of timeout and waits for the element to pop up. One waits until the timeout second ends. brpop listname [listname] timeout rpoplpush brpoplpush Common scenarios: Message delivery, task queue scenario: long polling Ajax, available for online chat One of the main advantages of lists is: It can contain multiple strings and can be centralized in the same place.
Example: A large number of users to carry out statistics, using bitmaps to statistics user login.
Assuming that there are seven users, we need to count whether the users log in every day and use bitmap to represent them. 0 means no login, 1 means login The first bit of 1010101 indicates the login status of user1, which can be counted by bit operation.
Scenario: 100 million users, each user logs in / does any operation, recorded as active today, otherwise recorded as inactive Weekly reviews: Award-winning active users: 7 consecutive days of activity Monthly review, etc. Train of thought: Userid dt active 1 2013-07-27 1 1 2013-0726 1 If it is placed in a table, 1: the table increases sharply, 2: to use group, sum operation, calculation is slow. Bitmap bit-map Log0721: '011001...............0' ...... log0726 : '011001...............0' Log0727 : '0110000.............1' 1: Record user login: Every day a bitmap is generated by date. After the user logs in, the bit value on the user_id bit is set to 1. 2: Calculate the bitmap and for a week. Bit 1 is a continuous login user. redis 127.0.0.1:6379> setbit mon 100000000 0 (integer) 0 redis 127.0.0.1:6379> setbit mon 3 1 (integer) 0 redis 127.0.0.1:6379> setbit mon 5 1 (integer) 0 redis 127.0.0.1:6379> setbit mon 7 1 (integer) 0 redis 127.0.0.1:6379> setbit thur 100000000 0 (integer) 0 redis 127.0.0.1:6379> setbit thur 3 1 (integer) 0 redis 127.0.0.1:6379> setbit thur 5 1 (integer) 0 redis 127.0.0.1:6379> setbit thur 8 1 (integer) 0 redis 127.0.0.1:6379> setbit wen 100000000 0 (integer) 0 redis 127.0.0.1:6379> setbit wen 3 1 (integer) 0 redis 127.0.0.1:6379> setbit wen 4 1 (integer) 0 redis 127.0.0.1:6379> setbit wen 6 1 (integer) 0 redis 127.0.0.1:6379> bitop and res mon feb wen (integer) 12500001 As mentioned above, advantages: 1: Save space, 100 million people landing every day, with 100 million bit s, about 1200 WByte, about 10M characters can be expressed. 2: Easy to calculate
Set set
1. Disorder. 2. Uniqueness. 3. Certainty. There is no order, so unlike a list with subscripts, there is no need to insert from left or right.
sadd key-name member [member member]Add a collection element.
smembers key-nameView the elements of the collection.
127.0.0.1:6379> sadd sex man woman
(integer) 2
127.0.0.1:6379> smembers sex
1) "man"
2) "woman"
127.0.0.1:6379> sadd sex man
(integer) 0
127.0.0.1:6379> smembers sex
1) "man"
2) "woman"
127.0.0.1:6379>
srem key-name item [item] Delete elements in a collection
127.0.0.1:6379> srem sex man a
(integer) 1
127.0.0.1:6379> smembers sex
1) "woman"
127.0.0.1:6379>
spop key-nameAn element in a random overflow set and returns the removed element.
127.0.0.1:6379> smembers sex
1) "a"
2) "l"
3) "n"
4) "b"
5) "m"
6) "f"
7) "d"
8) "woman"
9) "j"
10) "k"
11) "e"
12) "h"
13) "i"
14) "c"
15) "g"
127.0.0.1:6379> spop sex
"k"
127.0.0.1:6379> spop sex
"h"
127.0.0.1:6379> spop sex
"e"
127.0.0.1:6379> spop sex
"i"
127.0.0.1:6379> smembers sex
1) "a"
2) "l"
3) "n"
4) "b"
5) "m"
6) "f"
7) "d"
8) "woman"
9) "j"
10) "c"
11) "g"
127.0.0.1:6379>
//Application Scenario: Can be used for lottery.
srandmember key-nameReturns an element in a collection
127.0.0.1:6379> srandmember sex
"m"
127.0.0.1:6379> srandmember sex
"a"
127.0.0.1:6379> srandmember sex
"d"
127.0.0.1:6379> srandmember sex
"n"
127.0.0.1:6379> srandmember sex
"m"
sismember key-name itemSeeitemIs there a collection?
127.0.0.1:6379> sismember sex a
(integer) 1
127.0.0.1:6379> sismember sex z
(integer) 0
scard key Number of elements in a query set
127.0.0.1:6379> scard sex
(integer) 11
127.0.0.1:6379>
smove source-key dest-key item If set source-key Containing elementsitem
//So remove it from the set source-keyitemadd to dest-key In, successfully remove and return1,Otherwise return0
127.0.0.1:6379> sadd name lin
(integer) 1
127.0.0.1:6379> smove sex name woman
(integer) 1
127.0.0.1:6379> sadd name a l n
(integer) 3
127.0.0.1:6379> smove sex name a
(integer) 1
127.0.0.1:6379> smembers name
1) "l"
2) "woman"
3) "lin"
4) "a"
5) "n"
127.0.0.1:6379> smove sex name n
(integer) 1
127.0.0.1:6379>
sdiff key-name [key-name] Returns those elements that exist in the first set but do not exist in other sets
127.0.0.1:6379> sdiff lisi wangwu poly
1) "b"
127.0.0.1:6379> sdiff lisi wangwu
1) "b"
2) "d"
127.0.0.1:6379> sdiff lisi poly
1) "c"
2) "b"
127.0.0.1:6379>
sinter key-name [key-name key-name]Returns the intersection in the collection
127.0.0.1:6379> sinter lisi wangwu poly
1) "a"
//The corresponding sinterstore returns the intersection store.
sdiffstore dest key-name key-name [key-name...]
//Store difference sets in dest sets
127.0.0.1:6379> sdiffstore diff lisi wangwu
(integer) 2
127.0.0.1:6379> smembers diff
1) "b"
2) "d"
127.0.0.1:6379>
sunion key-name [key-name...]Returns elements that have at least one set, representing unions
sunionstore dest key-name [key-name...]
127.0.0.1:6379> smembers name
1) "woman"
2) "lin"
3) "a"
4) "l"
5) "n"
127.0.0.1:6379> sunion name sex
1) "f"
2) "m"
3) "woman"
4) "a"
5) "l"
6) "n"
7) "b"
8) "d"
9) "j"
10) "lin"
11) "c"
12) "g"
127.0.0.1:6379>
Ordered set
Ordered sets store the mapping between members and scores, and must have corresponding element weights. And the orderly acquisition or scanning of members and scores according to the size of scores.
zadd key-name score item score item Add an ordered collection element. Weights need to be added
127.0.0.1:6379> zadd class 12 lily 13 lucy 18 lilei 6 poly
(integer) 4
127.0.0.1:6379> zrange class 0 1
1) "poly"
2) "lily"
127.0.0.1:6379>
//Query between the first and the last.
zrange key-name start end Remove the set of start and end elements in an ordered set according to the order.
127.0.0.1:6379> zrange class 0 -1 withscores
1) "poly"
2) "6"
3) "lily"
4) "12"
5) "lucy"
6) "13"
7) "lilei"
8) "18"
9) "lin"
10) "90"
127.0.0.1:6379>
//Filter zrangebyscore according to the weight range, and also support limit to filter display entries
withscores Represents returning a weight value at the same time.
127.0.0.1:6379> zrangebyscore class 0 20 limit 0 1
1) "poly"
127.0.0.1:6379> zrangebyscore class 0 20
1) "poly"
2) "lily"
3) "lucy"
4) "lilei"
127.0.0.1:6379>
127.0.0.1:6379> zrangebyscore class 0 20 withscores
1) "poly"
2) "6"
3) "lily"
4) "12"
5) "lucy"
6) "13"
7) "lilei"
8) "18"
127.0.0.1:6379>
zcard Represents the number of elements in the acquisition set
127.0.0.1:6379> zcard class
(integer) 5
zcount Represents the number of elements in the weight range of the collection, and counts the number.
127.0.0.1:6379> zcount class 0 50
(integer) 4
zrank key-name item Getting Ordered Sets item Ranking
127.0.0.1:6379> zrank class lucy
(integer) 2
127.0.0.1:6379> zrank class lilei
(integer) 3
zrevrank Reverse ranking
127.0.0.1:6379> zrevrank class lilei
(integer) 1
127.0.0.1:6379> zrevrank class lucy
(integer) 2
127.0.0.1:6379> zrevrank class poly
(integer) 4
zrem key-name item item Delete elements from an ordered set
127.0.0.1:6379> zrem class poly lily lilei
(integer) 3
127.0.0.1:6379> zrange class 0 -1 withscores
1) "lucy"
2) "13"
3) "lin"
4) "90"
zremrangebyscore Delete according to weight range
zremrangebyrank Delete by sorting range
127.0.0.1:6379> zrange class 0 -1 withscores
1) "lisi"
2) "8"
3) "gou"
4) "9"
5) "lucy"
6) "13"
7) "lin"
8) "90"
9) "tt"
10) "90"
11) "linjunbin"
12) "100"
127.0.0.1:6379> zremrangebyscore class 9 20
(integer) 3
127.0.0.1:6379> zrange class 0 -1
1) "lisi"
2) "lin"
3) "tt"
127.0.0.1:6379> zremrangebyrank class 0 1
(integer) 2
127.0.0.1:6379> zrange class 0 -1
1) "tt"
127.0.0.1:6379>
zincrby key-name num member take member Members'weight score plus num.
127.0.0.1:6379> zincrby class 10 tt
"100"
127.0.0.1:6379> zrange class 0 -1 withscores
1) "tt"
2) "100"
zinterstore Perform intersection operations in similar sets for a given ordered set,And store the results
zinterstore key-name key-number key-name [key-name...] [aggregate sum|mim|max]
key-number Represents the number of subsequent intersections. The default is summation, and other operations can be used.
127.0.0.1:6379> zinterstore res 2 lisi wang aggregate sum
(integer) 3
127.0.0.1:6379> zrange res 0 -1 withscores
1) "cat"
2) "5"
3) "dog"
4) "11"
5) "horse"
6) "14"
127.0.0.1:6379>
//Setting weights can add weights to two or more ordered sets.
zunionstore Seeking Union.