搞懂分布式技术14:Spring Boot使用注解集成Redis缓存
2 --> 为了提高性能,减少数据库的压力,使用缓存是非常好的手段之一。本文,讲解 Spring Boot 如何集成缓存管理。 Spring注解缓存 Spring 3.1之后,引入了注解缓存技术,其本质上不是一个具体的缓存实现方案,而是一个对缓存使用的抽象,通过在既有代码中添加少量自定义的各种annotation,即能够达到使用缓存对象和缓存方法的返回对象的效果。Spring的缓存技术具备相当的灵活性,不仅能够使用SpEL(Spring Expression Language)来定义缓存的key和各种condition,还提供开箱即用的缓存临时存储方案,也支持和主流的专业缓存集成。其特点总结如下: 少量的配置annotation注释即可使得既有代码支持缓存; 支持开箱即用,不用安装和部署额外的第三方组件即可使用缓存; 支持Spring Express Language(SpEL),能使用对象的任何属性或者方法来定义缓存的key和使用规则条件; 支持自定义key和自定义缓存管理者,具有相当的灵活性和可扩展性。 和Spring的事务管理类似,Spring Cache的关键原理就是Spring AOP,通过Spring AOP实现了在方法调用前、调用后获取方法的入参和返回值,进而实现了缓存的逻辑。而Spring Cache利用了Spring AOP的动态代理技术,即当客户端尝试调用pojo的foo()方法的时候,给它的不是pojo自身的引用,而是一个动态生成的代理类。 如图12所示,实际客户端获取的是一个代理的引用,在调用foo()方法的时候,会首先调用proxy的foo()方法,这个时候proxy可以整体控制实际的pojo.foo()方法的入参和返回值,比如缓存结果,比如直接略过执行实际的foo()方法等,都是可以轻松做到的。Spring Cache主要使用三个注释标签,即@Cacheable、@CachePut和@CacheEvict,主要针对方法上注解使用,部分场景也可以直接类上注解使用,当在类上使用时,该类所有方法都将受影响。我们总结一下其作用和配置方法,如表1所示。 表1 标签类型 作用 主要配置参数说明 @Cacheable 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 value:缓存的名称,在 Spring 配置文件中定义,必须指定至少一个;key:缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则默认按照方法的所有参数进行组合;condition:缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 @CachePut 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用 value:缓存的名称,在 spring 配置文件中定义,必须指定至少一个;key:缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则默认按照方法的所有参数进行组合;condition:缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存 @CacheEvict 主要针对方法配置,能够根据一定的条件对缓存进行清空 value:缓存的名称,在 Spring 配置文件中定义,必须指定至少一个;key:缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,则默认按照方法的所有参数进行组合;condition:缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存;allEntries:是否清空所有缓存内容,默认为 false,如果指定为 true,则方法调用后将立即清空所有缓存;beforeInvocation:是否在方法执行前就清空,默认为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,默认情况下,如果方法执行抛出异常,则不会清空缓存 可扩展支持:Spring注解cache能够满足一般应用对缓存的需求,但随着应用服务的复杂化,大并发高可用性能要求下,需要进行一定的扩展,这时对其自身集成的缓存方案可能不太适用,该怎么办?Spring预先有考虑到这点,那么怎样利用Spring提供的扩展点实现我们自己的缓存,且在不改变原来已有代码的情况下进行扩展?是否在方法执行前就清空,默认为false,如果指定为true,则在方法还没有执行的时候就清空缓存,默认情况下,如果方法执行抛出异常,则不会清空缓存。 这基本能够满足一般应用对缓存的需求,但现实总是很复杂,当你的用户量上去或者性能跟不上,总需要进行扩展,这个时候你或许对其提供的内存缓存不满意了,因为其不支持高可用性,也不具备持久化数据能力,这个时候,你就需要自定义你的缓存方案了,还好,Spring也想到了这一点。 我们先不考虑如何持久化缓存,毕竟这种第三方的实现方案很多,我们要考虑的是,怎么利用Spring提供的扩展点实现我们自己的缓存,且在不改原来已有代码的情况下进行扩展。这需要简单的三步骤,首先需要提供一个CacheManager接口的实现(继承至AbstractCacheManager),管理自身的cache实例;其次,实现自己的cache实例MyCache(继承至Cache),在这里面引入我们需要的第三方cache或自定义cache;最后就是对配置项进行声明,将MyCache实例注入CacheManager进行统一管理。 声明式缓存 Spring 定义 CacheManager 和 Cache 接口用来统一不同的缓存技术。例如 JCache、 EhCache、 Hazelcast、 Guava、 Redis 等。在使用 Spring 集成 Cache 的时候,我们需要注册实现的 CacheManager 的 Bean。 Spring Boot默认集成CacheManager Spring Boot 为我们自动配置了多个 CacheManager 的实现。Spring Boot 为我们自动配置了 JcacheCacheConfiguration、 EhCacheCacheConfiguration、HazelcastCacheConfiguration、GuavaCacheConfiguration、RedisCacheConfiguration、SimpleCacheConfiguration 等。 默认的 ConcurrenMapCacheManager Spring 从 Spring3.1 开始基于 java.util.concurrent.ConcurrentHashMap 实现的缓存管理器。所以, Spring Boot 默认使用 ConcurrentMapCacheManager 作为缓存技术。 以下是我们不引入其他缓存依赖情况下,控制台打印的日志信息。 Bean'cacheManager' of type [class org.springframework.cache.concurrent.ConcurrentMapCacheManager] 实战演练 Maven 依赖 首先,我们先创建一个 POM 文件。 <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <groupId>com.lianggzone.demo</groupId> <artifactId>springboot-action-cache</artifactId> <version>0.1</version> <packaging>jar</packaging> <name>springboot-action-cache</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <defaultLibBundleDir>lib</defaultLibBundleDir> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> <useDefaultDelimiters>false</useDefaultDelimiters> <escapeString>\</escapeString> <delimiters> <delimiter>${*}</delimiter> </delimiters> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 其中,最核心的是添加 spring-boot-starter-cache 依赖。 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> 开启缓存支持 在 Spring Boot 中使用 @EnableCaching 开启缓存支持。 @Configuration @EnableCaching publicclassCacheConfiguration{} 服务层 创建一个服务类 @Service("concurrenmapcache.cacheService") publicclassCacheService{ } 首先,我们先来讲解下 @Cacheable 注解。@Cacheable 在方法执行前 Spring 先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放进缓存。有两个重要的值, value,返回的内容将存储在 value 定义的缓存的名字对象中。key,如果不指定将使用默认的 KeyGenerator 生成。 我们在查询方法上,添加 @Cacheable 注解,其中缓存名称为 concurrenmapcache。 @Cacheable(value ="concurrenmapcache") publiclong getByCache(){ try{ Thread.sleep(3*1000); }catch(InterruptedException e){ e.printStackTrace(); } returnnewTimestamp(System.currentTimeMillis()).getTime(); } @CachePut 与 @Cacheable 类似,但是它无论什么情况,都会将方法的返回值放到缓存中, 主要用于数据新增和修改方法。 @CachePut(value ="concurrenmapcache") publiclong save(){ long timestamp =newTimestamp(System.currentTimeMillis()).getTime(); System.out.println("进行缓存:"+ timestamp); return timestamp; } @CacheEvict 将一条或多条数据从缓存中删除, 主要用于删除方法,用来从缓存中移除相应数据。 @CacheEvict(value ="concurrenmapcache") publicvoiddelete(){ System.out.println("删除缓存"); } 控制层 为了展现效果,我们先定义一组简单的 RESTful API 接口进行测试。 @RestController("concurrenmapcache.cacheController") @RequestMapping(value ="/concurrenmapcache/cache") publicclassCacheController{ @Autowired privateCacheService cacheService; /** * 查询方法 */ @RequestMapping(value ="", method =RequestMethod.GET) publicString getByCache(){ Long startTime =System.currentTimeMillis(); long timestamp =this.cacheService.getByCache(); Long endTime =System.currentTimeMillis(); System.out.println("耗时: "+(endTime - startTime)); return timestamp+""; } /** * 保存方法 */ @RequestMapping(value ="", method =RequestMethod.POST) publicvoid save(){ this.cacheService.save(); } /** * 删除方法 */ @RequestMapping(value ="", method =RequestMethod.DELETE) publicvoiddelete(){ this.cacheService.delete(); } } 运行 @RestController @EnableAutoConfiguration @ComponentScan(basePackages ={"com.lianggzone.springboot"}) publicclassWebMain{ publicstaticvoid main(String[] args)throwsException{ SpringApplication.run(WebMain.class, args); } } 课后作业 我们分为几个场景进行测试。 多次调用查询接口,查看缓存信息是否变化,控制台日志是否如下?你得到的结论是什么? 调用保存接口,再调用查询接口,查看缓存信息是否变化?你得到的结论是什么? 调用删除接口,再调用查询接口,接口响应是否变慢了?你再看看控制台日志,你得到的结论是什么? 扩展阅读 如果想更深入理解 Spring 的 Cache 机制,这边推荐两篇不错的文章。 Spring Cache 抽象详解 Spring 4.1 新特性 - Spring 缓存框架增强 源代码 Spring Boot In Practice:Redis缓存实战 阅读本文需要对Spring和Redis比较熟悉。 Spring Framework 提供了Cache Abstraction对缓存层进行了抽象封装,通过几个annotation可以透明给您的应用增加缓存支持,而不用去关心底层缓存具体由谁实现。目前支持的缓存有java.util.concurrent.ConcurrentMap,Ehcache 2.x,Redis等。 一般我们使用最常用的Redis做为缓存实现(Spring Data Redis), 需要引入的starter:spring-boot-starter-data-redis,spring-boot-starter-cache; 自动配置生成的Beans:RedisConnectionFactory,StringRedisTemplate,RedisTemplate,RedisCacheManager,自动配置的Bean可以直接注入我们的代码中使用; I. 配置 application.properties # REDIS (RedisProperties) spring.redis.host=localhost # Redis server host. spring.redis.port=6379 # Redis server port. spring.redis.password= # Login password of the redis server. 具体对Redis cluster或者Sentinel的配置可以参考这里 开启缓存支持 @SpringBootApplication @EnableCaching//开启caching public class NewsWebServer { //省略内容 } 定制RedisTemplate 自动配置的RedisTemplate并不能满足大部分项目的需求,比如我们基本都需要设置特定的Serializer(RedisTemplate默认会使用JdkSerializationRedisSerializer)。 Redis底层中存储的数据只是字节。虽然Redis本身支持各种类型(List, Hash等),但在大多数情况下,这些指的是数据的存储方式,而不是它所代表的内容(内容都是byte)。用户自己来决定数据如何被转换成String或任何其他对象。用户(自定义)类型和原始数据类型之间的互相转换通过RedisSerializer接口(包org.springframework.data.redis.serializer)来处理,顾名思义,它负责处理序列化/反序列化过程。多个实现可以开箱即用,如:StringRedisSerializer和JdkSerializationRedisSerialize。Jackson2JsonRedisSerializer或GenericJackson2JsonRedisSerializer来处理JSON格式的数据。请注意,存储格式不仅限于value 它可以用于key,Hash的key和value。 声明自己的RedisTemplate覆盖掉自动配置的Bean: //通用的RedisTemplate @Bean public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); //template.setHashKeySerializer(template.getKeySerializer()); //template.setHashValueSerializer(template.getValueSerializer()); return template; } 这里我们使用GenericJackson2JsonRedisSerializer而不是Jackson2JsonRedisSerializer,后者的问题是你需要为每一个需要序列化进Redis的类指定一个Jackson2JsonRedisSerializer因为其构造函数中需要指定一个类型来做反序列化: redis.setValueSerializer(new Jackson2JsonRedisSerializer<Product>(Product.class)); 如果我们应用中有大量对象需要缓存,这显然是不合适的,而前者直接把类型信息序列化到了JSON格式中,让一个实例可以操作多个对象的反序列化。 定制RedisCacheManager 有时候Spring Boot自动给我们配置的RedisCacheManager也不能满足我们应用的需求,我看到很多用法都直接声明了一个自己的RedisCacheManager,其实使用CacheManagerCustomizer可以对自动配置的RedisCacheManager进行定制化: @Bean public CacheManagerCustomizer<RedisCacheManager> cacheManagerCustomizer() { return new CacheManagerCustomizer<RedisCacheManager>() { @Override public void customize(RedisCacheManager cacheManager) { cacheManager.setUsePrefix(true); //事实上这是Spring Boot的默认设置,为了避免key冲突 Map<String, Long> expires = new HashMap<>(); expires.put("myLittleCache", 12L*60*60); // 设置过期时间 key is cache-name expires.put("myBiggerCache", 24L*60*60); cacheManager.setExpires(expires); // expire per cache cacheManager.setDefaultExpiration(24*60*60);// 默认过期时间:24 hours } }; } II. 使用 缓存Key的生成 我们都知道Redis是一个key-value的存储系统,无论我们想要缓存什么值,都需要制定一个key。 @Cacheable(cacheNames = "user") public User findById(long id) { return userMapper.findById(id); } 上面的代码中,findById方法返回的对象会被缓存起来,key由默认的org.springframework.cache.interceptor.SimpleKeyGenerator生成,生成策略是根据被标注方法的参数生成一个SimpleKey对象,然后由RedisTemplate中定义的KeySerializer序列化后作为key(注意StringRedisSerializer只能序列化String类型,对SimpleKey对象无能为力,你只能定义其他Serializer)。 不过大多数情况下我们都会采用自己的key生成方案,方式有两种: 1.实现自己的KeyGenerator; @Configuration @EnableCaching public class CacheConfig extends CachingConfigurerSupport { @Bean public KeyGenerator customKeyGenerator() { return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... objects) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()); sb.append(method.getName()); for (Object obj : objects) { sb.append(obj.toString()); } return sb.toString(); } }; } } 2.在@Cacheable标注中直接声明key: @Cacheable(cacheNames = "user", key="#id.toString()") ❶ public User findById(long id) { return userMapper.findById(id); } @Cacheable(cacheNames = "user", key="'admin'") ❷ public User findAdmin() { return userMapper.findAdminUser(); } @Cacheable(cacheNames = "user", key="#userId + ':address'") ❸ public List<Address> findUserAddress(long userId) { return userMapper.findUserAddress(userId); } key的声明形式支持SpEL。❶ 最终生成的Redis key为:user:100234,user部分是因为cacheManager.setUsePrefix(true),cacheName会被添加到key作为前缀避免引起key的冲突。之所以#id.toString()要long型转为String是因为我们设置的KeySerializer为StringRedisSerializer只能用来序列化String。❷ 如果被标注方法没有参数,我们可以用一个静态的key值,最终生成的key为user:admin。❸ 最终生成的key为user:100234:address。 这种方式更符合我们以前使用Redis的习惯,所以推荐。 直接使用RedisTemplate 有时候标注不能满足我们的使用场景,我们想要直接使用更底层的RedisTemplate。 @Service public class FeedService { @Resource(name="redisTemplate") ❶ private ZSetOperations<String, Feed> feedOp; public List<Feed> getFeed(int count, long maxId) { return new ArrayList<>(feedOp.reverseRangeByScore(FEED_CACHE_KEY, 0, maxId, offset, count)); } //省略 } ❶ 我们可以直接把RedisTemplate的实例注入为ZSetOperations、ListOperations、ValueOperations等类型(Spring IoC Container帮我们做了转化工作,可以参考org.springframework.data.redis.core.ZSetOperationsEditor)。