Authored by hugufei

添加encache的配置文件

package com.yoho.search.common.cache.impls;
import java.io.InputStream;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -8,19 +15,13 @@ import org.springframework.stereotype.Service;
import com.yoho.search.common.cache.model.CacheObject;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
@Service("ehCache")
public class EhCache implements CacheInterface {
private static final Logger logger = LoggerFactory.getLogger(EhCache.class);
private static final CacheManager cacheManager = CacheManager.create();
private CacheManager manager;
private static final String cacheName = "talent";
private Cache cache;
private EhCache() {
... ... @@ -28,10 +29,22 @@ public class EhCache implements CacheInterface {
@PostConstruct
private void init() {
cacheManager.addCache(cacheName);
this.cache = cacheManager.getCache(cacheName);
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
if (inputStream == null) {
logger.error("url is null");
return;
}
this.manager = CacheManager.create(inputStream);
this.cache = manager.getCache("yohosearch");
}
@PreDestroy
private void Destroy() {
if (this.manager != null) {
this.manager.shutdown();
}
}
@Override
public void addOrUpdate(String key, CacheObject value, int expiredTimeInMinute) {
try {
... ...
<?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="false">
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10000"
maxElementsOnDisk="0"
eternal="true"
overflowToDisk="true"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
diskSpoolBufferSizeMB="50"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LFU" />
<cache name="yohosearch"
maxElementsInMemory="3000"
maxElementsOnDisk="0"
eternal="false"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="120"
timeToLiveSeconds="300"
diskSpoolBufferSizeMB="50"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
\ No newline at end of file
... ...