最近想利用spring aop 与 ehcache 设计一个缓存框架,我的想法是,配置ehcache.xml参数,然后在applicationContext.xml中配置脚本:
<bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
</bean>
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager"/>
</property>
<property name="cacheName">
<value>mobileCache</value>
</property>
</bean>
<bean id="methodCacheInterceptor" class="sparknet.starshine.cache.user.test.MethodCacheInterceptor">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>
<bean id="methodCacheAfterAdvice" class="com.co.cache.ehcache.MethodCacheAfterAdvice">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>
<bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheInterceptor"/>
</property>
<property name="patterns">
<list>
<value>cache.user.service.*UserService*\.*listUser.*</value>
</list>
</property>
</bean>
<bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheAfterAdvice"/>
</property>
<property name="patterns">
<list>
<value>sparknet.starshine.cache.user.service.*UserService*\.*save.*</value>
</list>
</property>
</bean>
现在遇到两个问题:
1.在拦截listUser方法时,是将该方法以hashmap类型存入缓存中,key以类名+参数名+参数值,但是如果参数是复杂类型,如对象,数组就无法解决了,不知有什么更好的方法吗?
public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean
{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 = this.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 ++ ){
if(arguments[i] != null && !arguments[i].equals( "" )){
sb.append( "." ).append( arguments[i].getClass().getName() ).append( "." ).append( arguments[i].toString() );
}
}
}
return sb.toString();
}
}
2.在拦截save等增删改方法时,会后置通知,删除相应的缓存,目前的通知方法,只能根据save等方法的类名删除该类名下的所有缓存,这样太死,不好,我想要一种灵活的方式,通过xml配置,save等操作绑定某些缓存的方法,这样我save操作时,就可以删除这些绑定的缓存。现在的问题是,不知道aop怎么去自定义配置?
public class MethodCacheAfterAdvice implements AfterReturningAdvice, InitializingBean{
public void afterReturning( Object obj, Method method, Object[] aobj, Object obj1 ) throws Throwable
{
String className = obj1.getClass().getName();
List list = cache.getKeys();
for(int i = 0;i<list.size();i++){
String cacheKey = String.valueOf(list.get(i));
if(cacheKey.startsWith(className)){
cache.remove(cacheKey);
logger.debug("remove cache " + cacheKey);
}
}
}
}