spring-boot整合redis作为缓存(2)——spring-boot的缓存
2017-04-14 17:10
821 查看
分几篇文章总结spring-boot与Redis的整合
1、redis的安装
2、redis的设置
3、spring-boot的缓存
4、自定义key
5、spring-boot引入redis
spring-boot对缓存的支持非常不错,少量的配置即可完成。
Generic
JCache (JSR-107) (EhCache 3, Hazelcast,Infinispan,
etc)
EhCache 2.x
Hazelcast
Infinispan
Couchbase
Redis
Caffeine
Guava (deprecated)
Simple
这里有两点需要说明:1、如果没有显示指明所用的缓存,spring-boot会通过扫描classpath去寻找引入的jar包,并按照上面提到的缓存类型从上到下的优先级,把该类型的缓存作为使用的缓存。比如在classpath同时扫描到JCache和Redis的jar包,则把JCache作为缓存。2、Simple为spring通过CurrentHashMap实现的缓存,由于是CurrentHashMap实现的,所以自然是线程安全的。
虽然由于不同的缓存,配置方式不同,但是在缓存管理方面,spring-boot是遵从JSR-107标准的。也就是需要一个CacheManager去管理多个Cache。你可以通过配置去指定CacheManager和Cache。当然你也可以自己实现CacheManager。
@Cacheable 用来标明该方法返回的结果是否被缓存,或者通过缓存返回结果
其提供的参数有
value:缓存名,也就是cacheManager中缓存的名字,可以指定多个
cacheNames:和value一样
key:key的作用为,在同一个缓存中,用来识别缓存数据的。这里是指定key的值,可以使用spel表达式
keyGenerator:你可以定义一个keyGenerator,可以实现生成key的逻辑
cacheManager:可以指定cacheManager,比如你有个name为area的cache在MyCacheManager中。你在配置时,配置的cacheManager为SimpleCacheManager,其
中没有area缓存,那么就可以指定该参数为MyCacheManager
cacheResolver:可以指定cacheManager的获得cache的逻辑
condition:spel表达式,加入缓存的条件,在方法执行前,后判断
unless:spel表达式,和condition相反,只在方法执行之后判断
@CachePut 用来表明该方法的结果是否更新到缓存中
其提供的参数和@Cacheable一样
@CacheEvict 用来表明该方法的结果是否从缓存中移除
其提供的参数和@Cacheable一样,并多了以下参数
allEntries:移除该缓存中的所有数据
beforeInvocation:移除缓存在调用方法之前,默认为false,即方法成功调用之后才移除缓存
例子
举一个配置SimpleCacheManager的例子
1、redis的安装
2、redis的设置
3、spring-boot的缓存
4、自定义key
5、spring-boot引入redis
spring-boot对缓存的支持非常不错,少量的配置即可完成。
支持的缓存类型
spring-boot支持的缓存类型如下:Generic
JCache (JSR-107) (EhCache 3, Hazelcast,Infinispan,
etc)
EhCache 2.x
Hazelcast
Infinispan
Couchbase
Redis
Caffeine
Guava (deprecated)
Simple
这里有两点需要说明:1、如果没有显示指明所用的缓存,spring-boot会通过扫描classpath去寻找引入的jar包,并按照上面提到的缓存类型从上到下的优先级,把该类型的缓存作为使用的缓存。比如在classpath同时扫描到JCache和Redis的jar包,则把JCache作为缓存。2、Simple为spring通过CurrentHashMap实现的缓存,由于是CurrentHashMap实现的,所以自然是线程安全的。
配置方式
配置方式支持xml,properties,yml,和javaconfig。其配置项由所用的缓存的不同而不同。这篇文章将用javaconfig的方式据一个配置Simple缓存的例子。虽然由于不同的缓存,配置方式不同,但是在缓存管理方面,spring-boot是遵从JSR-107标准的。也就是需要一个CacheManager去管理多个Cache。你可以通过配置去指定CacheManager和Cache。当然你也可以自己实现CacheManager。
使用方式
使用方式非常简单,只需要使用如下几个声明式注释即可。@Cacheable 用来标明该方法返回的结果是否被缓存,或者通过缓存返回结果
其提供的参数有
value:缓存名,也就是cacheManager中缓存的名字,可以指定多个
cacheNames:和value一样
key:key的作用为,在同一个缓存中,用来识别缓存数据的。这里是指定key的值,可以使用spel表达式
keyGenerator:你可以定义一个keyGenerator,可以实现生成key的逻辑
cacheManager:可以指定cacheManager,比如你有个name为area的cache在MyCacheManager中。你在配置时,配置的cacheManager为SimpleCacheManager,其
中没有area缓存,那么就可以指定该参数为MyCacheManager
cacheResolver:可以指定cacheManager的获得cache的逻辑
condition:spel表达式,加入缓存的条件,在方法执行前,后判断
unless:spel表达式,和condition相反,只在方法执行之后判断
@CachePut 用来表明该方法的结果是否更新到缓存中
其提供的参数和@Cacheable一样
@CacheEvict 用来表明该方法的结果是否从缓存中移除
其提供的参数和@Cacheable一样,并多了以下参数
allEntries:移除该缓存中的所有数据
beforeInvocation:移除缓存在调用方法之前,默认为false,即方法成功调用之后才移除缓存
例子
举一个配置SimpleCacheManager的例子@EnableCaching @Configuration public class SimpleCacheConfiguration { @Bean public CacheManager simpleCacheManager(){ SimpleCacheManager simpleCacheManager=new SimpleCacheManager(); //simpleCacheManager.setCaches(Collections.singletonList(new ConcurrentMapCache("authTree"))); List<ConcurrentMapCache> caches=new ArrayList<ConcurrentMapCache>(); caches.add(new ConcurrentMapCache("user")); caches.add(new ConcurrentMapCache("authTree")); caches.add(new ConcurrentMapCache("auth")); caches.add(new ConcurrentMapCache("role")); caches.add(new ConcurrentMapCache("vehicle")); caches.add(new ConcurrentMapCache("vehicleApply")); caches.add(new ConcurrentMapCache("vehicleApplyCollection")); caches.add(new ConcurrentMapCache("msgBox")); caches.add(new ConcurrentMapCache("report")); caches.add(new ConcurrentMapCache("breakRule")); caches.add(new ConcurrentMapCache("deviceParam")); caches.add(new ConcurrentMapCache("device")); caches.add(new ConcurrentMapCache("driver")); caches.add(new ConcurrentMapCache("route")); caches.add(new ConcurrentMapCache("area")); caches.add(new ConcurrentMapCache("system")); List<ConcurrentMapCache> cachesWrapper=Collections.unmodifiableList(caches); simpleCacheManager.setCaches(cachesWrapper); return simpleCacheManager; } }这里的cache也可以通过spring.cache.cache-name在properties或者yml中来配置,比如:
spring.cache.cache-name:user,authTree,auth,role,vehicle,vehicleApply,vehicleApplyCollection,msgBox,report,breakRule,deviceParam,device,driver,route,area,system
相关文章推荐
- spring-boot整合redis作为mysql二级缓存
- spring-boot整合redis作为缓存(4)——spring-boot引入Redis
- spring-boot整合redis作为缓存(1)——redis的设置
- spring-boot 整合redis作为数据缓存
- spring-boot整合redis作为缓存(3)——自定义key
- Spring Boot学习之整合Redis实现缓存
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- Spring整合Redis作为缓存
- spring-boot | 整合Redis缓存数据
- spring boot 整合redis对查询数据做缓存( 利用spring的AOP技术)
- spring boot整合redis实现缓存机制
- Spring整合Redis作为缓存
- Spring整合Redis作为缓存
- springboot整合redis作为K-V数据库
- REDIS学习(3.2)spring boot 使用redis作为缓存
- spring boot 整合redis对查询数据做缓存( 利用spring的AOP技术)
- Spring Boot 整合 Redis 实现缓存操作
- (转)Spring整合Redis作为缓存
- Spring Boot 整合 Redis 实现缓存操作