• SpringBoot整合Redis


    安装Redis

    下载链接https://github.com/MicrosoftArchive/redis/releases/tag/win-3.2.100

    服务器启动

    1. cmd进入安装目录下
    2. 启动命令:redis-server.exe redis.windows.conf
    3. 输入命令后可能会报下面的错误,这是Redis的一个bug,解决方法。
      1. 另开一个cmd中输入:redis-cli.exe
      2. 输入:shutdown,再输入:exit,后回车,退出客户端。
    4. 重新输入命令:redis-server.exe redis.windows.conf
    5. 另开一个cmd中输入:redis-cli.exe

    在SpringBoot项目中配置Redis

    application.yml中

    1. spring:
    2. redis:
    3. host: localhost #默认就是localhost
    4. port: 6379 #默认就是6379

    RedisTemplate

    RedisTemplate以对象作为key和value,内部对数据进行序列化。

    客户端默认是以字符串作为键值对进行存储和读取,这种方式不能读取在客户端存储的信息,客户端也不能读出程序所写入的信息。

    1. @SpringBootTest()
    2. class Xin1ApplicationTests {
    3. @Autowired
    4. private RedisTemplate redisTemplate; //操作Redis的对象,默认以对象为key-value
    5. @Test
    6. void set(){
    7. //存储键值对
    8. ValueOperations ops = redisTemplate.opsForValue();
    9. ops.set("age",33);
    10. //存储键对(键值对)
    11. HashOperations hash = redisTemplate.opsForHash();
    12. hash.put("info", "msg", "world");
    13. hash.put("info", "msg2", "world2");
    14. }
    15. @Test
    16. void contextLoads() {
    17. //读取键值对
    18. ValueOperations ops = redisTemplate.opsForValue();
    19. System.out.println(ops.get("age"));
    20. //读取键对(键值对)
    21. HashOperations hash = redisTemplate.opsForHash();
    22. System.out.println(hash.get("info", "msg"));
    23. System.out.println(hash.get("info", "msg2"));
    24. }
    25. }

    在客户端查看在程序中保存的key-value会发现写进去的值是序列化之后的。

     StringRedisTemplate

    这种方式可以读取在客户端中存储的信息,也可以在客户端中读取程序所写的信息。

    如果只使用字符类型的键值对,推荐使用!

    1. @SpringBootTest()
    2. class Xin1ApplicationTests {
    3. @Autowired
    4. private StringRedisTemplate redis; //默认以字符串类型的键值对
    5. @Test
    6. void set(){
    7. //存储键值对
    8. ValueOperations ops = redis.opsForValue();
    9. ops.set("age","33");
    10. //存储键对(键值对)
    11. HashOperations hash = redis.opsForHash();
    12. hash.put("info", "msg", "world");
    13. hash.put("info", "msg2", "world2");
    14. }
    15. @Test
    16. void contextLoads() {
    17. //读取键值对
    18. ValueOperations ops = redis.opsForValue();
    19. System.out.println(ops.get("age"));
    20. //读取键对(键值对)
    21. HashOperations hash = redis.opsForHash();
    22. System.out.println(hash.get("info", "msg"));
    23. System.out.println(hash.get("info", "msg2"));
    24. }
    25. }

    Jedis和Lettuce

    默认使用的Lettuce作为客户端。

    Jedis是同步的,不支持异步,Jedis客户端实例不是线程安全的,需要每个线程一个Jedis实例,一般通过连接池来使用Jedis。

    Lettuce是基于Netty框架的事件驱动的Redis客户端,其方法调用是异步的,Lettuce的API也是线程安全的,所以多个线程可以操作单个Lettuce连接来完成各种操作,同时Lettuce也支持连接池。

     

     

    使用Jedis 

    添加依赖

    1. <dependency>
    2. <groupId>redis.clientsgroupId>
    3. <artifactId>jedisartifactId>
    4. dependency>

    配置redis

    1. spring:
    2. redis:
    3. host: localhost
    4. port: 6379
    5. client-type: jedis

  • 相关阅读:
    Glide系列(五) — Request 构建流程分析
    【React】React学习:从初级到高级(二)
    LeetCode //C - 98. Validate Binary Search Tree
    Hash 哈希表和算法思路详解
    【OBS】Dropped Frames And General Connection Issues
    【GA+RBF+channel】基于遗传优化RBF的信道估计均衡算法matlab仿真
    侯捷 C++ STL标准库和泛型编程 —— 8 适配器
    java调用科大讯飞离线语音合成SDK --内附完整项目
    启动bert-server报错TypeError: cannot unpack non-iterable NoneType object
    CAP 8.0 版本发布通告 - CAP 7岁生日快乐!
  • 原文地址:https://blog.csdn.net/LYXlyxll/article/details/126685141