Java springmvc中的ehcaching
我在SpringMVC中使用ehcache进行缓存。考虑到一个控制器通过各种方法提供数据多次连接数据库的情况,如果我们使用缓存来缓存所有的服务都有相同的缓存名吗? 例如:Java springmvc中的ehcaching,java,spring-mvc,caching,ehcache,Java,Spring Mvc,Caching,Ehcache,我在SpringMVC中使用ehcache进行缓存。考虑到一个控制器通过各种方法提供数据多次连接数据库的情况,如果我们使用缓存来缓存所有的服务都有相同的缓存名吗? 例如: @RequestMapping(value="/test",method=RequestMethod.GET) public String mycontroller(){ ... List1= Service.get(Id); Info = Service.getInfo(Id);
@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"/>
一般来说,您应该使用多个缓存名称,从而使用多个缓存,每个服务类一个或多个缓存
此外,在大多数EhCache配置中,应用程序重新启动时,缓存的数据将丢失。但是,如果您查阅,您会发现,如果需要,您可以让缓存在重新启动期间变得持久。我建议您使用单个缓存来存储所有服务数据,因为缓存可以存储键值类型的多个条目,这是缓存的基本用途之一
在只需要缓存少量服务的情况下,很容易维护缓存。但如果服务是多个的,则创建缓存是不可取的 您是否将Spring缓存抽象与EhCache一起使用@爱德华·史密斯:是的,我用。运行时出现此异常:org.springframework.cache.ehcache.ehcachemanager您在XML配置中拼错了ehcachemanager,它应该是:ehcachemanager。