1, Connect
Start service
./redis-server redis.conf
connect
./redis-cli -h 127.0.0.1 -p 6379
perhaps
./redis-cli
2, Redis data types and application scenarios
Redis is a key value storage system written in ANSI C language.
Note: in Redis, the command ignores case, (set), and the key does not ignore case (name)
2.1 Key design
The type of key is string.
1. Use: split
2. Convert the table name to the key prefix, for example: user:
3. Place the primary key value in the second segment
4. Placing column names in the third paragraph
For example:
Key user of email: 9: email
2.2 value
The data types of value are:
- frequently-used:
String string type, list list type, set set type, sortedset (zset) ordered set type, hash.
- Unusual:
bitmap type, geo location type.
Redis5.0 added a new type: stream type
3, value details
3.1 string type
Express three types of values: string, integer and floating point number. 100.01 is a six bit string
3.1.1 application scenario
incr for optimistic lock
incr: increment number, which can be used to implement optimistic lock watch (transaction)
setnx for distributed locks
When value does not exist, assignment is adopted, which can be used to realize distributed locking
3.1.2} command line command
incr For optimistic lock incr: Incremental number, which can be used to implement optimistic locking watch(affair) setnx For distributed locks When value Assignment is adopted when it does not exist, which can be used to realize distributed locking 127.0.0.1:6379> setnx name zhangf #If name does not exist (integer) 1 127.0.0.1:6379> setnx name zhaoyun #Assignment again failed (integer) 0 127.0.0.1:6379> get name "zhangf" Normal assignment 127.0.0.1:6379> set age 18 NX PX 10000 #If there is no assignment, it is valid for 10 seconds OK 127.0.0.1:6379> set age 20 NX #Assignment failed (nil) 127.0.0.1:6379> get age #age failure (nil) 127.0.0.1:6379> set age 30 NX PX 10000 #Assignment succeeded OK 127.0.0.1:6379> get age "30"
3.1.3} command name and usage
3.2 list type
The list type can store ordered and repeatable elements
Getting records near the head or tail is extremely fast
The maximum number of elements in the list is 2 ^ 32-1 (4 billion)
3.2.1 application scenario
1. Use lists as stacks or queues. Ordered lists can be used as stacks and queues
2. It can be used for various lists, such as user list, product list, comment list, etc
3.2.2} command line command
127.0.0.1:6379> lpush list:1 1 2 3 4 5 3 (integer) 5 127.0.0.1:6379> lrange list:1 0 -1 1) "5" 2) "4" 3) "3" 4) "2" 5) "1" 127.0.0.1:6379> lpop list:1 # Start from 0 "5" 127.0.0.1:6379> rpop list:1 "1" 127.0.0.1:6379> lindex list:1 1 "3" 127.0.0.1:6379> lrange list:1 0 -1 1) "4" 2) "3" 3) "2" 127.0.0.1:6379> lindex list:1 1 "3" 127.0.0.1:6379> rpoplpush list:1 list:2 "2" 127.0.0.1:6379> lrange list:2 0 -1 1) "2" 127.0.0.1:6379> lrange list:1 0 -1 1) "4" 2) "3"
3.2.3} command name and usage
3.3 set type
Set: unordered, unique element
The maximum number of members in the collection is 2 ^ 32 - 1
3.3.1 application scenario
Applicable to data structures that cannot be repeated and do not require order
For example, users who are concerned can also draw random prizes through spop
3.3.2} command line command
127.0.0.1:6379> sadd set:1 a b c d (integer) 4 127.0.0.1:6379> smembers set:1 1) "d" 2) "b" 3) "a" 4) "c" 127.0.0.1:6379> srandmember set:1 "c" 127.0.0.1:6379> srandmember set:1 "b" 127.0.0.1:6379> sadd set:2 b c r f (integer) 4 127.0.0.1:6379> sinter set:1 set:2 1) "b" 2) "c" 127.0.0.1:6379> spop set:1 "d" 127.0.0.1:6379> smembers set:1 1) "b" 2) "a" 3) "c"
3.3.3} command name and usage
3.4 sortedset ordered set type
SortedSet(ZSet) ordered set: the elements themselves are unordered and not repeated
Each element is associated with a score
It can be sorted by score, and the score can be repeated
3.4.1 application scenario
Because it can be sorted by score, it is suitable for all kinds of leaderboards. For example: click on the leaderboard, sales leaderboard, pay attention to the leaderboard, etc
3.4.2} command line command
127.0.0.1:6379> zadd hit:1 100 item1 20 item2 45 item3 (integer) 3 127.0.0.1:6379> zcard hit:1 (integer) 3 127.0.0.1:6379> zscore hit:1 item3 "45" 127.0.0.1:6379> zrevrange hit:1 0 -1 1) "item1" 2) "item3" 3) "item2" 127.0.0.1:6379>
3.4.3} command name and usage
3.5 hash type (hash table)
Redis hash is a mapping table of field and value of string type, which provides the mapping of field and field value. Each hash can store 2 ^ 32 - 1 key value pairs (more than 4 billion).
3.5.1 application scenario
Storage of objects and mapping of table data
3.5.2} command line command
127.0.0.1:6379> hmset user:001 username zhangfei password 111 age 23 sex M OK 127.0.0.1:6379> hgetall user:001 1) "username" 2) "zhangfei" 3) "password" 4) "111" 5) "age" 6) "23" 7) "sex" 8) "M" 127.0.0.1:6379> hget user:001 username "zhangfei" 127.0.0.1:6379> hincrby user:001 age 1 (integer) 24 127.0.0.1:6379> hlen user:001 (integer) 4
3.5.3 command name and usage
3.6 bitmap type
bitmap performs bit operations. A bit bit represents the value or state corresponding to an element, and the key is the corresponding element itself. bitmap itself will greatly save storage space.
3.6.1 application scenario
1. The user signs in every month. The user id is key, and the date is taken as the offset. 1 indicates sign in
2. Count active users. The date is key, the user id is offset, and 1 indicates active
3. Query the user's online status. The date is key, the user id is offset, and 1 means online
3.6.2} command line command
127.0.0.1:6379> setbit user:sign:1000 20200101 1 #User id 1000 20200101 sign in (integer) 0 127.0.0.1:6379> setbit user:sign:1000 20200103 1 #User id 1000 20200103 sign in (integer) 0 127.0.0.1:6379> getbit user:sign:1000 20200101 #Obtain the sign in status of user 20200101 with id 1000 1 Sign in (integer) 1 127.0.0.1:6379> getbit user:sign:1000 20200102 #Obtain the sign in status of user 20200102 with id 1000 0 Indicates no sign in (integer) 0 127.0.0.1:6379> bitcount user:sign:1000 # Get the number of user check-in times with id 1000 (integer) 2 127.0.0.1:6379> bitpos user:sign:1000 1 #The first check-in date of the user with id 1000 (integer) 20200101 127.0.0.1:6379> setbit 20200201 1000 1 #User 1000 of 20200201 goes online (integer) 0 127.0.0.1:6379> setbit 20200202 1001 1 #User 1000 of 20200202 goes online (integer) 0 127.0.0.1:6379> setbit 20200201 1002 1 #User 1002 of 20200201 goes online (integer) 0 127.0.0.1:6379> bitcount 20200201 #There are 2 online users in 20200201 (integer) 2 127.0.0.1:6379> bitop or desk1 20200201 20200202 #Merge 20200201 users and 20200202 Online Selected users (integer) 126 127.0.0.1:6379> bitcount desk1 #Statistics show that 20200201 and 20200202 are online Number of households (integer) 3
3.6.3} command name and usage
3.7} geo location type
geo is used by Redis to process location information.
On redis3 2 official use. It mainly uses Z-order curve, Base32 coding and geohash algorithm
3.7.1 application scenario
1. Record geographic location
2. Calculate distance
3. Find "people nearby"
3.7.2} command line command
127.0.0.1:6379> geoadd user:addr 116.31 40.05 zhangf 116.38 39.88 zhaoyun 116.47 40.00 diaochan #Add the longitude and latitude of user addresses zhangf, zhaoyun and diaochan (integer) 3 127.0.0.1:6379> geohash user:addr zhangf diaochan #Obtain the geohash code of zhangf and diaochan 1) "wx4eydyk5m0" 2) "wx4gd3fbgs0" 127.0.0.1:6379> geopos user:addr zhaoyun #Obtain the latitude and longitude of zhaoyun 1) 1) "116.38000041246414185" 2) "39.88000114172373145" 127.0.0.1:6379> geodist user:addr zhangf diaochan #Calculate the distance from zhangf to diaochan, single Bit is m "14718.6972" 127.0.0.1:6379> geodist user:addr zhangf diaochan km #Calculate the distance from zhangf to diaochan, Unit is km "14.7187" 127.0.0.1:6379> geodist user:addr zhangf zhaoyun km "19.8276" 127.0.0.1:6379> georadiusbymember user:addr zhangf 20 km withcoord withdist count 3 asc # Obtain the name, distance, longitude and latitude of the top three members within 20km from zhangf in order from near to far #Withclock: get latitude and longitude withlist: get distance withhash: get geohash code 1) 1) "zhangf" 2) "0.0000" 3) 1) "116.31000012159347534" 2) "40.04999982043828055" 2) 1) "diaochan" 2) "14.7187" 3) 1) "116.46999925374984741" 2) "39.99999991084916218" 3) 1) "zhaoyun" 2) "19.8276" 3) 1) "116.38000041246414185" 2) "39.88000114172373145"
3.7.3} command name and usage
3.6 stream data flow type
stream is redis5 The data structure added after 0 is used for persistent message queues.
Almost all the contents of message queue are met, including:
- Serialization generation of message ID
- Message traversal
- Blocking and non blocking reads of messages
- Packet consumption of messages
- Unfinished message processing
- Message queue monitoring
Each Stream has a unique name, which is the key of Redis. It is automatically created when the xadd instruction is used to append messages for the first time.
3.6.1 application scenario
Use of message queues
3.6.2} command line command
127.0.0.1:6379> xadd topic:001 * name zhangfei age 23 "1591151905088-0" 127.0.0.1:6379> xadd topic:001 * name zhaoyun age 24 name diaochan age 16 "1591151912113-0" 127.0.0.1:6379> xrange topic:001 - + 1) 1) "1591151905088-0" 2) 1) "name" 2) "zhangfei" 3) "age" 4) "23" 2) 1) "1591151912113-0" 2) 1) "name" 2) "zhaoyun" 3) "age" 4) "24" 5) "name" 6) "diaochan" 7) "age" 8) "16" 127.0.0.1:6379> xread COUNT 1 streams topic:001 0 1) 1) "topic:001" 2) 1) 1) "1591151905088-0" 2) 1) "name" 2) "zhangfei" 3) "age" 4) "23" #Created group1 127.0.0.1:6379> xgroup create topic:001 group1 0 OK # Create cus1 and join group1 to consume the first message that has not been consumed 127.0.0.1:6379> xreadgroup group group1 cus1 count 1 streams topic:001 > 1) 1) "topic:001" 2) 1) 1) "1591151905088-0" 2) 1) "name" 2) "zhangfei" 3) "age" 4) "23" #Continued consumption article 2 127.0.0.1:6379> xreadgroup group group1 cus1 count 1 streams topic:001 > 1) 1) "topic:001" 2) 1) 1) "1591151912113-0" 2) 1) "name" 2) "zhaoyun" 3) "age" 4) "24" 5) "name" 6) "diaochan" 7) "age" 8) "16" #There is nothing to consume 127.0.0.1:6379> xreadgroup group group1 cus1 count 1 streams topic:001 > (nil)
3.6.3} command name and usage
4, Redis other common commands
Official order website: http://www.redis.cn/commands.html
4.1 scan
Range query
scan 0 match * count 1000
4.2 exists
redis 127.0.0.1:6379> exists HongWan (integer) 0 redis 127.0.0.1:6379> exists age (integer) 1 redis 127.0.0.1:6379>
4.3 expire
redis 127.0.0.1:6379> set test 1 set up test The value of is 1 OK redis 127.0.0.1:6379> get test obtain test Value of "1" redis 127.0.0.1:6379> EXPIRE test 5 set up test The survival time is 5 seconds (integer) 1 redis 127.0.0.1:6379> TTL test see test The generation time of has 1 second to delete (integer) 1 redis 127.0.0.1:6379>redis 127.0.0.1:6379> TTL test (integer) -2 redis 127.0.0.1:6379> get test obtain test The value of has been deleted (nil)
4.4 rename
redis 127.0.0.1:6379> rename age age_new OK redis 127.0.0.1:6379> scan 0 match * count 100 1) "age_new" redis 127.0.0.1:6379
4.5 type
Displays the data type of the specified key
redis 127.0.0.1:6379> type addr string redis 127.0.0.1:6379> type myzset2 zset redis 127.0.0.1:6379> type mylist list
4.6 encoding
Displays the data type of the specified value
127.0.0.1:6379> object encoding name "embstr"