Java redis Series 2 (installation and use of redis + implementation of redis persistence)
What is redis?
Redis is an open source and high-performance key value database developed with C language. It officially provides test data, and 50 requests execute 100000 requests concurrently. The read speed is 110000 times / s, and the write speed is 81000 times / s. redis can adapt to the storage requirements in different scenarios by providing multiple key value data types. So far, the key value data types supported by redis are as follows:
- String type string
- Hash type hash
- List type list
- Set type
- Ordered set type sortedset
Application scenarios of redis
1. Cache (data query, short link, news content, commodity content)
2. Online friends in chat room
3. Task queue (second kill, rush purchase)
4. Application ranking
5. Website visit statistics
6. Data overdue processing
7. Session separation in distributed cluster architecture
Download and install
- Official website:https://redis.io
- Chinese website:http://www.redis.net.cn/
- Decompression can be used directly:
* redis.windows.conf : Profile
* redis- cli.exe : redis client
* redis- server.exe : redis server
Redis data structure
Brothers who have read my last article should know that redis is data in the format of key and value, where key is a string
There are five different data structures
graphic
Data structure type of value
1. String type string
2. Hash type hash: map format
3. List type list: LinkedList format, supporting duplicate elements
4. Set type: duplicate elements are not allowed
5. Ordered set type: sortedset: repeated elements are not allowed, and the elements are ordered
Command operation
Let’s open the server first
Open the client again
1. String type
1.1 store set key value
1.2 getting get value
demonstration
2. Hash type
1. Storage: hset key field value
2. Get: hget key field
3. Get all the keys and valus: hgetall myhash
4. Delete: HDEL key field
demonstration
3. List type
You can add an element with the head or tail of a list
1. Lpush key value: adds an element to the left table of the list
2. Rpush key value: adds an element to the right table of the list
3. Get: lrange key start end: get range
4. Delete the leftmost element in the list and return it to: lpop key
5. Delete the rightmost element in the list and return it to: rpop key
demonstration
//Insert element from left
127.0.0.1:6379> lpush list a
(integer) 1
127.0.0.1:6379> lpush list b
(integer) 2
//Get all elements
127.0.0.1:6379> lrange list 0 -1
1) "b"
2) "a"
127.0.0.1:6379> rpush list c
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "b"
2) "a"
3) "c"
//Remove the leftmost element of the list
127.0.0.1:6379> lpop list
"b"
127.0.0.1:6379> lrange list 0 -1
1) "a"
2) "c"
//Remove the rightmost element of the list
127.0.0.1:6379> rpop list
"c"
127.0.0.1:6379> lrange list 0 -1
1) "a"
127.0.0.1:6379>
Set type: duplicate elements are not allowed
1. Stored data Sadd key value
2. Delete data SREM key value
3. Get data: smembers key gets all the elements in the set
//Add data
127.0.0.1:6379> sadd set 1
(integer) 1
127.0.0.1:6379> sadd set 2
(integer) 1
//Get all elements
127.0.0.1:6379> smembers set
1) "1"
2) "2"
//Delete the specified element
127.0.0.1:6379> srem set 1
(integer) 1
127.0.0.1:6379> smembers set
1) "2"
127.0.0.1:6379>
Ordered set type sortedset
Repeated elements are not allowed, and the elements are ordered. Each element is associated with a score of double type. Redis is to sort the members of a set from small to large by fractions.
//Insert data
127.0.0.1:6379> zadd sortedset 1 zhangsan
(integer) 1
127.0.0.1:6379> zadd sortedset 5 lisi
(integer) 1
127.0.0.1:6379> zadd sortedset 2 wangwu
(integer) 1
//Get data
127.0.0.1:6379> zrange sortedset 0 -1
1) "zhangsan"
2) "wangwu"
3) "lisi"
//Remove specified data
127.0.0.1:6379> zrem sortedset wangwu
(integer) 1
127.0.0.1:6379> zrange sortedset 0 -1
1) "zhangsan"
2) "lisi"
127.0.0.1:6379>
General Command
Keys *: get all keys
Type key: get the type of value corresponding to the key
Del key: delete the specified key value
Redis persistence
Why persistence?
Redis is a memory database. When the redis server restarts, the data will be lost, so we need to persist the redis data to the hard disk file
Persistence mechanism of redis
1. RDB: the default mode. No configuration is required. This mechanism is used by default
In a certain time interval, through the detection of key changes, and then persistent data
1.1 editing redis.windows.conf file
save 900 1
At least one of them changes after 15 minutes
save 300 10
After five minutes, at least 10 of them changed
save 60 10000
At least 10000 changes in a minute
1.2 restart the redis server and specify the name of the configuration file
D:\JavaWeb2018\day23_ Redis / information / redis / windows-64 / redis-2.8.9 > redis- server.exe redis.windows.conf
Aof: logging mode, which can record the operation of each command. You can persist data after each command operation
1. Editing redis.windwos.conf file
Appendonly no (close AOF) - > appendonly Yes (open AOF)
#Appendfsync always: every operation is persistent
Appendfsync everysec: persistent every second
#Appendfsync No: no persistence