|
|
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 {
|
...
|
...
|
|