情景:执行了一条非常复杂的SQL,并且耗时久,数据量大并且一次访问被多个地方使用的这么一个情景.我使用了ehcache来做这个缓存.本来开始是使用io流把数据持久化到本地.后来发现在同事电脑上出现硬盘访问受限的问题导致存储失败.就改用ehcache.
1.首先引入jar包:
如果项目使用maven管理,引入以下坐标即可
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
2.配置ehcache.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<!-- 磁盘缓存位置 -->
<diskStore path="java.io.tmpdir/ehcache"/>
<!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!-- 超劳数据缓存 -->
<cache name="OverWorkCache"
maxElementsInMemory="10000"
maxElementsOnDisk = "0"
eternal="true"
overflowToDisk="false"
memoryStoreEvictionPolicy="FIFO"/>
</ehcache>
3.交给spring管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<description>ehcache缓存配置管理文件</description>
<!-- 启用缓存注解开关 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
</beans>
4.测试
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class test {
public static void main(String[] args) {
// 1. 创建缓存管理器
CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache.xml");
// 2. 获取缓存对象
Cache cache = cacheManager.getCache("HelloWorldCache");
// 3. 创建元素
Element element = new Element("key1", "value1");
// 4. 将元素添加到缓存
cache.put(element);
// 5. 获取缓存
Element value = cache.get("key1");
System.out.println(value);
System.out.println(value.getObjectValue());
// 6. 删除元素
cache.remove("key1");
System.out.println(cache.getSize());
// 7. 刷新缓存
cache.flush();
// 8. 关闭缓存管理器
cacheManager.shutdown();
}
}
5项目中使用关键代码
-------------------存-------------------------------------
//缓存数据
Cache overWorkCache = cacheManager.getCache("OverWorkCache");
//存入缓存
if(overWorkCache!= null){
overWorkCache.put("OverWork",resultList);
}
-------------------取-------------------------------------
public ArrayList<Map<String,Object>> getWorkTimeDetail() {
Cache cache = cacheManager.getCache("OverWorkCache");
ArrayList overWork = null;
if(cache != null){
overWork = cache.get("OverWork", ArrayList.class);
}
return overWork;
}