Home > Database > Redis > How to implement pipelines by integrating Redis in SpringBoot

How to implement pipelines by integrating Redis in SpringBoot

WBOY
Release: 2023-05-30 09:19:56
forward
1447 people have browsed it

1. Redis pipeline

To execute a Redis command, the Redis client and Redis server need to perform the following steps:

  • The client sends the command To the server;

  • The server accepts the command request, executes the command, and generates the corresponding result;

  • The server returns the result to the client;

  • The client accepts the execution results of the command and displays them to the user.

Most of the time consumed by the Redis command is spent on sending command requests and receiving command results, packaging any number of Redis command requests together, and then sending them all at once All are sent to the server, and the server will process all command requests and return all their execution results to the client at once.

Note:

The Redis server does not limit the number of commands included in the pipeline by the client, but it sets a default volume limit of 1GB for the client's input buffer: when When the amount of data sent by the client exceeds this limit, the Redis server will forcefully close the client. Therefore, it is best not to execute a large number of commands or some very large commands in the same pipeline at once.

In addition, many clients also have implicit buffer size limits. If you find that some pipeline commands are not executed when using the pipeline feature, or the results returned by the pipeline If it is incomplete, then it is likely that your program has hit the client's built-in buffer size limit.

2. SpringBoot integrated Redis pipeline example

SpringBoot integrated redis example

Use a single increment command to process 2 million keys:

public class RedisPipelineStudy extends BaseTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String PREFIX = "test0:";

    @Test
    public void test() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("test0");
        for (int times = 0; times <p>Time consuming As shown below: It is 12 bits, the unit is ns<br></p><p><img src="https://img.php.cn/upload/article/000/887/227/168540959894676.png" alt="How to implement pipelines by integrating Redis in SpringBoot"></p><p> Use the pipe incrBy to process 2 million keys, each time packaging 300 commands and send them to the server, as shown below: </p><pre class="brush:php;toolbar:false">public class RedisPipelineStudy extends BaseTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String PREFIX = "test1:";

    @Test
    public void test() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("test1");
        List<integer> recordList = new ArrayList();
        for (int times = 0; times  300) {
                        incrByPipeline(recordList);
                        recordList = new ArrayList();
                    }
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
            if (!CollectionUtils.isEmpty(recordList)) {
                incrByPipeline(recordList);
                recordList = new ArrayList();
            }
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }

    private void incrByPipeline(List<integer> recordList) {
        stringRedisTemplate.executePipelined(new RedisCallback<object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                try {
                    for (Integer record : recordList) {
                        byte[] key = (PREFIX + record).getBytes();
                        connection.incrBy(key, 1);
                    }
                } catch (Exception e) {
                    System.out.println(e);
                }
                return null;
            }
        });
    }
}</object></integer></integer>
Copy after login

Time consumed: 11 bits, unit: ns, 1/6 of the time taken by a single command.

How to implement pipelines by integrating Redis in SpringBoot

The above is the detailed content of How to implement pipelines by integrating Redis in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template