Home  >  Article  >  Database  >  The difference between jedis and redistemplate

The difference between jedis and redistemplate

(*-*)浩
(*-*)浩Original
2019-11-22 14:46:018472browse

The difference between jedis and redistemplate

Jedis is the officially recommended Java-oriented client for operating Redis, and RedisTemplate is a high-level encapsulation of JedisApi in SpringDataRedis.

Using native jedis and spring's redisTemplate to call the connection pool, I found a huge difference:                       (recommended learning: Redis video tutorial)

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; i++) {
      try (Jedis jedis = jedisPool.getResource()) {
        // 1、推送
        long time1 = System.currentTimeMillis();
        jedis.lpush("jedis-mq", greExamStr);
        // 2、接收
        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("总时间:" + 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; i++) {
      // 1、推送
      long time1 = System.currentTimeMillis();
      redisTemplate.opsForList().leftPush("redisTemplate-mq", greExamStr);
      // 2、接收
      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("总时间:" + time);
  }

Timeliness comparison:

The difference between jedis and redistemplateConclusion: The efficiency of native jedis is better than redisTemplate. In addition, the test found that 100 requests were used, and no business operations were performed each time. The execution speed was very fast. Redis only maintained a few connections. However, if you add your own business processing or sleep for a few seconds, you will find that redis continues to maintain The connection pool is configured for 50 connections.

Because it is a single thread, after the last jedis execution is completed, it seems that it is not closed, and the next call does not allocate an idle connection, but opens a new connection until the minimum number of connections is saturated, and then allocates empty idle connections to The next one (it stands to reason that a single thread should ensure that when a new call is made, the previous one is completely idle, so no new connections will be opened). The specific reason is still in doubt. It may be related to the principle of the thread pool.

For more Redis-related technical articles, please visit the Introduction to Using Redis Database Tutorial column to learn!

The above is the detailed content of The difference between jedis and redistemplate. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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