SSM 配置encache没有效果问题

本文详细介绍了在SSM框架中配置EHCache的具体步骤,并指出了常见的配置误区。特别强调了在同一类的不同方法间调用缓存方法时可能遇到的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ssm配置ehcache没有效果

ehcache.xml:
  <?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="D:/cache" />       
<!-- <diskStore path="java.io.tmpdir" /> -->       
    <defaultCache eternal="false" maxElementsInMemory="100" overflowToDisk="true" />
    <cache name="tokenCache" maxElementsInMemory="1000" eternal="true"  timeToIdleSeconds = "7200"   timeToLiveSeconds = "7200"   overflowToDisk="true" diskPersistent="true"/>
    <cache name="ticketCache" maxElementsInMemory="1000" eternal="true"  timeToIdleSeconds = "7200"   timeToLiveSeconds = "7200"  overflowToDisk="true" diskPersistent="true"/>  
</ehcache>

applicationContext.xml 代码:
  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/cache   
            http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

<!-- 设置定时任务 -->
    <task:annotation-driven/>

<context:component-scan base-package="com.zlw.*"/>

<!-- 缓存配置 -->  
    <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->  
    <cache:annotation-driven cache-manager="cacheManager"/>
    <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->  
    <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->  
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation" value="classpath:ehcache.xml"/>  
    </bean>  
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
        <property name="cacheManager" ref="cacheManagerFactory"/>  
    </bean>
...
web.xml:
   <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
 version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
...

java:
  
@Cacheable(value = "tokenCache")
public String getAccessToken() {
// TODO Auto-generated method stub
String accessToken = null;
// 如果存储在数据库的token没有过期,则用当前token,否则重新获取token并更新数据库的token
// accessToken = iWxJSSDKService.getAccessToken();

/*if (!"-1".equalsIgnoreCase(accessToken)) {
log.debug("存储token为:" + accessToken);
return accessToken;
}*/
          return "获取到的token";
        }

 

现在终于知道了是那里有问题了,我的这个问题不是配置的问题,而是调用的问题。

不能在同一个类的方法里面调用这个类的缓存方法。

即A类有缓存方法A及方法B。那么不能在方法B中直接调用方法A,否则A方法中的缓存将不起作用!!! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值