The basics of redis
Redis has five data types, which are string, hash, list, set and zset.
1. String type:
Redis 127.0.0.1 name 6379 > set name doo// sets a key-value pair with the key name and the value doo
Redis 127.0.0.1 name 6379 > get name// gets the value of name
Redis 127.0.0.1 key 6379 > set name ya / / again assignment will be overwritten. One value corresponds to one key.
Redis 127.0.0.1 6379 > setnx name doo
/ / determines whether name exists. If there is a return of 0, there is no return of 1, and the key-value pair of name=doo is written.
Redis 127.0.0.1 red 6379 > setex haircolor 10 red / / the value of the specified haircolor is stored in 10 seconds, but disappears after the time.
Redis 127.0.0.1 doo.com 6379 > setrange name 6
/ / if the seventh position of the string name is changed to doo.com, if the replacement does not have the original length, the original end still exists, for example: name=abcdefg, setrange name 2 12, then name becomes a12defg
Redis 127.0.0.1 6379 > mset key1 doo key2 ya / / batch set key-value pair
Redis 127.0.0.1 6379 > msetnx / / to determine whether a batch exists. As long as one exists, all settings are unsuccessful.
Redis 127.0.0.1 key 6379 > getset key 30 / / get the value of key and reassign the value to key
Redis 127.0.0.1 6379 > mget / / get values in batches, empty if available
Redis 127.0.0.1 key7 6379 > incr key7 / / A pair of key7 increments 1. If key7 does not exist, the default is 0.
Redis 127.0.0.1 key6 6379 > incrby key6 5 / / one pair of key6 self-increment 5
Redis 127.0.0.1 key6 6379 > incrby key6-4 / / one pair of key6 minus 4
Redis 127.0.0.1 key6 6379 > decr key6 1 / / one pair of key6 minus 1
Redis 127.0.0.1 key6 6379 > decr key6-2 / / one pair of key6 self-increment 2
Redis 127.0.0.1 net 6379 > append name net / / spell the string net after the value of name
Redis 127.0.0.1 name 6379 > strlen name / / View the length of the value of the name
2. Hash data type, which is a mapping table of field field and values, which takes up less memory.
Redis 127.0.0.1 myhash 6379 > hset myhash field1 hello / / is a hash table
Redis 127.0.0.1 field1 6379 > hget myhash field1 / / get the value of the field1 of the myhash table
Redis 127.0.0.1 redis 6379 > hsetnx myhash hello / / determines whether it exists. If it exists, it returns 0. If it does not exist, it is assigned.
Redis 127.0.0.1 6379 > hmget myhash field1 hello filed2 word / / batch assignment
Redis 127.0.0.1 6379 > hmget myhash filed1 filed2 / / get values in batch
Redis 127.0.0.1 age 6379 > hincrby user:003 age 5 / / A pair of age self-increments for user:003 tables 5
Redis 127.0.0.1 age 6379 > hexists user:003 age / / determines whether age exists. If it exists, it returns 0, otherwise it returns 1.
Redis 127.0.0.1 redis 6379 > hlen user:001 / / returns the number of keys in the hash table
Redis 127.0.0.1 age 6379 > hdel myhash age / / A pair of age fields in the myhash table are deleted. A successful deletion is returned.
Redis 127.0.0.1 myhash 6379 > hkeys myhash / / returns all fields of the myhash table, no value is returned
Redis 127.0.0.1 myhash 6379 > hvals myhash / / returns all values of the myhash table, no fields
Redis 127.0.0.1 myhash 6379 > hgetall myhash / / returns all keys and values of the myhash table
3. List data type
Redis 127.0.0.1 word 6379 > lpush mylist "word" / / add an element from the header
Redis 127.0.1 hello 6379 > lpush mylist "hello" / /
Redis 127.0.0.1 redis 6379 > lrange mylist 0-1 / / take from the head to the tail first
Redis 127.0.0.1 be 6379 > rpush mylist "be" / / add an element from the tail
Redis 127.0.0.1 two 6379 > linsert mylist before "one"two" / / add an element to the front of the one element, preceded by the header.
127.0.0.1 one 6379 > lpush list2 one// inserts the value one into the list2
(integer) 1
127.0.0.1 purl 6379 > lpush list2 two
(integer) 2
127.0.0.1 purl 6379 > lpush list2 three
(integer) 3
127.0.0.1 lrange list2 6379 > all values of output list2
1) "three"
2) "two"
3) "one"
127.0.0.1 lset list2 6379 > list2 1 four / / change the two of list2 to four insert data is inserted from the head, and the newly inserted data angle is marked 0
OK
127.0.0.1 lrange list2 6379 > 0-1
1) "three"
2) "four"
3) "one"
127.0.0.1 lrem list2 6379 > one// removes 3 elements from list2 that are the same as one
(integer) 1
127.0.0.1Plus 6379 > ltrim list2 2-1 beat / keep the third to the end element
OK
127.0.0.1 rpoplpush list1 list2// adds the tail of list1 to the head of list2
127.0.0.1 6379 > lindex list2 0 lindex list2 / returns the value of the first position of the list2
127.0.0.1 6379 > llen list2// returns the number of elements in list2
IV. Collection of sets data types
127.0.0.1 hello 6379 > sadd myset1 hello// adds the hello element to the collection myset1
(integer) 1
127.0.0.1 6379 > smembers myset1// view all elements of myset1
1) "hello"
127.0.0.1 purl 6379 > sadd myset1 one
(integer) 1
127.0.0.1 one 6379 > srem myset1 one// removes the one element of myset1
(integer) 1
127.0.0.1 6379 > spop myset1// randomly removes an element from the myset1
127.0.0.1 sdiff myset1 myset2// 6379 > returns the difference between the two sets, using the previous set as the standard, returning elements in the previous set that are not in set 2
127.0.0.1 6379 > sdiffstore myset4 myset2 myset3// stores the gap between sets 2 and 3 in set 4
127.0.0.1 sinter myset1 myset2// 6379 > take the intersection of set 1 and set 2
127.0.0.1 6379 > sinterstore myset3 myset1 myset2// stores the intersection of set 1 and set 2 in set 3
127.0.0.1 6379 > sunion myset1 myset2// takes the union of set 1 and set 2
127.0.0.1 sunionstore myset3 myset1 myset2 6379 > the union of set 1 and set 2 is stored in set 3
127.0.0.1 smove myset2 myset3 three 6379 > move the three of set 2 to set 7
127.0.0.1 6379 > scard myset1// returns the number of elements in set 1
127.0.0.1one 6379 > sismember myset1 one// tests whether there is an one in the myset1. If so, it returns 1, otherwise 0.
127.0.0.1 srandmember myset1// 6379 > randomly returns an element in set 1 without deletion
5. Ordered set zset
127.0.0.1 one// 6379 > zadd myzset1 1 add zset1
(integer) 1
127.0.0.1 6379 > zadd myzset1 2 two
(integer) 1
127.0.0.1 6379 > zadd myzset1 3 two
(integer) 0
127.0.0.1 zrange myzset1 6379 > 0-1 beat / View all elements of an ordered set
1) "one"
2) "two"
127.0.0.1 withscores// 6379 > zrange myzset1 0-1 withscores// plus withscores realizable element serial number
1) "one"
2) "5"
127.0.0.1 zrem myzset1 two// 6379 > delete elements in the collection
127.0.0.1 one// 6379 > zincrby myzset1 2 add 2 to the serial number of one
127.0.0.1 one 6379 > zincrby myzset1-2 one / / Serial number of a pair of one-2
127.0.0.1 zrank myzset1 three// 6379 > returns the index value of three, with the index value starting at 0 and the sequence number starting at 1
127.0.0.1 purl 6379 > zrank myzset1 three
(integer) 2
127.0.1 zrevrank myzset1 three// 6379 > descend the order first, and then find the index
(integer) 0
127.0.0.1 6379 > zrange myzset1 0-1 withscores// view elements and real serial numbers
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
127.0.0.1 withscores// 6379 > zrevrange myzset1 0-1 descend first, and then check the element and serial number
1) "three"
2) "3"
3) "two"
4) "2"
5) "one"
6) "1"
127.0.0.1 6379 > zrangebyscore myzset1 2 3 withscores// look for elements with serial numbers 2 and 3
1) "two"
2) "2"
3) "three"
4) "3"
127.0.0.1 zcount myzset1 6379 > number of elements in the return sequence from 2 to 4
127.0.0.1 zcard myzset1 6379 > number of ordered set elements returned
127.0.0.1 zrange myzset1 6379 > 0-1 swap / look at the index first
127.0.0.1 6379 > zremrangebyrank myzset1 0 1 / / Delete elements with index values from 0 to 1
127.0.0.1 6379 > zremrangebyscore myzset1 2 5ram / delete elements with serial numbers 2 to 5
Element expires:
127.0.0.1 6379 > set age 20 / set the value of age to 20
OK
127.0.0.1 get age 6379 > get the value of age
"20"
127.0.0.1 expire age 6379 > 20 expire age / set age value stored in 20 seconds
(integer) 1
127.0.0.1 ttl age// 6379 > get the remaining existence time, second
(integer) 16
Key-value related commands:
127.0.0.1 6379 > keys * / / returns all keys
127.0.0.1 keys my* 6379 > return all keys at the beginning of my
127.0.0.1 exists name 6379 > check whether the name key exists. If it exists, it returns 1, otherwise it returns 0.
127.0.0.1 6379 > del age / / remove age key
127.0.0.1 expire age 6379 > expired / / setting expired
127.0.0.1 6379 > persist age / / cancel expiration setting
127.0.0.1 move age 6379 > move the key age to database 1 /
There are a total of 16 databases. The default is 0 from 0-15.
127.0.0.1 select 6379 > choose to enter database 0
127.0.0.1 6379 > randomkey randomly returns a key
127.0.0.1 rename age age.new 6379 > rename the key
127.0.0.1 type age the data type of the returned value (string, zset, list, etc.)
Server-related commands:
127.0.0.1 ping test connection to the server is normal, normal pong is returned
127.0.0.1 6379 > echo is similar to echo
127.0.0.1 6379 > select 2 Select Database 2
127.0.0.1 quit 6379 > exit / / or use exit
127.0.0.1 dbsize 6379 > number of current library keys returned
127.0.0.1 info 6379 > get the information of redis
127.0.0.1 6379 > config get * / / returns all current configuration items
127.0.0.1 6379 > CONFIG GET timeout// returns the parameter value of the timeout configuration item
1) "timeout"
2) "0"
127.0.0.1 6379 > flushdb / / Delete all key in the current library
127.0.0.1 flushall 6379 > delete all key of all libraries, no matter under which library