“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
Related articles
Redis combat summary: Redis combat
I. Detailed explanation of the configuration file
As the saying goes: there is no expert! Just do it! The previous study is just to let us know how to use Redis, but as developers, we need to understand the whys and wheres, so we need to fundamentally understand, let’s study the redis.conf file carefully and improve ourselves! Let go! In practice: a few small configurations can make you stand out!
- Unit: Redis configuration is case insensitive!
Note here: anything can be written, case insensitive.
units are case insensitive so 1GB 1Gb 1gB are all the same.
Copy the code
- Included: You can use includes to set up a Redis cluster
- Network:
The explanation is as follows:
bind 127.0. 01.# Binding IP addressprotected-mode yes # Protection mode port6379# Port SettingsCopy the code
- GENERAL GENERAL
Daemonize yes # runs as a daemon, default is no, we need to start it as yes! pidfile /var/run/redis_6379.pid # This can be one of: # debug (a lot of information, usefulfordevelopment/testing) # verbose (many rarely useful info, but not a mess like the debug level) # notice (moderately verbose, What you want in production probably) Production environment # Warning (only very important/critical messages are logged) Loglevel notice logfile""# log file location name databases16# Number of databases, default is16Always -show-logo yes # Specifies whether to always show the logoCopy the code
- Snapshot (RDB) : persistence, the number of times the operation is performed within a specified period of time will persist to the file.rdb.aof
Redis is an in-memory database. If it is not persistent, then data will be lost when power is cut off!
If within 900s, if there is at least one1Key has been modified, we and persisting operation save900 1If within 300s, if at least10Key has been modified, we and persisting operation save300 10If 60s, if at least10000Key has been modified, we and persisting operation save60 10000We'll learn about persistence later, and we'll define this test ourselves!Copy the code
- SECURITY safety
You can set a password for Redis here. By default, there is no password. ① Set the parameters using commands
127.0. 01.:6379> ping
PONG
127.0. 01.:6379> config get requirePass #1) "requirepass"
2) ""
127.0. 01.:6379> config set requirepass "123456"# Set Redis password to123456OK # Ctrl+C to exit the current connection [root@dyjcomputer bin]# redis-cli -p 6379# Reconnect127.0. 01.:6379> ping # Test ping, failed, all commands show no permission (error) NOAUTH Authentication required.127.0. 01.:6379> set k1 v1 # failed, all commands show no permission (error) NOAUTH Authentication required.127.0. 01.:6379> auth 123456#auth + password login to OK127.0. 01.:6379> ping # normal PONG127.0. 01.:6379> config get requirepass #1) "requirepass"
2) "123456"
Copy the code
② By modifying the configuration file, locate the position shown in the figure and add the password
Restart the Redis test!
127.0. 01.:6379> ping
(error) NOAUTH Authentication required.
127.0. 01.:6379> auth 123456
OK
127.0. 01.:6379> ping
PONG
Copy the code
- Limit the CLIENTS
maxclients 10000Maxmemory <bytes> # Redis configures maximum memory capacity maxmemory-policy noeviction # Specifies the processing policy when the memory limit is reached1,volatile-lru: lru only for keys with expiration time set (default)2, allkeys-lru: delete the key of the LRU algorithm3,volatile-random: deletes keys that are about to expire randomly4"Allkeys-random" : indicates random deletion5,volatile- TTL: deletes the TTL that is about to expire6, noeviction: never expires, returns errorCopy the code
- APPEND ONLY mode Aof configuration (persistent save)
Appendonly no # the default is to disable aof mode. The default is to use RDB persistence. In most cases, RDB is sufficient! appendfilename"appendonly.aof"Appendfsync always # sync for every change. Appendfsync Everysec # performs sync once per second and may lose data for those 1s! # appendfsync no # sync is not executed. The operating system synchronizes data itself at this time.Copy the code
Jedis operates Redis
In short, Jedis is an official Java connection development tool recommended by Redis! Although the current springboot2.x version has changed Jedis to Lettuce, I think it is necessary to know about the use of Jedis!
- How to integrate Jedis and connect to Redis database in Java project?
Create a Maven project
Empty can ~ how to create I do not need to repeat!
Import Jedis and FastJSON dependencies and wait for the download to complete!
<! <groupId> <artifactId>jedis</artifactId> <version>3.2.0< version> </dependency> <! -- dependency> <groupId>com.alibaba</groupId> <artifactId> <version>1.2.62</version> </dependency>Copy the code
③ Connect Redis test, here in order to facilitate the test, connect is local Redis service, connect remote need to change the configuration file and close the firewall, later will make a separate article to introduce this!
// 1, new Jedis object
Jedis jedis = new Jedis("127.0.0.1".6379);
// All jedis commands are all our previous commands
System.out.println(jedis.ping());
Copy the code
As shown in the figure:
Return to PONG to prove that the connection is successful!
(4) How to use the API?
// 1, new Jedis object
Jedis jedis = new Jedis("127.0.0.1".6379);
jedis.flushDB();// Delete all data from the current library
jedis.set("name"."dingyongjun");
jedis.set("age"."23");
jedis.set("high"."173");
System.out.println("name:"+jedis.get("name") +"\nage:"+jedis.get("age") +"\nhigh"+jedis.get("high"));
Copy the code
As shown in the figure:
jedis.lpush("list"."1"."2"."3"."4");
System.out.println("list: "+jedis.lrange("list".0, -1));
Copy the code
As shown in the figure:
⑤ Conclusion: Connect to use Redis in Jedis, and Redis console command is exactly the same, I do not repeat one command to write again, later if there is time, I will give all the commands to improve slowly!
I see no ending, but I will search high and low
If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah