Spring Boot 使用Redis缓存
本文示例源码,请看这里
Spring Cache的官方文档,请看这里
缓存存储
Spring 提供了很多缓存管理器,例如:
- SimpleCacheManager
- EhCacheCacheManager
- CaffeineCacheManager
- GuavaCacheManager
- CompositeCacheManager
这里我们要用的是除了核心的Spring框架之外,Spring Data提供的缓存管理器:RedisCacheManager
在Spring Boot中通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),默认情况下Spring Boot根据下面的顺序自动检测缓存提供者:
- Generic
- JCache (JSR-107)
- EhCache 2.x
- Hazelcast
- Infinispan
- Redis
- Guava
- Simple
但是因为我们之前已经配置了redisTemplate了,Spring Boot无法就无法自动给RedisCacheManager设置redisTemplate了,所以接下来要自己配置CacheManager 。
- 首先修改RedisConfig配置类,添加@EnableCaching注解,并继承CachingConfigurerSupport,重写CacheManager 方法
...
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.afterPropertiesSet();
setSerializer(redisTemplate);
return redisTemplate;
}
private void setSerializer(RedisTemplate<String, String> template) {
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(jackson2JsonRedisSerializer);
}
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
// 设置缓存过期时间,秒
rcm.setDefaultExpiration(60);
return rcm;
}
...
Spring提供了如下注解来声明缓存规则:
- @Cacheable triggers cache population
- @CacheEvict triggers cache eviction
- @CachePut updates the cache without interfering with the method execution
- @Caching regroups multiple cache operations to be applied on a method
- @CacheConfig shares some common cache-related settings at class-level
| 注 解 | 描 述 |
|---|---|
| @Cacheable | 表明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就会返回缓存的值。否则的话,这个方法就会被调用,返回值会放到缓存之中 |
| @CachePut | 表明Spring应该将方法的返回值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用 |
| @CacheEvict | 表明Spring应该在缓存中清除一个或多个条目 |
| @Caching | 这是一个分组的注解,能够同时应用多个其他的缓存注解 |
| @CacheConfig | 可以在类层级配置一些共用的缓存配置 |
| 属 性 | 类 型 | 描 述 |
|---|---|---|
| value | String[] | 要使用的缓存名称 |
| condition | String | SpEL表达式,如果得到的值是false的话,不会将缓存应用到方法调用上 |
| key | String | SpEL表达式,用来计算自定义的缓存key |
| unless | String | SpEL表达式,如果得到的值是true的话,返回值不会放到缓存之中 |
在一个请求方法上加上@Cacheable注解,测试下效果
@Cacheable(value="testallCache") @RequestMapping(value = "/redis/user/{userId}", method = RequestMethod.GET) public User getUser(@PathVariable() Integer userId) { User user = userService.getUserById(userId); return user; }然后访问这个请求,控制台就报错啦。
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at org.springframework.data.redis.serializer.StringRedisSerializer.serialize(StringRedisSerializer.java:33) at org.springframework.data.redis.cache.RedisCacheKey.serializeKeyElement(RedisCacheKey.java:74) at org.springframework.data.redis.cache.RedisCacheKey.getKeyBytes(RedisCacheKey.java:49) at org.springframework.data.redis.cache.RedisCache$1.doInRedis(RedisCache.java:176) at org.springframework.data.redis.cache.RedisCache$1.doInRedis(RedisCache.java:172) at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:207)原因如下:
先看一下Redis缓存默认的Key生成策略- If no params are given, return SimpleKey.EMPTY.
- If only one param is given, return that instance.
- If more the one param is given, return a SimpleKey containing all parameters.
从上面的生成策略可以知道,上面的缓存testallCache使用的key是整形的userId参数,但是我们之前在redisTemplate里设置了template.setKeySerializer(new StringRedisSerializer());,所以导致类型转换错误。虽然也可以使用SpEL表达式生成Key(详见这里),但是返回结果还是需要是string类型(比如#root.methodName就是,#root.method就不是),更通用的办法是重写keyGenerator定制Key生成策略。
修改RedisConfig类,重写keyGenerator方法:
@Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(":" + method.getName()); for (Object obj : params) { sb.append(":" + obj.toString()); } return sb.toString(); } }; }- 再次进行刚才的请求(分别以1,2作为userId参数),浏览器结果如下图:


使用redisclient工具查看下:


可以看到Redis里保存了:
- 两条string类型的键值对:key就是上面方法生成的结果,value就是user对象序列化成json的结果
- 一个有序集合:其中key为@Cacheable里的value+~keys,分数为0,成员为之前string键值对的key
这时候把userId为1的用户的username改为ansel(原来是ansel1),再次进行https://localhost:8443/redis/user/1 请求,发现浏览器返回结果仍是ansel1,证明确实是从Redis缓存里返回的结果。


