Spring3.1 MVC +Ehcache问题
鲁虾 2012-04-07 02:44:15 现在用的spring3.1的mvc 想在读取数据时增加ehcache,但不知道怎么调用,目前还是不能用缓存
------------------------------
seach.java
@Service
public class Search{
@Cacheable("andCache")
public String getString()
{
System.out.println("getSearchResult is running...");
return "1230-1230-1230-0123-0123";
}
}
---------------------------------
IndexController.java
@Controller
public class IndexController
{
@RequestMapping("/t")
@ResponseBody
public String getString(HttpServletRequest request) {
//earchUtil service = (SearchUtil)applicationContext.getBean("searchUtil"); //不知道这里怎么写,要是用Search search=new Search(); 还是能打印出来"getSearchResult is running...",说明没用缓存,
String sr =service.getString();
return sr;
}
}
----------------------------
环境:spring3.1+ehcache2.4.4
其他配置文件
***-servlet.xml
<!-- 启用缓存注解功能 -->
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml" />
<!-- 声明cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="cacheManagerFactory" />
***********************************
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">
<!--
<diskStore path="java.io.tmpdir" /> -->
<diskStore path="E:/cachetmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<cache name="andCache" maxElementsInMemory="10000"
maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU" />
</ehcache>