Spring Boot学习总结(9)——SpringBoot集成Ehcache3.x
来源:互联网 发布:2017暑运大数据报告 编辑:程序博客网 时间:2024/05/29 06:59
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.4.0</version>
</dependency>
@EnableCaching添加注解后Spring会根据以下顺序扫描缓存提供者进行创建
Generic→JCache (JSR-107)→ EhCache 2.x→Hazelcast→Infinispan→Redis →Guava→Simple
因为项目已经增加了Ehcache相关jar所以最终创建的cacheManager是Eh107CacheManager
也可以通过application.yml直接强制指定使用的缓存
spring: cache: jcache: config: classpath:ehcache.xml
3、配置cache
配置方法有两种:
代码配置
cacheManager.createCache("people", new MutableConfiguration<>() .setExpiryPolicyFactory(TouchedExpiryPolicy .factoryOf(new Duration(SECONDS, 40))) .setStoreByValue(false) .setStatisticsEnabled(true));
主要这里使用的cacheManager是javax.cache.CacheManager
并非spring提供的org.springframework.cache.CacheManager
xml配置
也可以建一个ehcache.xml进行配置
<cache alias="people" uses-template="heap-cache"> <expiry> <ttl unit="seconds">40</ttl> </expiry> </cache>
4、缓存的使用
通过spring提供的缓存注解可以和容易的配置缓存的使用这里介绍一下主要的缓存注解
@CacheConfig(cacheNames="people")
CacheConfig加在类上,制定类下方的方法使用的缓存
@Cacheable(key="#id")
Cacheable加在类的方法,如果不指定cacheNames会自动找到类上配置的cacheNames进行赋值
@CacheEvict(key="#customer.id")
CacheEvict用于清除缓存,可以根据key进行删除,也可以清空所有数据
也可以使用java规范定义的一套缓存注解spring也会进行识别,这里就不细说了
如果有更复杂的使用场景可以直接让Spring注入cacheManager进行更复杂的缓存操作,建议直接使用
org.springframework.cache.CacheManager 这样如果切换缓存实现就不需要改动代码了
5.注意:JSR107规范
ehcache3.x与2.x的差距还是非常大的,主要区别在于3.x后使用了java的缓存规范JSR107
Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Expiry。
CachingProvider定义了创建、配置、获取、管理和控制多个CacheManager。一个应用可以在运行期访问多个CachingProvider。
CacheManager定义了创建、配置、获取、管理和控制多个唯一命名的Cache,这些Cache存在于CacheManager的上下文中。一个CacheManager仅被一个CachingProvider所拥有。
Cache是一个类似Map的数据结构并临时存储以Key为索引的值。一个Cache仅被一个CacheManager所拥有。
Entry是一个存储在Cache中的key-value对。
每一个存储在Cache中的条目有一个定义的有效期,即Expiry Duration。一旦超过这个时间,条目为过期的状态。一旦过期,条目将不可访问、更新和删除。缓存有效期可以通过ExpiryPolicy设置。
项目的搭建
- Spring Boot学习总结(9)——SpringBoot集成Ehcache3.x
- spring boot 调用ehcache3.x(jsr107接口)原生api
- spring boot spring cache ehcache3.x整合
- Springboot整合集成ehcache3
- Spring Boot学习总结(7)——SpringBoot之于Spring优势
- Spring Boot学习总结(8)——SpringBoot Common application properties(application.properties)详解
- Spring Boot学习总结(3)——SpringBoot魅力所在
- Spring Boot学习总结(5)——SpringBoot Jar应用Linux后台部署执行
- Spring Boot学习总结(10)——SpringBoot打包成Docker镜像
- Spring Boot学习总结(11)——SpringBoot的Starter依赖包及作用
- SpringBoot:spring boot集成mybatis
- SpringBoot学习笔记(4) Spring Boot 集成 Mybatis
- SpringBoot学习笔记(4) Spring Boot 集成 Mybatis
- Spring Boot学习总结(6)——SpringBoot解决ajax跨域请求问题的配置
- spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)
- Spring Boot整合ehcache3.0 缓存
- spring boot初探(二)——springboot配置log4j
- Spring Boot干货——(六)SpringBoot整合mybatis
- 关于使用SQL Server LocalDB 一点体会
- hdu 5775 线段树
- linux解压
- eclipse创建maven web项目更改servlet版本小技巧
- 经验总结-完整介绍Android Studio中Git的使用之git的基本准备及提交代码到本地(一)-2
- Spring Boot学习总结(9)——SpringBoot集成Ehcache3.x
- ELK 完整部署和使用
- POJ 2318 TOYS <计算几何>
- 八数码问题
- Maven3 快速入门
- 微信小程序入门-事件冒泡、模板、视图渲染
- 2017CCPC秦皇岛 E:String of CCPC
- Qt下MSVC/Mingw平台dump/crash log报告调试方法差异
- hdu 6225 Little Boxes