This is the first day of my participation in the More text Challenge. For details, see more text Challenge

This article is participating in the “Java Theme Month – Java Development in action”. See the link to the event for more details


Related articles

Redis combat summary: Redis combat


An overview,

  • What is Redis? What?

Redis (Remote Dictionary Server) Is an open source, net-based, memory-persistent-based logging, key-value database written in ANSI C, and provides apis in multiple languages.Redis periodically writes updated data to disk or changes to appended record files, and implements master-slave synchronization on this basis. Free and open source! Is one of the hottest NoSQL technologies right now! Also known as structured database!

  • What can Redis do?

1, memory storage, persistence, memory is lost, so persistence is important (RDB, Aof) 2, high efficiency, can be used for caching 3, publish and subscribe system 4, map information analysis 5, timers, counters (pageviews!) 6,…

2. Basic understanding of Redis

For an installation tutorial, see my other blog post: How to install Redis in Widows and Linux.

  1. First of all, we can see the official document of Redis:

① English documentsClick the jump. (2) Chinese documentsClick the jump. 2. Redis-key Briefly introduces the operation commands of Redis squadron Key. I hope you can follow the comments to type again, a simple note, are the most commonly used commands!

127.0. 01.:6379> ping # Check whether the current connection is normal. If it is normal, return PONG PONG127.0. 01.:6379> clear # Clear the current console (to better see the commands entered below)127.0. 01.:6379> keys * #1) "db"
127.0. 01.:6379> FLUSHALL # FLUSHALL127.0. 01.:6379> keys * 
(empty array)
127.0. 01.:6379> setSelect * from 'dingdada' where key = 'name' and value = 'dingdada'127.0. 01.:6379> select * from 'name' where key = 'name'"dingdada"
127.0. 01.:6379> keys *
1) "name"
127.0. 01.:6379> set name1 dingdada2
OK
127.0. 01.:6379> get name1
"dingdada2"
127.0. 01.:6379> keys * #1) "name1"
2) "name"
127.0. 01.:6379> EXISTS name # Specifies whether the current key EXISTS (INTEGER)1
127.0. 01.:6379> move name 1# Remove current library1Data whose key is' name '(INTEGER)1
127.0. 01.:6379> keys *
1) "name1"
127.0. 01.:6379> FLUSHALL # FLUSHALL # FLUSHALL # FLUSHALL # FLUSHALL127.0. 01.:6379> set name dingdada
OK
127.0. 01.:6379> set name1 dingdada1
OK
127.0. 01.:6379> set name2 dingdada2
OK
127.0. 01.:6379> EXPIRE name 15# Set the expiration time of data whose key is' name 'to15Second unit seconds (integer)1
127.0. 01.:6379> TTL name # Check the remaining life cycle time (integer)13
127.0. 01.:6379> ttl name
(integer)12 127.0.0.1:6379 > TTLname
(integer)11 127.0.0.1:6379 > TTLname
(integer)8 127.0.0.1:6379 > TTLname
(integer)6 127.0.0.1:6379 > TTLname
(integer)3 127.0.0.1:6379 > TTLname
(integer)2 127.0.0.1:6379 > TTLname
(integer)1 127.0.0.1:6379 > TTLname
(integer)0 127.0.0.1:6379> TTL name # If -2 is returned, the key has expired(integer)-2 127.0.0.1:6379> get name #(nil)127.0.0.1:6379 > type name1string127.0.0.1:6379 >Copy the code

If encountered will not command! Remember to check Redis Chinese website, there is an official document! Link above, you can click to jump ~

Five data types of Redis

  1. String (String)

(1) Add, query, append, and Obtain length, and check whether operations exist

127.0. 01.:6379> setSelect * from 'dingdada' where key = 'name' and value = 'dingdada'127.0. 01.:6379> select * from 'name' where key = 'name'"dingdada"
127.0. 01.:6379> get key1
"hello world!"
127.0. 01.:6379> keys * #1) "name"
127.0. 01.:6379> EXISTS name # Specifies whether the data whose key is' name 'EXISTS1
(integer) 1
127.0. 01.:6379> EXISTS name1 # Does not exist return0
(integer) 0
127.0. 01.:6379> APPEND name1 dingdada1 # APPEND to 'name'; APPEND to 'name'; APPEND to 'name'; APPEND to 'dingdada1'setName1 dingdada1, and returns the total length of the data (integer)9
127.0. 01.:6379> get name1
"dingdada1"
127.0. 01.:6379> select * from 'name1' where key = 'name1';9
127.0. 01.:6379> APPEND name1,dingdada2 # APPEND, '+' if key exists, return total length (integer)19
127.0. 01.:6379> STRLEN name1
(integer)19 127.0.0.1:6379> get name1 "dingdada1,dingdada2"setkey1 "hello world!" Note: If there is a space in the data inserted, please use "" double quotation marks, otherwise an error will be reported! OK 127.0.0.1:6379 >setkey1 hello world! # Error because the space is the delimiter in Redis(error)ERR syntax error 127.0.0.1:6379 >setkey1 hello,world! # comma is OKCopy the code

(2) self-increasing and self-decreasing operation

