开发者社区 问答 正文

Spring中ehcache配置拦截器缓存问题

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"    
    monitoring="autodetect">
    <!-- 设置缓存文件 .data 的创建路径。
         如果该路径是 Java 系统参数,当前虚拟机会重新赋值。
         下面的参数这样解释:
         user.home – 用户主目录
         user.dir      – 用户当前工作目录
         java.io.tmpdir – 默认临时文件路径 -->
    <diskStore path="java.io.tmpdir"/>
    <!--缺省缓存配置。CacheManager 会把这些配置应用到程序中。
        下列属性是 defaultCache 必须的:
        maxInMemory           - 设定内存中创建对象的最大值。
        eternal                        - 设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超
                                              时限制且元素永不消亡。
        timeToIdleSeconds  - 设置某个元素消亡前的停顿时间。
                                              也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。
                                              这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则
                                              设置该属性也无用)。
                                              如果该值是 0 就意味着元素可以停顿无穷长的时间。
        timeToLiveSeconds - 为元素设置消亡前的生存时间。
                                               也就是一个元素从构建到消亡的最大时间间隔值。
                                               这只能在元素不是永久驻留时有效。
        overflowToDisk        - 设置当内存中缓存达到 maxInMemory 限制时元素是否可写到磁盘
                                               上。
        -->
    <defaultCache  
        maxElementsInMemory="1000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"  
        /> 
    <cache name="eLearnCache"
        maxElementsInMemory="300"
        eternal="false"
        timeToIdleSeconds="5000"
        timeToLiveSeconds="5000"
        overflowToDisk="true"
        />
</ehcache>



Spring配置

<!-- ======================缓存 ======================= -->
	<!-- 引用ehCache的配置 -->  
    <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
      <property name="shared" value="true"></property>
      <property name="configLocation">  
        <value>classpath:ehcache.xml</value>  
      </property>  
    </bean>  
      
    
 ;  
    <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
      <property name="cacheManager">  
        <ref local="defaultCacheManager"/>  
      </property>  
      <property name="cacheName">  
          <value>eLearnCache</value>  
      </property>  
    </bean>  
  
    <!-- find/create cache拦截器 -->  
    <bean id="methodCacheInterceptor" class="com.elearn.cache.MethodCacheInterceptor">  
      <property name="cache">  
        <ref local="ehCache" />  
      </property>  
    </bean>  
    <!-- flush cache拦截器 -->  
    <bean id="methodCacheAfterAdvice" class="com.elearn.cache.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>.*queryForLong.*</value>  
            <value>.*find.*</value>  
            <value>.*query.*</value>  
            <value>.*queryForInt.*</value>  
            <value>.*queryForRowSet.*</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>.*update.*</value>  
          <value>.*updateObject.*</value>  
          <value>.*batchUpdateObjectList.*</value>  
          <value>.*updateMap.*</value>  
          <value>.*batchUpdateListMap.*</value>  
          <value>.*batchUpdate.*</value>  
        </list>  
      </property>  
    </bean>



拦截器配置后,调用相关方法无法调用到ehcache缓存,麻烦各位大神给看看到底是什么地方的问题,第一次使用缓存框架!

展开
收起
huc_逆天 2020-05-28 09:15:59 561 分享 版权
阿里云 AI 助理回答

从你提供的配置片段来看,你正在尝试在Spring项目中集成EHCache作为二级缓存,并且通过Spring的AOP(面向切面编程)来拦截特定的方法调用以实现缓存功能。但是遇到了方法调用没有命中缓存的问题。这里有几个可能的原因和排查步骤:

  1. 确保EHCache配置正确加载:

    • 你的Spring配置指定了ehcache.xml的位置为classpath:ehcache.xml,请确认这个文件确实存在于项目的类路径下,并且格式正确无误。可以检查应用启动日志中是否有EHCache初始化成功的信息。
  2. 检查EHCache配置细节:

    • ehcache.xml中,每个cache元素需要有合理的配置,比如maxElementsInMemorytimeToLiveSeconds等属性,确保这些设置符合你的需求。
    • 确保你为想要缓存的数据定义了正确的cache名称,并且与你在Spring的@Cacheable@CachePut注解中指定的名称相匹配。
  3. @Cacheable和@CachePut注解使用:

    • 请确保你在希望被缓存的方法上正确使用了@Cacheable注解,并且指定了正确的缓存名称(如eLearnCache),以及合适的key生成策略(默认是基于方法参数)。例如:
      @Cacheable(value = "eLearnCache", key = "#someKey")
      public YourReturnType yourMethod(YourParameters someKey) {
       // 方法逻辑
      }
      
    • 对于更新操作,应该使用@CachePut来同步更新缓存中的数据。
  4. AOP配置是否生效:

    • 你提到的“拦截器配置”实际上应该是Spring AOP的配置。请确保你的AOP代理模式(默认是JDK动态代理)能够覆盖到你想要拦截的类和方法。如果目标对象是接口的实现类,那么默认配置应该没问题;如果是普通类,则可能需要调整为CGLIB代理模式。
  5. 查看日志信息:

    • 开启详细的日志记录,特别是关于Spring Cache和EHCache的调试日志,这将帮助你了解哪些方法被拦截,缓存是否被命中,以及任何潜在的错误信息。
  6. 版本兼容性问题:

    • 确认Spring框架、Spring Cache以及EHCache的版本之间不存在兼容性问题。有时候不同版本之间的API变化可能会导致配置不生效。

如果以上步骤都检查过还是无法解决问题,建议提供更详细的错误信息或者日志内容,以便进行更深入的分析。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答