outspacemuyi 2014-08-05 01:48 采纳率: 0%
浏览 1832

spring aop 拦截器 ehcache 缓存加载

最近想利用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);

}

}

}
}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 19:37
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能