Java redis Series 2 (installation and use of redis + implementation of redis persistence))


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:

  1. String type string
  2. Hash type hash
  3. List type list
  4. Set type
  5. 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

  1. Official website:
  2. Chinese website:
  3. Decompression can be used directly:


* : 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



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
在这里插入图片描述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
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


//Insert element from left> lpush list a
(integer) 1> lpush list b
(integer) 2
//Get all elements> lrange list 0 -1
1) "b"
2) "a"> rpush list c
(integer) 3> lrange list 0 -1
1) "b"
2) "a"
3) "c"
//Remove the leftmost element of the list> lpop list
"b"> lrange list 0 -1
1) "a"
2) "c"
//Remove the rightmost element of the list> rpop list
"c"> lrange list 0 -1
1) "a">

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> sadd set 1
(integer) 1> sadd set 2
(integer) 1
//Get all elements> smembers set
1) "1"
2) "2"
//Delete the specified element> srem set 1
(integer) 1> smembers set
1) "2">

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> zadd sortedset 1 zhangsan
(integer) 1> zadd sortedset 5 lisi
(integer) 1> zadd sortedset 2 wangwu
(integer) 1
//Get data> zrange sortedset 0 -1
1) "zhangsan"
2) "wangwu"
3) "lisi"
//Remove specified data> zrem sortedset wangwu
(integer) 1> zrange sortedset 0 -1
1) "zhangsan"
2) "lisi">

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 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

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

These are some basic operations of Redis and how to persist. This is also the second chapter of my JAVA redis series. I will continue to update it later, and it will not be easy to write. Please welcome the old fellow to support it, feel that it can help you to collect it, I will update articles frequently, and also pay attention to me.