1.redis client tool jedis
1.1 Introduction and preparation of jedis
Introduction to 1.1.1 jedis
Jedis is a client in Java that operates on redis, similar to accessing the mysql database through jdbc.
To implement jedis in java's mevan, you need to import the following dependencies:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
1.1.2 Preparation
1. Officially download the corresponding version of redis.conf file from redis.io at the following address (if you can't download it, remove it from code or take it from your classmates):
2. Stop redis and delete the redis.conf configuration file in the mount directory (/usr/local/docker/redis01/conf).
docker stop redis01
3. Copy the downloaded redis.conf file to the redis mount directory (/usr/local/docker/redis01/conf)
4. Open the redis.conf file based on vim, comment on the line bind 127.0.0.1, and modify the value of protected-mode to no.
5. Restart the redis service and check the startup log (docker logs container id)
1.2 Quick Start Implementation
Define a unit test class in the Jedis project and a unit test method in the class. The code implementation is as follows:
public class JedisTests { //Connectivity Test @Test public void testGetConnection(){ //If not, comment out bind 127.0.0.1 in redis.conf. //And change the value of protected-mode to no, then restart redis and try again Jedis jedis=new Jedis("192.168.126.130",6379); //jedis.auth("123456");//Suppose you set the password in redis.conf String ping = jedis.ping(); System.out.println(ping); } //String Type Practice @Test public void testStringOper() throws InterruptedException { //Link building (link with redis) Jedis jedis=new Jedis("192.168.126.130",6379); //Store data (key/value) jedis.set("count","1"); jedis.set("id","10001"); jedis.set("content","aaaaaaaadfas"); //Update Data jedis.expire("id",1);//Set the effective length of key jedis.incr("count");//Self-incrementing key values //get data String count = jedis.get("count"); //TimeUnit is an enumeration type in Java, SECONDS is an instance of enumeration type, and the Thread.sleep() method is called at the bottom of sleep //TimeUnit.SECONDS.sleep(1);//Hibernate for a second Thread.sleep(1000); String id=jedis.get("id"); Long num=jedis.strlen("content"); System.out.println("cart.count="+count); System.out.println("id="+id); System.out.println("num="+num); //Release Resources jedis.close(); } }
Note: By starting redis through mobate, changing the configuration, you can connect java to the redis database, thus achieving the effect of Java connection to mysql, retrieving data from the database and performing business operations on it.
1.3 jedis command basic operations
There are four different data types in the redis database. There are four operation instances of data types in java to add, delete, and subtract different data types. The code for the example is as follows:
1. hash type exercise
//hash type exercise @Test public void testHashOper01(){ //1. Connect Jedis jedis=new Jedis("192.168.126.130",6379); //2. Store object information based on hash type jedis.hset("member","id","101"); jedis.hset("member","username","jack"); jedis.hset("member","mobile","3333333"); //3. Update data stored in hash type jedis.hset("member","username","tony"); //4. Get hash type data information String username=jedis.hget("member","username"); String mobile = jedis.hget("member", "mobile"); System.out.println(username); System.out.println(mobile); //5. Release resources jedis.close(); } //hash type exercise (direct storage of map objects) @Test public void testHashOper02(){ //1. Connect Jedis jedis=new Jedis("192.168.126.130",6379); //2. Store a blog message Map<String,String> map=new HashMap<>(); map.put("x","100"); map.put("y","200"); jedis.hset("point",map); //3. Get blog content and output map=jedis.hgetAll("point"); System.out.println(map); //4. Release resources jedis.close(); }
2.List Collection Exercise
@Test public void testListOper02(){ //1. Connect redis Jedis jedis=new Jedis("192.168.126.128",6379); //2. Store data in queues //jedis.lpush("list1","A","B","C"); //3. Data from queues in FIFO order List<String> list= jedis.brpop(40,"list1"); System.out.println(list); jedis.brpop(40,"list1"); jedis.brpop(40,"list1"); jedis.brpop(40,"list1"); //4. Release resources jedis.close(); }
3.set type exercises
@Test public void testSetOper01() { //1. Connect redis Jedis jedis = new Jedis("192.168.126.128", 6379); //2. Circle of friends jedis.sadd("count", "1", "1", "2"); //3. Take out the favorite number Set<String> set = jedis.smembers("count"); System.out.println(set); //4. Release resources jedis.close(); }
Note: Different types of data can be operated with different Api s, while expanding the corresponding business according to some characteristics of the data type, such as list type to achieve a blocking queue, hash to achieve blog favorite function, and polymer instances to show later.
1.4 jedis connection pool operations
When we access redis directly from Jedis, freeing up the connection each time we get it can incur a significant performance overhead. With the help of the Jedis connection pool, we can reuse the created connection to improve its performance. Simple applications are as follows:
public class JedisPoolTests { @Test public void testJedisPool(){ //Define the configuration of the connection pool JedisPoolConfig config=new JedisPoolConfig(); config.setMaxTotal(1000);//maximum connection config.setMaxIdle(60);//Maximum idle time (connection unused to release) //Create connection pool JedisPool jedisPool= new JedisPool(config,"192.168.126.130",6379); //Get a connection from the pool Jedis resource = jedisPool.getResource(); resource.auth("123456"); //Accessing data through a jedis connection resource.set("class","cgb2004"); String clazz=resource.get("class"); System.out.println(clazz); //Return link to pool resource.close(); //Close Connection Pool jedisPool.close(); } }
2. Application of jedis and redisTemplate
2.1 Introduction to RedisTemplate and preparation for testing
brief introduction
RedisTemplate is a Java object that operates on the redis database in the SpringBoot project and encapsulates some basic operations on redis.
Dead work
1. Create the project configuration file application.yml
2. Create Project Startup Class
3 Quick Start Implementation
2.2 RedisTemplate application
2.3 Customize RedisTemplate Object (Extended)
3. Project Function Implementation
3.1 Single landing
Business Description
Key Code Implementation
functional analysis
3.2 Voting System
Business Description
In many system designs, there is an activity design. Before starting an activity, you can conduct a survey of its support, such as designing a voting system based on this activity, for example:
Key Code Implementation
Summary analysis
3.3 Secondary Kill Queue
Business Description
When designing a second-kill or snap-up system, in order to improve the response speed of the system, users'second-kill or snap-up requests are usually stored in a redis queue first, where we implement a first-in-first-out queue based on redis, for example:
Key Code Implementation
Summary analysis
3.4 Distributed id
Business Description
In a distributed system, when the amount of data is increasing, data needs to be sorted. However, after sorting, the data in each table will increase at its own pace, which is likely to result in id conflicts.A separate mechanism is needed to generate unique IDs, which can also be called distributed IDs. Here we implement a simple distributed id with redis. Of course, there are some third-party systems that can help you generate such IDs and expand your learning.
Key Code Implementation
Summary analysis
4. Summary
4.1 Common points and precautions
4.2 Difficulties and Problems Analysis