springboot使用redis缓存

Redis 优势

本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存

  • 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
  • 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
  • 原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。
  • 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性
  1. 启动Redis

下载地址:https://github.com/MicrosoftArchive/redis/releases

  1. 导入依赖
    就只需要这一个依赖!不需要spring-boot-starter-cache

    org.springframework.boot spring-boot-starter-data-redis

当你导入这一个依赖时,SpringBoot的CacheManager就会使用RedisCache。

如果你的Redis使用默认配置,这时候已经可以启动程序了。

  1. 配置Redis

    spring: cache: type: redis redis:

    Redis数据库索引(默认为0)

    database: 8
    # Redis服务器地址
    host: 10.168.1.209
    # Redis服务器连接端口
    port: 6379
    # Redis服务器连接密码(默认为空)
    password: ''
    pool:
    # 连接池最大连接数(使用负值表示没有限制)
      max-active: 1000
      # 连接池最大阻塞等待时间(使用负值表示没有限制)
      max-wait: -1
      # 连接池中的最大空闲连接
      max-idle: 10
      # 连接池中的最小空闲连接
      min-idle: 2
      # 连接超时时间(毫秒)
    timeout: 0
    

测试

整合完毕!

别忘了在启动类开启缓存!

源码地址:https://gitee.com/LiZhendd/demo

springboot使用cache缓存

springboot使用ehcache缓存

本文同步分享在 博客“吟风者”(JianShu)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

评论

暂无评论