2014-11-03 125 views
0

我使用ehcache在Spring mvc缓存。考虑一个控制器通过多种方法提供数据连接数据库的次数如果我们使用缓存来缓存所有的服务都有相同的缓存名称或不是?在春天ehcaching mvc

例如:

@RequestMapping(value="/test",method=RequestMethod.GET) 
    public String mycontroller(){ 
     ... 

    List1= Service.get(Id); 
    Info = Service.getInfo(Id); 
    messages = Service.allMessage(Id); 
    ... 

}

而且如果服务器被重置,缓存的数据会丢失?

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhcacheCacheManager" p:cache-manager="ehcache"/> 

<!-- Ehcache library setup --> 
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="/WEB-INF/ehcache.xml"/> 

和ehcache.xml中

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> 
<defaultCache 
     maxElementsInMemory="500" eternal="true" overflowToDisk="false" memoryStoreEvictionPolicy="LFU"/> 
<cache name="contactCache" maxElementsInMemory="500" eternal="true" overflowToDisk="false" 
     memoryStoreEvictionPolicy="LFU"/> 
     <cache name="cachname" 
     maxElementsInMemory="100" 
     eternal="false" 
     timeToIdleSeconds="120" 
     timeToLiveSeconds="120" 
     overflowToDisk="true" 
     maxElementsOnDisk="10000000" 
     diskPersistent="false" 
     diskExpiryThreadIntervalSeconds="120" 
     memoryStoreEvictionPolicy="LRU"/> 

感谢

+0

您是否在使用EhCache的Spring Cache抽象? http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/cache.html – 2014-11-03 19:05:22

+0

@爱德华史密斯是的,我使用。当我运行它时,会发生以下异常:org.springframework.cache.ehcache.EhcacheCacheManager – ali 2014-11-03 19:08:40

+0

您在XML配置中拼错了EhcacheCacheManager,它应该是:'EhCacheCacheManager'。 – 2014-11-03 22:12:07

回答

0

一般来说,你应该使用多个高速缓存名称(并因此多个缓存),一个或每个服务类的更多。

此外,在大多数EhCache配置中,应用程序重新启动时,缓存的数据将丢失。但是,如果您参考documentation,您会发现,如果需要,您可以使缓存在重新启动时保持不变。