jedis和redistemplate区别


本文摘自PHP中文网,作者(*-*)浩,侵删。

Jedis是Redis官方推荐的面向Java的操作Redis的客户端,而RedisTemplate是SpringDataRedis中对JedisApi的高度封装。

使用原生jedis和spring的redisTemplate调用连接池,发现差别巨大: (推荐学习:Redis视频教程)

redis配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

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单元测试:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

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单元测试:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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);

}

时效对比:

阅读剩余部分

相关阅读 >>

为什么需要Redis

Redis数据持久化之rdb

如何使用Redis来实现分布式锁

Redis过期时间设置多久合适

怎么提高Redis缓存命中率

如何通过命令行修改Redis的配置

浅谈Redis sds跟c字符串的区别

Redis info命令介绍

Redis 模糊匹配 searchkeys

Redis高频面试题(附答案分析)

更多相关阅读请进入《Redis》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...