本文示例源码,请看这里

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 。

  1. 首先修改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 可以在类层级配置一些共用的缓存配置

@Cacheable和@CachePut有一些共有的属性

属  性 类  型 描  述
value String[] 要使用的缓存名称
condition String SpEL表达式,如果得到的值是false的话,不会将缓存应用到方法调用上
key String SpEL表达式,用来计算自定义的缓存key
unless String SpEL表达式,如果得到的值是true的话,返回值不会放到缓存之中
  1. 在一个请求方法上加上@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;
    }
  2. 然后访问这个请求,控制台就报错啦。

    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生成策略。
  3. 修改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();
            }
        };
    }
  4. 再次进行刚才的请求(分别以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缓存里返回的结果。



缓存更新与删除

  1. 更新与删除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();
            }
        };
    }
  2. 接下来编写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缓存的更多相关文章

  1. Spring Boot 结合 Redis 缓存

    Redis官网: 中:http://www.redis.cn/ 外:https://redis.io/ redis下载和安装 Redis官方并没有提供Redis的Windows版本,这里使用微软提供的 ...

  2. Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

    Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 ...

  3. SpringBoot入门系列(七)Spring Boot整合Redis缓存

    前面介绍了Spring Boot 中的整合Mybatis并实现增删改查,.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/ ...

  4. spring boot集成redis缓存

    spring boot项目中使用redis作为缓存. 先创建spring boot的maven工程,在pom.xml中添加依赖 <dependency> <groupId>or ...

  5. Spring Boot Cache Redis缓存

    1.集成MyBatis 1.1.引入maven依赖 1.2.生成Mapper 具体可以看MyBatis Generator官网 http://www.mybatis.org/generator/run ...

  6. (转)spring boot整合redis

    一篇写的更清晰的文章,包括redis序列化:http://makaidong.com/ncjava/330749_5285125.html 1.项目目录结构 2.引入所需jar包 <!-- Sp ...

  7. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  8. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  9. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

随机推荐

  1. day_1_登录接口

    #/usr/bin/env python3# -*- coding: utf-8 -*-##This is an account login authentication##Version 1.0## ...

  2. css样式,边界和边框,格式和布局

    1.大小:width:宽:heigh:高 2.背景:1)background-color:背景颜色 2)background-image:背景图片url路径 3)background-repeat:图 ...

  3. [Leetcode] Binary search -- 475. Heaters

    Winter is coming! Your first job during the contest is to design a standard heater with fixed warm r ...

  4. OWIN的概念初接触

    OWIN这个词我昨天才认识,一直疑惑它是个什么东西,通过一定量的研究,得到一个初步的认识,留个脚印. OWIN是什么 OWIN是一个规范和标准,旨在阐述web服务器和web应用应该如何去解耦,它使得原 ...

  5. APUE-文件和目录(七)符号链接

    符号链接 符号链接的用途 符号链接是对一个文件的间接指针,它与前面介绍的硬连接不同,硬连接指向文件的i节点.引入符号链接是为了避开硬连接的一些限制: 硬链接通常要求链接和文件位于同一文件系统中. 只有 ...

  6. 机器学习:保序回归(IsotonicRegression):一种可以使资源利用率最大化的算法

    1.数学定义 保序回归是回归算法的一种,基本思想是:给定一个有限的实数集合,训练一个模型来最小化下列方程: 并且满足下列约束条件: 2.算法过程说明 从该序列的首元素往后观察,一旦出现乱序现象停止该轮 ...

  7. discuz用户登录不响应,提示nginx gateway timeout解决方法

    在使用nginx+php-cgi搭建discuz论坛过程中,出现论坛登录不响应,一直提示nginx gateway timeout504网关超时,单单采用php方式登录无问题.但因需要使用nginx把 ...

  8. SQL 和 .NET Framework 数据类型对应表

    SQL Server data type CLR data type (SQL Server) CLR data type (.NET Framework) varbinary SqlBytes, S ...

  9. Spring Security3详细配置

    Spring Security3详细配置 表名:RESOURCE 解释:资源表备注: 资源表 RESOURCE(资源表) 是否主键 字段名 字段描述 数据类型 长度 可空 约束 缺省值 备注 是 ID ...

  10. 基于Spring的最简单的定时任务实现与配置(一)

    朋友的项目中有点问题.他那边是Spring架构的,有一个比较简单的需要定时的任务执行.在了解了他的需求之后,于是提出了比较简单的Spring+quartz的实现方式. 注意本文只是讨论,在已搭建完毕的 ...