springboot cache redis 缓存
2017-10-26 00:00
549 查看
上一节学习spring的cache缓存,介绍了spring缓存的基础:
https://my.oschina.net/silenceyawen/blog/1555996
现在学习使用redis实现缓存:
1. 指定provider可以使用配置:
2. redis在这里充当缓存提供者的角色(cache provider),上节中没有明显指定cache provider,则会按顺序查找以下的provider:
3. 若没有查找到前9类缓存的cacheManager,则会使用最后一个simple缓存,也就是在内存中使用ConcurrentHashMap实现缓存。spring官方建议生产环境中勿使用simple缓存。上一篇就是使用这个cache provider。
4. 使用redis作为cache provider, 只需要依赖spring-boot-starter-data-redis,spring就会找到redisCacheManager,就可以使用redis作为缓存。其中redis的默认配置如下:
5. windows 搭建 redis
下载后windows版本的redis如下:
运行start.bat就是 使用redis.windows.conf配置 来启动 redis-server.exe
6. 启动redis后,运行上一节的测试代码,就可以看到redis的窗口出现读写的输出。
注意:实体类一定要实现serializable接口。
redis在数据库重启,或应用程序重启后,写在redis中的缓存还是存在的,但我们可以通过实体类中的时间来判断数据是否是从缓存中读取的。
例如,图中是我重启后第一次运行测试程序,但时间显示却是五分钟之前,所以这是从缓存中读取的。
https://my.oschina.net/silenceyawen/blog/1555996
现在学习使用redis实现缓存:
1. 指定provider可以使用配置:
2. redis在这里充当缓存提供者的角色(cache provider),上节中没有明显指定cache provider,则会按顺序查找以下的provider:
3. 若没有查找到前9类缓存的cacheManager,则会使用最后一个simple缓存,也就是在内存中使用ConcurrentHashMap实现缓存。spring官方建议生产环境中勿使用simple缓存。上一篇就是使用这个cache provider。
4. 使用redis作为cache provider, 只需要依赖spring-boot-starter-data-redis,spring就会找到redisCacheManager,就可以使用redis作为缓存。其中redis的默认配置如下:
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 搭建 redis
下载后windows版本的redis如下:
运行start.bat就是 使用redis.windows.conf配置 来启动 redis-server.exe
6. 启动redis后,运行上一节的测试代码,就可以看到redis的窗口出现读写的输出。
注意:实体类一定要实现serializable接口。
redis在数据库重启,或应用程序重启后,写在redis中的缓存还是存在的,但我们可以通过实体类中的时间来判断数据是否是从缓存中读取的。
例如,图中是我重启后第一次运行测试程序,但时间显示却是五分钟之前,所以这是从缓存中读取的。
相关文章推荐
- spring boot+spring cache实现两级缓存(redis+caffeine)
- Spring Boot Cache + redis 设置有效时间和自动刷新缓存,时间支持在配置文件中配置
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用
- SpringBoot 整合redis实现缓存 记录@CachePut值为1
- spring boot redis cache 缓存学习
- SpringBoot集成Redis来实现缓存技术方案
- Spring Boot 基于注解的 Redis 缓存使用详解
- Spring Boot学习(缓存-Redis)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十五)使用Redis做集中式缓存(1)
- SpringBoot注解缓存配置浅析(Redis集群)
- spring-boot-2.0.3之redis缓存实现,不是你想的那样哦!
- spring boot使用redis做数据缓存
- Spring Boot + Redis 缓存方案深度解读
- spring boot 下 shiro+redis与mybatis redis cache冲突的解决办法
- springboot中关于缓存@Cacheable、@CacheEvict、@CachePut的用法理解浅析
- Spring Boot 整合 Redis 实现缓存操作
- SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法
- SpringBoot集成Redis来实现缓存技术方案
- Spring Boot缓存实战 默认Cache(ConcurrentMapCacheManager)
- springboot mybatis redis 二级缓存