对象缓存就是将查询的数据,添加到缓存中,下次再次查询的时候直接从缓存中获取,而不去数据库中查询。
对象缓存一般是针对方法、类而来的,结合Spring的Aop对象、方法缓存就很简单。这里需要用到切面编程,用到了Spring的MethodInterceptor
代码如下:
package com.last.cache.ehcache.filter;
import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean {
private static final Logger log = Logger.getLogger(MethodCacheInterceptor.class);
private Cache cache;
public void setCache(Cache cache) {
this.cache = cache;
}
public void afterPropertiesSet() throws Exception {
log.info(cache + " A cache is required. Use setCache(Cache) to provide one.");
}
public Object invoke(MethodInvocation invocation) throws Throwable {
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
Object result;
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = null;
synchronized (this) {
element = cache.get(cacheKey);
if (element == null) {
log.info(cacheKey + "加入到缓存: " + cache.getName());
// 调用实际的方法
result = invocation.proceed();
element = new Element(cacheKey, (Serializable) result);
cache.put(element);
} else {
log.info(cacheKey + "使用缓存: " + cache.getName());
}
}
return element.getValue();
}
private String getCacheKey(String targetName, String methodName, Object[] arguments) {
StringBuffer sb = new StringBuffer();
sb.append(targetName).append(".").append(methodName);
if ((arguments != null) && (arguments.length != 0)) {
for (int i = 0; i < arguments.length; i++) {
sb.append(".").append(arguments[i]);
}
}
return sb.toString();
}
}
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
spring-ehcache.xml
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:component-scan base-package="com.last" />
<!-- 配置eh缓存管理器 -->
<bean id="mycacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:shared="true" />
<!-- 配置一个简单的缓存工厂bean对象 -->
<bean id="simpleCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager" ref="mycacheManager" />
<!-- 使用缓存 关联ehcache.xml中的缓存配置 -->
<property name="cacheName" value="mobileCache" />
</bean>
<!-- 配置一个缓存拦截器对象,处理具体的缓存业务 -->
<bean id="cacheAdvice" class="com.last.cache.ehcache.filter.MethodCacheInterceptor">
<property name="cache" ref="simpleCache" />
</bean>
<aop:config proxy-target-class="false">
<!-- "execution(* com.last.service.*.*(..))" -->
<aop:pointcut id="pc"
expression="(execution(* com.last.service.impl.*.find*(..))) or (execution(* com.last.service.impl2.*.find*(..)))" />
<aop:advisor pointcut-ref="pc" advice-ref="cacheAdvice" />
</aop:config>
</beans>
AOP的配置展示了如何使用多个表达式,有一点要注意,过滤包名下的任意方法要多一个星,如:execution(* com.last.service.impl.*.find*(..)) 表示impl下及子包下的方法,并不能去掉这个星,变成execution(* com.last.service.impl.find*(..)) ,表示impl下的方法也不能改成上面这样。
分享到:
相关推荐
1.解压缩到目录下,复制ehcache-monitor-kit-1.0.0\lib\ehcache-probe-1.0.0.jar包到application的web-inf/lib目录下 2.将以下配置copy的ehcache.xml文件的ehcache标签中,注:上述链接中说的配置少写了个probe包名...
具有集群支持的 Ehcache 3(ehcache-clustered-3.8.1-kit.zip)
ehcache-core-2.6.10.jar依赖包 MyBatiesEhCache二级缓存 Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个...
ehcache缓存jar(ehcache-core-2.4.6.jar+ehcache-web-2.0.4.jar)
赠送jar包:shiro-ehcache-1.4.0.jar; 赠送原API文档:shiro-ehcache-1.4.0-javadoc.jar; 赠送源代码:shiro-ehcache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-ehcache-1.4.0.pom; 包含翻译后的API文档...
Mybatis-ehcache 1.2.1源码(ehcache-cache-mybatis-ehcache-1.2.1.tar.gz)
ehcache1.6,ehcache-web-2.0.4,slf4j-api-1.7.21.jar 不好意思,缺点分用。。
Mybatis-ehcache 1.2.1源码(ehcache-cache-mybatis-ehcache-1.2.1.zip)
赠送jar包:ehcache-3.3.1.jar; 赠送原API文档:ehcache-3.3.1-javadoc.jar; 赠送源代码:ehcache-3.3.1-sources.jar; 赠送Maven依赖信息文件:ehcache-3.3.1.pom; 包含翻译后的API文档:ehcache-3.3.1-javadoc-...
ehcache-1.6.0-beta1-javadoc
赠送jar包:ehcache-3.3.1.jar; 赠送原API文档:ehcache-3.3.1-javadoc.jar; 赠送源代码:ehcache-3.3.1-sources.jar; 赠送Maven依赖信息文件:ehcache-3.3.1.pom; 包含翻译后的API文档:ehcache-3.3.1-javadoc-...
赠送jar包:ehcache-2.10.0.jar; 赠送原API文档:ehcache-2.10.0-javadoc.jar; 赠送源代码:ehcache-2.10.0-sources.jar; 包含翻译后的API文档:ehcache-2.10.0-javadoc-API文档-中文(简体)版.zip 对应Maven...
赠送jar包:ehcache-2.10.0.jar 赠送原API文档:ehcache-2.10.0-javadoc.jar 赠送源代码:ehcache-2.10.0-sources.jar 包含翻译后的API文档:ehcache-2.10.0-javadoc-API文档-中文(简体)-英语-对照版.zip 对应...
赠送jar包:shiro-ehcache-1.3.2.jar; 赠送原API文档:shiro-ehcache-1.3.2-javadoc.jar; 赠送源代码:shiro-ehcache-1.3.2-sources.jar; 赠送Maven依赖信息文件:shiro-ehcache-1.3.2.pom; 包含翻译后的API文档...
Ehcache 2.10.8(bigmemory-max-4.3.8.4.2.tar.gz)
赠送jar包:shiro-ehcache-1.2.3.jar; 赠送原API文档:shiro-ehcache-1.2.3-javadoc.jar; 赠送源代码:shiro-ehcache-1.2.3-sources.jar; 赠送Maven依赖信息文件:shiro-ehcache-1.2.3.pom; 包含翻译后的API文档...
用于ehcache-core-ee-2.4.1 企业级功能。比如ehcache的offheapsize设置
赠送jar包:ehcache-web-2.0.4.jar; 赠送原API文档:ehcache-web-2.0.4-javadoc.jar; 赠送源代码:ehcache-web-2.0.4-sources.jar; 赠送Maven依赖信息文件:ehcache-web-2.0.4.pom; 包含翻译后的API文档:...
赠送jar包:ehcache-3.9.9.jar; 赠送原API文档:ehcache-3.9.9-javadoc.jar; 赠送源代码:ehcache-3.9.9-sources.jar; 赠送Maven依赖信息文件:ehcache-3.9.9.pom; 包含翻译后的API文档:ehcache-3.9.9-javadoc-...