127.0. 01.:6379> set num 0# Insert an initial value of0The data is OK127.0. 01.:6379> get num
"0"
127.0. 01.:6379> incr num # Select * from 'num'1, which returns the Java equivalent of i++ (integer)1
127.0. 01.:6379> < span style = "box-sizing: border-box; color: RGB (74, 74, 74)"1"
127.0. 01.:6379> incr num
(integer)2 127.0.0.1:6379 > incrnum
(integer)3 127.0.0.1:6379> get num "3" 127.0.0.1:6379> decr num #(integer)2 127.0.0.1:6379 > decrnum
(integer)1 127.0.0.1:6379 > decrnum
(integer)0 127.0.0.1:6379> decr num #(integer)-1 127.0.0.1:6379> decr num #(integer)- 2 127.0.0.1:6379 > decrnum
(integer)-3 127.0.0.1:6379> INCRBY num 10 #(integer) 7
127.0.0.1:6379> INCRBY num 10
(integer)17 127.0.0.1:6379> DECRBY num 3 #(integer) 14
127.0.0.1:6379> DECRBY num 3
(integer) 11
Copy the code

③ Intercept and replace the string operation

# interception127.0. 01.:6379> set key1 "hello world!"
OK
127.0. 01.:6379> get key1
"hello world!"
127.0. 01.:6379> GETRANGE key1 0 4# Intercept a string, equivalent to subString in Java, with subscript from0At first, the original data will not change"hello"
127.0. 01.:6379> get key1
"hello world!"
127.0. 01.:6379> GETRANGE key1 0 - 1  #0to- 1It's the same thing as get key1, it's the same thing, it gets the whole piece of data"hello world!"# replace127.0. 01.:6379> set key2 "hello,,,world!"
OK
127.0. 01.:6379> get key2
"hello,,,world!"
127.0. 01.:6379> SETRANGE key2 5 888This statement is similar to the Java version of replace, with subscripts from0In Java, the replacement character is specified, while in Redis, the replacement character is specified, and the replacement data is the same as the length of the replacement, and the return value is the length of the replacement (integer).14
127.0. 01.:6379> get key2
"hello888world!"
127.0. 01.:6379> SETRANGE key2 5 67# Only two bits (integer) are replaced here14
127.0. 01.:6379> get key2
"hello678world!"
Copy the code

(4) Set the expiration time. No setting operation is performed

Setex expires at the time of creation. Setex expires at the time of creation. Setex expires at the time of creation127.0. 01.:6379> setex name1 15Dingdada # create key 'name1', value 'dingdada', expiration time '15The string data for seconds is OK127.0. 01.:6379> TTL name1 # Check the expiration time (integer) of key 'name1'6
127.0. 01.:6379> ttl name1
(integer)5 127.0.0.1:6379 > TTLname1
(integer)3 127.0.0.1:6379 > TTLname1
(integer)1 127.0.0.1:6379 > TTLname1
(integer)0 127.0.0.1:6379> TTL name1 # Returns -2 to prove that the key has expired, that is, does not exist(integer)-2 # does not exist set 127.0.0.1:6379> setnx name2 dingdadA2 # If key 'name2' does not exist, add data, return value 1 to prove success(integer)1 127.0.0.1:6379> get name2" dingdada2" 127.0.0.1:6379> keys * 1) "name2" 127.0.0.1:6379> setnx name2" dingdada3" If key 'name2' already exists, the setting fails and the value 0 is returnedsetThe difference is:setSetnx does not. If it exists, it is not set, ensuring data misoperation(integer) 0
127.0.0.1:6379> get name2
"dingdada2"
Copy the code

⑤ Mset and mGET operations

127.0. 01.:6379> mset k1 v1 k2 v2 k3 v3127.0. 01.:6379> keys * #1) "k2"
2) "k3"
3) "k1"
127.0. 01.:6379< span style = "box-size: border-box; color: RGB (74, 74, 74); font-size: 14px! Important; white-space: normal;1) "v1"
2) "v2"
3) "v3"
127.0. 01.:6379> MSETNX k1 v1 k4 v4  # msetNx is an atomic operation that guarantees transactions to a certain extent! Either we all succeed or we all fail! The equivalent ofif&& (and)
(integer) 0
127.0. 01.:6379> keys *
1) "k2"
2) "k3"
3) "k1"
127.0. 01.:6379> MSETNX k5 v5 k4 v41
127.0. 01.:6379> keys *
1) "k2"
2) "k4"
3) "k3"
4) "k5"
5) "k1"
Copy the code

⑥ Add the operations of obtaining objects and getSet

This is essentially a string, but it's a clever design of the key. ##mset student:1:name student equals class name, 1 equals ID, name equals attributeIf all the required data is designed like this, then we do not need to pay attention to too much key in the Java business code.127.0. 01.:6379> mset student:1:name dingdada student:1:age 22# add key 'student' :1:name ', value is' dingdada '. Data such as OK127.0. 01.:6379> keys * #1) "student:1:age"
2) "student:1:name"
127.0. 01.:6379> mget student:1:age student:1:name # Get data1) "22"
2) "dingdada"

## getset operation
127.0. 01.:6379> getset name1 dingdada1set, first get the key, if not,setValue goes in, returns the value of get (nil).127.0. 01.:6379> get name1
"dingdada1"
127.0. 01.:6379> getset name1 dingdada2 ##setThe most recent value is added, returning the value of get"dingdada1"
127.0. 01.:6379< span style = "box-sizing: border-box; color: RGB (74, 74, 74)"dingdada2"
Copy the code

String is the most commonly used data type in Redis. It is also the simplest data type in Redis. First of all, it is ostensibly a string, but in fact it can flexibly represent three kinds of values: string, integer, and floating point. Redis automatically recognizes these three values.


The road ahead is long, but I see no ending. I will continue to search for more and more. Because space is limited, I will write another article for the rest. If you think the blogger is good! Writing is not easy, please like, follow, comment to give the blogger a encouragement ~