springboot cache redis 缓存

上一节学习spring的cache缓存,介绍了spring缓存的基础:node

https://my.oschina.net/silenceyawen/blog/1555996redis

如今学习使用redis实现缓存:spring

1. 指定provider能够使用配置:数据库

    

2. redis在这里充当缓存提供者的角色(cache provider),上节中没有明显指定cache provider,则会按顺序查找如下的provider:windows

    

3. 若没有查找到前9类缓存的cacheManager,则会使用最后一个simple缓存,也就是在内存中使用ConcurrentHashMap实现缓存。spring官方建议生产环境中勿使用simple缓存。上一篇就是使用这个cache provider。缓存

4. 使用redis做为cache provider, 只须要依赖spring-boot-starter-data-redis,spring就会找到redisCacheManager,就能够使用redis做为缓存。其中redis的默认配置以下:bash

spring.redis.host=localhost
spring.redis.port=6379
# 第0个database
spring.redis.database=0
# 默认密码为空
spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-wait=-1
spring.redis.ssl=false
spring.redis.timeout=0
#spring.redis.cluster.max-redirects=
#spring.redis.cluster.nodes=

5. windows 搭建 rediside

    下载后windows版本的redis以下:spring-boot

    

    运行start.bat就是 使用redis.windows.conf配置 来启动 redis-server.exe学习

6. 启动redis后,运行上一节的测试代码,就能够看到redis的窗口出现读写的输出。

    注意:实体类必定要实现serializable接口。

    redis在数据库重启,或应用程序重启后,写在redis中的缓存仍是存在的,但咱们能够经过实体类中的时间来判断数据是不是从缓存中读取的。

    例如,图中是我重启后第一次运行测试程序,但时间显示倒是五分钟以前,因此这是从缓存中读取的。

    

相关文章
相关标签/搜索