一:详细配置步骤
1,添加ehcache.xml文件
将ehcache.xml文件添加到src路径下面。ehcache.xml文件内容如下
[html] view plain copy
- <ehcache>
- <diskStore path="java.io.tempdir" />
- <defaultCache maxElementsInMemory="1000" eternal="false"
- timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
- <cache name="ehcacheName" maxElementsInMemory="10000"
- eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000"
- overflowToDisk="true" />
- </ehcache>
<ehcache>
<diskStore path="java.io.tempdir" />
<defaultCache maxElementsInMemory="1000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
<cache name="ehcacheName" maxElementsInMemory="10000"
eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000"
overflowToDisk="true" />
</ehcache>
2,添加spring配置文件
在applicContext.xml文件中添加
[html] view plain copy
- <bean id="cacheManagerFactory"
- class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
- p:configLocation="classpath:ehcache.xml"></bean>
- <!-- 声明cacheManager -->
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
- p:cacheManager-ref="cacheManagerFactory" ></bean>
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"></bean>
<!-- 声明cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory" ></bean></pre><br>
二:使用
1,定义EHCache工具方法
[java] view plain copy
print?
1.
2. public class EHCache {
3. private static final CacheManager cacheManager = new CacheManager();
4. private Cache cache;
5. public EHCacheService(){
6. this.cache=cacheManager.getCache("ehcacheName")
7. }
8.
9. public Cache getCache() {
10. return cache;
11. }
12.
13. public void setCache(Cache cache) {
14. this.cache = cache;
15. }
16.
17.
18.
19. /*
20. * 通过名称从缓存中获取数据
21. */
22. public Object getCacheElement(String cacheKey) throws Exception {
23. net.sf.ehcache.Element e = cache.get(cacheKey);
24. if (e == null) {
25. return null;
26. }
27. return e.getValue();
28. }
29. /*
30. * 将对象添加到缓存中
31. */
32. public void addToCache(String cacheKey, Object result) throws Exception {
33. Element element = new Element(cacheKey, result);
34. cache.put(element);
35. }
36.
37.
38. }
39.
public class EHCache {
private static final CacheManager cacheManager = new CacheManager();
private Cache cache;
public EHCacheService(){
this.cache=cacheManager.getCache("ehcacheName")
}
public Cache getCache() {
return cache;
}
public void setCache(Cache cache) {
this.cache = cache;
}
/*
* 通过名称从缓存中获取数据
*/
public Object getCacheElement(String cacheKey) throws Exception {
net.sf.ehcache.Element e = cache.get(cacheKey);
if (e == null) {
return null;
}
return e.getValue();
}
/*
* 将对象添加到缓存中
*/
public void addToCache(String cacheKey, Object result) throws Exception {
Element element = new Element(cacheKey, result);
cache.put(element);
}
}
2,测试
[java] view plain copy
print?
1.
2. public class Test{
3. EHCache ehCache = new EHCache();
4. public void Test(){
5. //测试将json对象存入缓存中
6. JSONObject obj = new JSONObject();
7. obj.put("name","lsz");
8. ehCache.addToCache("cache_json",obj);
9.
10. //从缓存中获取
11. JSONObject getobj = (JSONObject)ehCache.getCacheElement("cache_json");
12. System.out.println(getobj.toString());
13. }
14. }
15.
public class Test{
EHCache ehCache = new EHCache();
public void Test(){
//测试将json对象存入缓存中
JSONObject obj = new JSONObject();
obj.put("name","lsz");
ehCache.addToCache("cache_json",obj);
//从缓存中获取
JSONObject getobj = (JSONObject)ehCache.getCacheElement("cache_json");
System.out.println(getobj.toString());
}
}
三:问题解决
1,框架环境是自己搭建的,添加ehcache后运行出错:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/cache]
Offending resource: class path resource [applicationContext.xml]
出现这种问题,原因是因为在applicationContext.xml文件中 多加了
<cache:annotation-driven cache-manager="cacheManager" /> 将其去掉即可
2,框架需要添加jar包
spring-context-support-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar