EhCache 缓存 使用注解与Spring整合
2017-09-22 15:57
686 查看
EhCache 缓存 使用注解与Spring整合
之前由于项目中一个接口的结果集需要重复请求另一系统的接口,并且相关的重复请求也比较多就去学习了一下EhCache,过程中发现其他技术博客不是哪里差点东西,就是用的原始方法做的,拿来在项目中简单使用的话会非常复杂,因此介绍一下个人的学习理解,还望多指教。运用到的maven、Spring技术知识,不是很懂的同学请自行学习。
概述
依赖
EhCache.xml
Spring-beans.xml
常用注解
概述
Ehcache 是一个非常轻量级的成熟的纯Java的进程内缓存框架,你可以直接使用它来管理你的缓存,不需要什么特别的配置,非常轻便跟简洁; Spring提供了对缓存功能的抽象:即使用不同的注解解决缓存方案。当然缓存数据的清理、保留时间、数据跟换还是需要依据各自的项目去进行相应的设计,这里不做介绍。1、主要特性
1) 快速.
2) 简单.
3) 多种缓存策略
4) 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5) 缓存数据会在虚拟机重启的过程中写入磁盘
6) 可以通过RMI、可插入API等方式进行分布式缓存
7) 具有缓存和缓存管理器的侦听接口
8) 支持多缓存管理器实例,以及一个实例的多个缓存区域
依赖
一般来说这个版本已经够用了<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>1.6.2</version> </dependency>
EhCache.xml
<ehcache> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"/> <cache name="UserInfo" maxElementsOnDisk="20000" maxElementsInMemory="2000" eternal="true" overflowToDisk="true" diskPersistent="true"/> <cache name="ConpanyInfo" maxElementsOnDisk="20000" maxElementsInMemory="2000" eternal="true" overflowToDisk="true" diskPersistent="true"/> </ehcache>
各配置参数详解,各位可根据需求更改、添加相应的参数
<ehcache> <!-- 指定一个文件目录,默认临时缓存数据文件路径 --> <diskStore path="java.io.tmpdir"/> <!-- 设定缓存的默认数据过期策略 --> <defaultCache maxElementsInMemory="1000" 内存最大缓存对象数 eternal="false" timeToIdleSeconds="120" 设定允许对象处于空闲状态最大时间(s) timeToLiveSeconds="120" 设定允许对象存在缓存中最长时间(s) overflowToDisk="false"/> <cache name="xxxInfo" 缓存名称 maxElementsOnDisk="20000" 硬盘中最大缓存对象数 maxElementsInMemory="2000" eternal="true" true表示对象永不过期 overflowToDisk="true" 内存达到界限后写入硬盘 memoryStoreEvictionPolicy="LRU" 内存达限后清理策略:LRU默认、FIFO(先进先出)、LFU(最少访问次数)。 clearOnFlush = "true" 内存达限时是否清除。 diskPersistent="true"/> </ehcache>
Spring-beans.xml
在主配置文件中需要加上: xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd" <!-- Ehcache缓存配置 --> <cache:annotation-driven cache-manager="cacheManager"/> 自动加载扫描注解 <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:config/spring/ehcache.xml"/> </bean> <!-- EhCache缓存策略 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean>
常用注解
@Cacheable:[save] -- 据key 值变化进行缓存。 @Cacheable(value="UserInfo",key = "#doorId",Condition ="#doorId >250") (缓存名 , 缓存key , 缓存条件 -- 默认true) @CachePut: [update] -- 每次方法都进行缓存,与key无关。 @CachePut(value="UserInfo",key = "#doorId",Condition = "") @CacheEvict : [delete] -- 根据一定的条件清除缓存. @CacheEvict(value="UserInfo",key = "#doorId",condition = "" ,allEntries = true,beforeInvocation = false) (名 ,key ,条件 ,全部清除? ,方法前清空?)
注意:图片中标红的地方名称要保持一致
注解一般是放在Service 或 Dao的实现类中的方法上,据须所用,我的理解就到这了。
相关文章推荐
- Spring与ehcache整合,通过注解的方式使用缓存
- Spring与ehcache整合,通过Spring原生注解使用缓存
- Spring与ehcache整合,通过Spring原生注解使用缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Spring整合Mybaits实现ehcache 注解查询缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Spring整合Mybaits实现ehcache 注解查询缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存
- Spirng3基于注解(annotation)整合ehcache 使用页面缓存、对象缓存
- Ehcache 整合Spring 使用页面、对象缓存