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>
<!-- ======================缓存 ======================= -->
<!-- 引用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>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
从你提供的配置片段来看,你正在尝试在Spring项目中集成EHCache作为二级缓存,并且通过Spring的AOP(面向切面编程)来拦截特定的方法调用以实现缓存功能。但是遇到了方法调用没有命中缓存的问题。这里有几个可能的原因和排查步骤:
确保EHCache配置正确加载:
ehcache.xml的位置为classpath:ehcache.xml,请确认这个文件确实存在于项目的类路径下,并且格式正确无误。可以检查应用启动日志中是否有EHCache初始化成功的信息。检查EHCache配置细节:
ehcache.xml中,每个cache元素需要有合理的配置,比如maxElementsInMemory、timeToLiveSeconds等属性,确保这些设置符合你的需求。cache名称,并且与你在Spring的@Cacheable或@CachePut注解中指定的名称相匹配。@Cacheable和@CachePut注解使用:
@Cacheable注解,并且指定了正确的缓存名称(如eLearnCache),以及合适的key生成策略(默认是基于方法参数)。例如:
@Cacheable(value = "eLearnCache", key = "#someKey")
public YourReturnType yourMethod(YourParameters someKey) {
// 方法逻辑
}
@CachePut来同步更新缓存中的数据。AOP配置是否生效:
查看日志信息:
版本兼容性问题:
如果以上步骤都检查过还是无法解决问题,建议提供更详细的错误信息或者日志内容,以便进行更深入的分析。