#spring boot 3 -- cache
来源:互联网 发布:炫酷黑客手机源码 编辑:程序博客网 时间:2024/05/29 17:38
EHcache
引入:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId></dependency>
在Spring Boot主类中增加@EnableCaching注解开启缓存功能
在数据访问接口中,增加缓存配置注解
@CacheConfig(cacheNames = "users")public interface UserRepository extends JpaRepository<User, Long> { @Cacheable User findByName(String name);}
Cache注解详解
@CacheConfig:主要用于配置该类中会用到的一些共用的缓存配置。在这里@CacheConfig(cacheNames = “users”):配置了该数据访问对象中返回的内容将存储于名为users的缓存对象中,我们也可以不使用该注解,直接通过@Cacheable自己配置缓存集的名字来定义。
@Cacheable:配置了findByName函数的返回值将被加入缓存。同时在查询时,会先从缓存中获取,若不存在才再发起对数据库的访问。该注解主要有下面几个参数:
value、cacheNames:两个等同的参数(cacheNames为Spring 4新增,作为value的别名),用于指定缓存存储的集合名。由于Spring 4中新增了@CacheConfig,因此在Spring 3中原本必须有的value属性,也成为非必需项了
key:缓存对象存储在Map集合中的key值,非必需,缺省按照函数的所有参数组合作为key值,若自己配置需使用SpEL表达式,比如:@Cacheable(key = “#p0”):使用函数第一个参数作为缓存的key值,更多关于SpEL表达式的详细内容可参考官方文档
condition:缓存对象的条件,非必需,也需使用SpEL表达式,只有满足表达式条件的内容才会被缓存,比如:@Cacheable(key = “#p0”, condition = “#p0.length() < 3”),表示只有当第一个参数的长度小于3的时候才会被缓存,若做此配置上面的AAA用户就不会被缓存,读者可自行实验尝试。
unless:另外一个缓存条件参数,非必需,需使用SpEL表达式。它不同于condition参数的地方在于它的判断时机,该条件是在函数被调用之后才做判断的,所以它可以通过对result进行判断。
keyGenerator:用于指定key生成器,非必需。若需要指定一个自定义的key生成器,我们需要去实现org.springframework.cache.interceptor.KeyGenerator接口,并使用该参数来指定。需要注意的是:该参数与key是互斥的
cacheManager:用于指定使用哪个缓存管理器,非必需。只有当有多个时才需要使用
cacheResolver:用于指定使用那个缓存解析器,非必需。需通过org.springframework.cache.interceptor.CacheResolver接口来实现自己的缓存解析器,并用该参数指定。
除了这里用到的两个注解之外,还有下面几个核心注解:
@CachePut:配置于函数上,能够根据参数定义条件来进行缓存,它与@Cacheable不同的是,它每次都会真是调用函数,所以主要用于数据新增和修改操作上。它的参数与@Cacheable类似,具体功能可参考上面对@Cacheable参数的解析
@CacheEvict:配置于函数上,通常用在删除方法上,用来从缓存中移除相应数据。除了同@Cacheable一样的参数之外,它还有下面两个参数:
allEntries:非必需,默认为false。当为true时,会移除所有数据
beforeInvocation:非必需,默认为false,会在调用方法之后移除数据。当为true时,会在调用方法之前移除数据
缓存配置
完成了上面的缓存实验之后,可能大家会问,那我们在Spring Boot中到底使用了什么缓存呢?
在Spring Boot中通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),Spring Boot根据下面的顺序去侦测缓存提供者:
Generic
JCache (JSR-107)
EhCache 2.x
Hazelcast
Infinispan
Redis
Guava
Simple
除了按顺序侦测外,我们也可以通过配置属性spring.cache.type来强制指定。我们可以通过debug调试查看cacheManager对象的实例来判断当前使用了什么缓存。
本文中不对所有的缓存做详细介绍,下面以常用的EhCache为例,看看如何配置来使用EhCache进行缓存管理。
在Spring Boot中开启EhCache非常简单,只需要在工程中加入ehcache.xml配置文件并在pom.xml中增加ehcache依赖,框架只要发现该文件,就会创建EhCache的缓存管理器。
在src/main/resources目录下创建:ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <cache name="users" maxEntriesLocalHeap="200" timeToLiveSeconds="600"> </cache></ehcache>
在pom.xml中加入
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId></dependency>
完成上面的配置之后,再通过debug模式运行单元测试,观察此时CacheManager已经是EhCacheManager实例,说明EhCache开启成功了。
对于EhCache的配置文件也可以通过application.properties文件中使用spring.cache.ehcache.config属性来指定,比如:
spring.cache.ehcache.config=classpath:config/another-config.xml
RedisCache
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
application.properties中增加redis配置,以本地运行为例,比如:
spring.redis.host=localhostspring.redis.port=6379spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1
我们需要做的配置到这里就已经完成了,Spring Boot会在侦测到存在Redis的依赖并且Redis的配置是可用的情况下,使用RedisCacheManager初始化CacheManager。
为此,我们可以单步运行我们的单元测试,可以观察到此时CacheManager的实例是org.springframework.data.redis.cache.RedisCacheManager
@CacheConfig(cacheNames = "users")public interface UserRepository extends JpaRepository<User, Long> { @Cacheable(key = "#p0") User findByName(String name); @CachePut(key = "#p0.name") User save(User user);}
- #spring boot 3 -- cache
- Spring boot配置cache
- Spring Boot集成cache
- spring boot 配置Cache
- Spring-boot中使用Cache
- Spring boot配置cache(ehcache)
- Spring Boot 数据缓存 Cache
- spring boot cache理论详解
- Spring Boot+Spring Data JPA+Spring Cache
- spring boot spring cache ehcache3.x整合
- spring boot中spring cache 整合redis
- 36. Spring Boot Cache理论篇【从零开始学Spring Boot】
- 36. Spring Boot Cache理论篇【从零开始学Spring Boot】
- Spring Boot 菜鸟教程 17 Cache-缓存
- spring-boot配置文件-cache(中文翻译)
- Spring Boot 轻量级缓存 Ace-Cache
- spring boot redis cache 缓存学习
- spring boot guava cache 缓存学习
- 启动页广告
- Android开发之Android Context,上下文(Activity Context, Application Context)
- CharacterEncodingFilter cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突 解决方案
- Ribbon实现默认接口类
- git操作和npm操作
- #spring boot 3 -- cache
- 二叉树
- [微信小程序]消息推送配置Token令牌错误校验失败
- 线性表 —— 4
- 2017.7.27 bill的挑战 失败总结
- Webpack2 入门踩坑教程(三)
- c++学习-虚函数学习
- Tomcat启动过慢
- redhat6.4_LVS_install安装文档