redis practice in step04day14 java operation

Posted by btubalinal on Mon, 06 Sep 2021 20:18:42 +0200

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:




1.1.2 Preparation

1. Officially download the corresponding version of redis.conf file from 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, 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
    public void testGetConnection(){
        //If not, comment out bind in redis.conf.
        //And change the value of protected-mode to no, then restart redis and try again
        Jedis jedis=new Jedis("",6379);
        //jedis.auth("123456");//Suppose you set the password in redis.conf
        String ping =;

    //String Type Practice
    public void testStringOper() throws InterruptedException {
        //Link building (link with redis)
        Jedis jedis=new Jedis("",6379);
        //Store data (key/value)
        //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
        String id=jedis.get("id");
        Long num=jedis.strlen("content");
        //Release Resources

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
  public void testHashOper01(){
        //1. Connect
        Jedis jedis=new Jedis("",6379);
        //2. Store object information based on hash type
        //3. Update data stored in hash type
        //4. Get hash type data information
        String username=jedis.hget("member","username");
        String mobile = jedis.hget("member", "mobile");
        //5. Release resources
//hash type exercise (direct storage of map objects)
public void testHashOper02(){
    //1. Connect
    Jedis jedis=new Jedis("",6379);
    //2. Store a blog message
    Map<String,String> map=new HashMap<>();
    //3. Get blog content and output
    //4. Release resources

2.List Collection Exercise

public void testListOper02(){
    //1. Connect redis
    Jedis jedis=new Jedis("",6379);
    //2. Store data in queues
    //3. Data from queues in FIFO order
    List<String> list= jedis.brpop(40,"list1");
    //4. Release resources

3.set type exercises

public void testSetOper01() {
    //1. Connect redis
    Jedis jedis = new Jedis("", 6379);
    //2. Circle of friends
    jedis.sadd("count", "1", "1", "2");
    //3. Take out the favorite number
    Set<String> set = jedis.smembers("count");
    //4. Release resources

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 {
   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,"",6379);
        //Get a connection from the pool
        Jedis resource = jedisPool.getResource();
        //Accessing data through a jedis connection
        String clazz=resource.get("class");
        //Return link to pool
        //Close Connection Pool

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

Topics: Java Database Redis