缓存更新与删除
更新与删除Redis缓存需要用到@CachePut和@CacheEvict。这时候我发现如果使用上面那种key的生成策略,以用户为例:它的增删改查方法无法保证生成同一个key(方法名不同,参数不同),所以修改一下keyGenerator,使其按照缓存名称+userId方式生成key:
@Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); String[] value = new String[1]; // sb.append(target.getClass().getName()); // sb.append(":" + method.getName()); Cacheable cacheable = method.getAnnotation(Cacheable.class); if (cacheable != null) { value = cacheable.value(); } CachePut cachePut = method.getAnnotation(CachePut.class); if (cachePut != null) { value = cachePut.value(); } CacheEvict cacheEvict = method.getAnnotation(CacheEvict.class); if (cacheEvict != null) { value = cacheEvict.value(); } sb.append(value[0]); for (Object obj : params) { sb.append(":" + obj.toString()); } return sb.toString(); } }; }接下来编写user的增删改查方法:
@CachePut(value = "user", key = "#root.caches[0].name + ':' + #user.userId") @RequestMapping(value = "/redis/user", method = RequestMethod.POST) public User insertUser(@RequestBody User user) { user.setPassword(SystemUtil.MD5(user.getPassword())); userService.insertSelective(user); return user; } @Cacheable(value = "user") @RequestMapping(value = "/redis/user/{userId}", method = RequestMethod.GET) public User getUser(@PathVariable Integer userId) { User user = userService.getUserById(userId); return user; } //#root.caches[0].name:当前被调用方法所使用的Cache, 即"user" @CachePut(value = "user", key = "#root.caches[0].name + ':' + #user.userId") @RequestMapping(value = "/redis/user", method = RequestMethod.PUT) public User updateUser(@RequestBody User user) { user.setPassword(SystemUtil.MD5(user.getPassword())); userService.updateByPrimaryKeySelective(user); return user; } @CacheEvict(value = "user") @RequestMapping(value = "/redis/user/{userId}", method = RequestMethod.DELETE) public void deleteUser(@PathVariable Integer userId) { userService.deleteByPrimaryKey(userId); }因为新增和修改传递的参数为user对象,keyGenerator无法获取到userId,只好使用SpEL显示标明key了。
然后进行测试:
进行insert操作:

插入后,进行get请求:

查看Redis存储:


进行update操作:

更新后,进行get请求:

查看Redis存储:

进行delete操作:

查看Redis存储:

发现user:3的记录已经没有了,只剩user:1,user:2了
一直很想知道网上很多用之前那种keyGenerator方法的,他们是怎么进行缓存更新和删除的,有知道的可以告知下。
Spring Boot 使用Redis缓存的更多相关文章
- Spring Boot 结合 Redis 缓存
Redis官网: 中:http://www.redis.cn/ 外:https://redis.io/ redis下载和安装 Redis官方并没有提供Redis的Windows版本,这里使用微软提供的 ...
- Spring Boot自定义Redis缓存配置,保存value格式JSON字符串
Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 ...
- SpringBoot入门系列(七)Spring Boot整合Redis缓存
前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...
- spring boot集成redis缓存
spring boot项目中使用redis作为缓存. 先创建spring boot的maven工程,在pom.xml中添加依赖 <dependency> <groupId>or ...
- Spring Boot Cache Redis缓存
1.集成MyBatis 1.1.引入maven依赖 1.2.生成Mapper 具体可以看MyBatis Generator官网 http://www.mybatis.org/generator/run ...
- (转)spring boot整合redis
一篇写的更清晰的文章,包括redis序列化:http://makaidong.com/ncjava/330749_5285125.html 1.项目目录结构 2.引入所需jar包 <!-- Sp ...
- Spring Boot + Mybatis + Redis二级缓存开发指南
Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...
- (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...
- 玩转spring boot——结合redis
一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...
随机推荐
- Myeclipse 配置多个tomcat
1.首先准备多个tomcat 命名为: tomcat-8087 tomcat-8088 tomcat-8089 2.修改对应的server.xml ①:修改关闭时端口,分别设为 8005 8 ...
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
javaCV图像处理系列: javaCV图像处理之1:实时视频添加文字水印并截取视频图像保存成图片,实现文字水印的字体.位置.大小.粗度.翻转.平滑等操作 javaCV图像处理之2:实时视频添加图片水 ...
- winfrom DataSet和实体类的相互转换
最近做WInfrom项目,对表格和控件的数据绑定非常喜欢用实体类对象来解决,但是绑定以后 又怎么从控件中拿到实体类或者转换为datatable 或者dataset呢 经过在网上的搜索以及自己的改进 完 ...
- 带着问题写React Native原生控件--Android视频直播控件
最近在做的采用React Native项目有一个需求,视频直播与直播流播放同一个布局中,带着问题去思考如何实现,能更容易找到问题关键点,下面分析这个控件解决方法: 现在条件:视频播放控件(开源的ijk ...
- MVC架构简介及其测试策略
最近在WEB端测试工作中陷入了瓶颈,单纯的手动功能测试在没有成熟的代码规范之前还是很容易坑的,WEB自动化测试一时半会还没有什么进展,所以决定先学习一下网站用的MVC架构,跟着教程写了一个小网站,大概 ...
- js获取网页请求类型是http还是https
代码如下,即可判断 var ishttps = 'https:' == document.location.protocol ? true : false; if(ishttps) { alert(& ...
- ASP.NET MVC开发学习过程中遇到的细节问题以及注意事项
1.datagrid中JS函数传值问题: columns: { field: 'TypeName', title: '分类名称', width: 120, sortable: true, format ...
- iptables 汇总
iptables 一. 背景知识 1. 相关网络背景知识 (1) Linux 主机内部路由 Linux 在 内核中维护由一个路由表, 报文进入本机后, 由该路由表判断目标地址; 报文离开本机之前, 判 ...
- Windows系统完全退出VMware方法
原始日期:2013-11-30 16:09 事件起因:本来机子上装的Vbox,装了个winXp系统,目的是将一些开发用地软件工具神马的安装在虚拟机,保证主机的流畅稳定.无奈,Vbox对主机与虚拟机的文 ...
- zend framework 1 安装教程
网上的安装教程总是一笔带过,本人结合已经爬过的坑,为大家展示最简单的安装方式: 博主环境如下: 操作系统:win7 64bit 开发环境:lnmp(phpstudy) 注意: zftest:官方下载的 ...