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
为了使用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
相关文章推荐
- java-Spring-1
- spring+mybatis配置多数据源总结,重点是动态加载数据源,支持动态切换
- Spring Security 保护业务层
- Java线程池使用说明
- java去重(1通过迭代器,2直接赋值)
- 在源Eclipse上安装Myeclipse插件
- Java读写Excel文件示例
- Java中的自动装箱与拆箱
- (java)Implement strStr()
- Java 发送邮件
- 在Eclipse中安装Hibernate tools、Spring、Activiti插件
- Java 数组详解 - 用法、遍历、排序、实用API
- 【JavaWeb Structs2学习】 路径介绍(在JSP页面a标签怎么写1)
- 使用Java导出Excel案例详解
- Java list的用法排序及遍历
- 模仿QQ5.0界面侧滑
- JAVA中堆栈的定义及区别
- Eclipse中使用Gradle实现APK分包
- Java程序员之Spring(一) 入门
- java桌面显示出错,显示占半边,显绿色。