Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between jedis and redistemplate?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

What's the difference between jedis and redistemplate? This article uses the example code to show, the code is very detailed, interested friends can refer to, hope to be helpful to you.

Jedis is a client officially recommended by Redis to operate Redis for Java, while RedisTemplate is a high encapsulation of JedisApi in SpringDataRedis.

Using native jedis and spring redisTemplate call connection pooling, there is a big difference:

Redis configuration:

Redis: database: 0 host: 127.0.0.1 port: 6379 password: 123456 timeout: 5000 lettuce: shutdown-timeout: 200 pool: max-active: 500 max-idle: 100 min-idle: 50 max-wait: 2000

Jedis unit test:

Public void testJedis () throws IOException {GreExam greExam = new GreExam (); greExam.setId (997); String greExamStr = JacksonUtil.bean2Json (greExam); long time = 0; for (int I = 0; I < 100; iTunes +) {try (Jedis jedis = jedisPool.getResource ()) {/ / 1, push long time1 = System.currentTimeMillis (); jedis.lpush ("jedis-mq", greExamStr) / 2. Receive String msg = jedis.brpoplpush ("jedis-mq", "jedis-mq_bak", 0); jedis.lrem ("jedis-mq_bak", 1, msg); long time2 = System.currentTimeMillis (); time + = time2-time1;} catch (Exception e) {e.printStackTrace ();} System.out.println ("Total time:" + time);}

RedisTemplate unit test:

Public void testRedisTemplate () throws IOException {GreExam greExam = new GreExam (); greExam.setId (997); String greExamStr = JacksonUtil.bean2Json (greExam); long time = 0; for (int I = 0; I < 100; iTunes +) {/ / 1, push long time1 = System.currentTimeMillis (); redisTemplate.opsForList (). LeftPush ("redisTemplate-mq", greExamStr) / 2. Receive String msg = (String) redisTemplate.opsForList (). RightPopAndLeftPush ("redisTemplate-mq", "redisTemplate-mq_bak", 1, TimeUnit.SECONDS); redisTemplate.opsForList (). Remove ("redisTemplate-mq_bak", 1, msg); long time2 = System.currentTimeMillis (); time + = time2-time1;} System.out.println ("Total time:" + time);}

Time effect comparison:

Conclusion: the efficiency of native jedis is better than that of redisTemplate. In addition, the test found that 100 requests were used and no business operation was performed each time, and the execution speed was very fast. Redis only maintained a few connections, but if you add your own business processing or sleep for a few seconds, you will find that redis maintains the configuration of connection pool for 50 + connections.

Because it is a single thread, after the last jedis execution, it does not seem to be closed, and the next call does not dispatch a free connection, but opens a new connection until the minimum number of connections is saturated. It is not until the minimum number of connections is saturated that the free connection is allocated to the next one (single thread should ensure that when the new call is made, the previous one is completely idle, so no new connection is opened.), the specific reason location, keep in doubt, may be related to the principle of thread pool.

So far, there is a basic understanding of the difference between jedis and redistemplate, but the specific usage still needs to be consolidated and practiced. If you want to know more about it, please pay attention to the industry information.

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report