【发布时间】:2016-05-13 14:48:21
【问题描述】:
我们有基于 spring(3.2.9.Release) 的 java web 应用程序,并使用 hibernate 进行数据库操作。 我们目前有使用通过 WebSphere 服务器配置的 Dynacache 并使用 jndi 映射的缓存机制。我们在第一页加载时从数据库中检索所有内容并将其存储在 Dynacache 中。由于每次都是外部调用,所以我们想实现 Eh-Cache 并提高性能。但令人惊讶的是,Eh-Cache 的性能不如 Dynacache 并且加载页面需要很长时间。下面是我们对 Eh-Cache 的配置:
xml配置:
<bean id="cacheService" class="com.wlp.sales.ols.core.api.cache.CacheService"></bean>
<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="/WEB-INF/configs/EhCache/ehcache.xml" />
<property name="shared" value="true" />
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<cache name="contentCache"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="0" timeToLiveSeconds="0"
memoryStoreEvictionPolicy="LRU"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
依赖关系:
<!-- ehCache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
我们有一个缓存实现类,它将使用 get 和 put 方法生成缓存键,从数据库中检索并作为键值对放入缓存映射中。
public Object get(CdiRequest request) {
Object cdiObject = cacheService.get(request.getContentElement()
.getContentType(), keyBuilder.build(request));
return cdiObject instanceof CdiResponse ? (CdiResponse) cdiObject
: request;
}
//Put方法实现:
cacheService.put(cdiResponse.getCdiRequest().getContentElement()
.getContentType(),
keyBuilder.build(cdiResponse.getCdiRequest()), cdiResponse);
实现类:
public class CacheService implements ApplicationContextAware{
@Autowired
private CacheManager cacheManager;
private ApplicationContext applicationContext;
public Object get(String applnName, Object key) {
Cache cache = cacheManager.getCache("contentCache");
return cache.get(key);
}
public boolean put(String applnName, Object key, Object value) {
Cache cache = cacheManager.getCache("contentCache");
cache.put(key, value);
return true;
}
}
刷新或重新加载每个页面大约需要 60 到 80 秒,而 dynacache 只需要 3-4 秒。请告知是否有任何错误或可以以更好的方式完成。
【问题讨论】:
标签: java spring spring-mvc caching ehcache