In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to realize pipeline by integrating Redis in SpringBoot". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to realize pipeline by integrating Redis in SpringBoot" can help you solve the problem.
1. Redis pipeline
To execute a Redis command, the Redis client and Redis server need to perform the following steps:
The client sends commands to the server;
The server receives the command request, executes the command, and generates the corresponding result.
the server returns the result to the client;
The client accepts the execution result of the command and displays it to the user.
Most of the time spent on Redis commands is spent sending command requests and receiving command results, packing any number of Redis command requests together and sending them all to the server at once, and the server will process all command requests and return all their execution results to the client at once.
Notes:
The Redis server does not limit the number of commands a client can include in a pipeline, but it does set a default size limit of 1GB for the client's input buffer: when a client sends more data than this limit, the Redis server forces the client to shut down. Therefore, it is best not to put a large number of commands or some very large commands into the same pipeline.
In addition, many clients have implicit buffer size limits, and if you find that some pipeline commands are not executed or the pipeline returns incomplete results during the use of pipeline features, it is likely that your program has hit the built-in buffer size limit of the client.
2. SpringBoot Integration Redis Pipeline Instance
Examples of SpringBoot integration redis
Use a single increment command to process 200w 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
< 2; times++) { for (int i = 0; i < 1000000; i++) { stringRedisTemplate.opsForValue().increment(PREFIX + i, 1L); } } stopWatch.stop(); System.out.println(stopWatch.prettyPrint()); }} 耗时如下所示:是 12 位 ,单位ns 使用管道 incrBy 处理 200w个key,每次打包300条命令发送给服务器,如下所示: 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 recordList = new ArrayList(); for (int times = 0; times < 2; times++) { for (int i = 0; i < 1000000; i++) { try { recordList.add(i); if (recordList.size() >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 recordList) { stringRedisTemplate.executePipelined(new RedisCallback() { @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; } }); }}
Elapsed time: 11 bits, unit: ns, is 1/6 of the time consumed by a single command.
The content of "How to implement pipelines by integrating Redis in SpringBoot" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.