这是我的实际方法。
当我逐秒刷新我的应用程序时,日期参数始终是更新。
控制器
@Controller
public class MainController implements ToCache {
@RequestMapping(value = {"/", "/index"})
@Cacheable(value = "main", key = "#root.method.name")
public String showHome(HttpServletRequest request) {
request.setAttribute("now", new Date());
request.setAttribute("tmp", System.getProperty("java.io.tmpdir"));
return "index";
}
}
界面
public interface ToCache {
String showHome(HttpServletRequest request);
}
的Ehcache-servlet.xml中
<mvc:annotation-driven />
<context:component-scan base-package="com.pjcom.**" />
<!-- Manejador de vistas -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/view/"
p:suffix=".jsp"
p:exposeContextBeansAsAttributes="true" />
Ehcache与Spring的集成
applicationContext.xml中
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"/>
</bean>
<bean id ="enhancedDefaultKeyGenerator" class="org.springframework.cache.interceptor.DefaultKeyGenerator"/>
<cache:annotation-driven mode="proxy"
proxy-target-class="true"
cache-manager="cacheManager"
key-generator="enhancedDefaultKeyGenerator"/>
<!-- END EHCACHE -->
Ehcache配置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true"
maxBytesLocalHeap="150M">
<diskStore path="java.io.tmpdir"/>
<cache name="main"
maxBytesLocalHeap="100M"
eternal="false"
t
imeToIdleSeconds="10"
timeToLiveSeconds="10"
overflowToDisk="true"
maxElementsOnDisk="1000"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
运行时日志,部署和刷新。
31-Mar-2014 21:33:20.881 INFO [http-nio-8084-exec-52] org.apache.catalina.startup.HostConfig.undeploy Repliegue (undeploy) de la aplicación web que tiene como trayectoria de contexto /springehcache
31-Mar-2014 21:33:20.917 INFO [http-nio-8084-exec-53] org.apache.catalina.startup.HostConfig.deployDescriptor Desplieque del descriptor de configuración C:\...\springehcache.xml
31-Mar-2014 21:33:20.919 WARNING [http-nio-8084-exec-53] org.apache.catalina.startup.SetContextPropertiesRule.begin [SetContextPropertiesRule]{Context} Setting property 'antiJARLocking' to 'true' did not find a matching property.
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
31-Mar-2014 21:33:22.843 INFO [http-nio-8084-exec-50] org.apache.catalina.util.LifecycleBase.start The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/springehcache]] after start() had already been called. The second call will be ignored.