81,092
社区成员
发帖
与我相关
我的任务
分享
<ehcache>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="500"
timeToLiveSeconds="1000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
<cache name="levelOneCache"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="200"
timeToLiveSeconds="300"
overflowToDisk="true"
/>
</ehcache>
<!-- 引用ehCache的配置 声明cacheManager -->
<bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<!-- 定义ehCache的工厂,并设置所使用的Cache name -->
<bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager"/>
</property>
<property name="cacheName">
<value>DEFAULT_CACHE</value>
</property>
</bean>
<!-- find/create cache拦截器 -->
<bean id="methodCacheInterceptor" class="com.dt.util.MethodCacheInterceptor">
<property name="cache">
<ref local="methodCache"/>
</property>
</bean>
<!-- 触发从缓存读取数据的方法名称 -->
<bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheInterceptor"/>
</property>
<!-- 下面的配置就使得在数据访问时,cache将拦截从数据库获取的数据,与cache数据比较,如有就不放入cache,没有就放入,更新到数据库去,也是先存入cache,再更新到数据库中去 -->
<property name="patterns">
<list>
<value>.*get.*</value>
<value>.*getUserList</value>
</list>
</property>
</bean>
<!-- 用户管理-->
<bean id="userDaoBean" class="com.dt.proc.userManage.UserDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="userTarget" parent="transationBase">
<property name="target">
<ref bean="userDaoBean"/>
</property>
</bean>
<bean id="userSerBean" class="com.dt.proc.userManage.UserService">
<property name="uDao">
<ref bean="userTarget" />
</property>
</bean>
//主要问题还是在这里,不知道怎么用这个bean,是怎样结合Struts中的action,执行他???
<bean id="userCacheBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- <property name="target">
<ref local="userSerBean"/>
</property> -->
<property name="interceptorNames">
<list>
<value>methodCachePointCut</value>
</list>
</property>
</bean>
<bean id="userActionBean" class="com.dt.proc.userManage.UserAction" scope="prototype" parent="userCacheBean">
<property name="userSer">
<ref bean="userSerBean" />
</property>
<!-- <property name="userSer">
<ref bean="userCacheBean" />
</property> -->
</bean>
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 {
System.out.println("cache ...");
log.info(cache + " A cache is required. Use setCache(Cache) to provide one.");
}
/**
* 拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值,
* 否则,返回数据库查询结果,并将查询结果放入cache
*/
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) {
System.out.println("cache add");
log.info(cacheKey + "加入到缓存: " + cache.getName());
// 调用实际的方法
result = invocation.proceed();
element = new Element(cacheKey, (Serializable) result);
cache.put(element);
} else {
System.out.println("cache user");
log.info(cacheKey + "使用缓存: " + cache.getName());
}
}
return element.getValue();
}
/**
* <b>function:</b> 返回具体的方法全路径名称 参数
* @author hoojo
* @createDate 2012-7-2 下午06:12:39
* @param targetName 全路径
* @param methodName 方法名称
* @param arguments 参数
* @return 完整方法名称
*/
/**
* 获得cache key的方法,cache key是Cache中一个Element的唯一标识
* cache key包括 包名+类名+方法名,如com.co.cache.service.UserServiceImpl.getAllUser
*/
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();
}
}
public class UserAction extends BaseAction {
ISerUser userSer;
getUserser(){...}
settUserser(){...}
public String getUserList(){
int ires = userSer.getUserList(request);
return "userList";
}
}
public class UserService extends BaseService implements ISerUser{
public int getUserList(HttpServletRequest request) {
// 调用dao....,获取数据
}
}