您的位置:首页 > 编程语言 > Java开发

AAA Spring Cache注解+ehcache(最完整的页面缓存实施方案)

2015-12-30 18:59 706 查看
Spring 的缓存只是作为一个把注解范围扩到了java EE的任何层次而已,底层鼻血加上诸如ehCache之类的缓存框架才可以实现

为了使用enchache,需要在类路径下加添加一个ehcache.xml文件,里面就是<cache name="A">...

需要注意的是spring注解里面的cache value值就是这个配置文件里面的A缓冲区

当然ehcache的与spring的配置也非常简单,只需要加载这个xml文件,然后在缓存管理器里面注入这个ehcache即可。

以后程序调用该缓存区的数据时,只要与之前的一样时,就立马走缓存而不是走数据库。它是按照一个个缓存区来搞的。

无论spring底层采用哪种缓存框架,都必须要在缓存管理器里面表明一下,这就是缓存在spring里面唯一的一个接口配置东西

@Cacheable :一般用在查询方法上面,保留缓存值

@CacheEvict:一般用在删除方法上,删除缓存值

@CachePut:一般用在更新方法上面,是重新赋缓存值的意思

一:详细配置步骤

1,添加ehcache.xml文件

将ehcache.xml文件添加到src路径下面。ehcache.xml文件内容如下

[java] view
plaincopy





<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文件中添加

[java] view
plaincopy





<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->

<cache:annotation-driven cache-manager="cacheManager" />

<!-- 声明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>

二:使用

1,定义EHCache工具方法

[java] view
plaincopy





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
plaincopy





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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: