EhCache.java 3.98 KB
package com.yoho.search.cache.impls;

import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.cache.model.CacheObject;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.Statistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.InputStream;
import java.util.*;

@Service("ehCache")
public class EhCache implements CacheInterface {

	private static final Logger logger = LoggerFactory.getLogger(EhCache.class);

	private CacheManager manager;

	private Cache cache;

	private EhCache() {
	}

	@PostConstruct
	private void init() {
		try {
			InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ehcache.xml");
			this.manager = CacheManager.create(inputStream);
			this.cache = manager.getCache("yohosearch");
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
	public Statistics getStatistics(){
		return cache.getStatistics();
	}

	@PreDestroy
	private void destroy() {
		if (this.manager != null) {
			this.manager.shutdown();
		}
	}

	@Override
	public void addOrUpdate(RedisKeyBuilder redisKeyBuilder, CacheObject value, int expiredTimeInMinute) {
		try {
			Element element = new Element(redisKeyBuilder.getKey(), value);
			element.setTimeToLive(expiredTimeInMinute * 60);
			cache.put(element);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public CacheObject get(RedisKeyBuilder redisKeyBuilder) {
		try {
			Element e = cache.get(redisKeyBuilder.getKey());
			return (e == null) ? null : (CacheObject) e.getObjectValue();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}

	@Override
	public boolean exist(RedisKeyBuilder redisKeyBuilder) {
		return this.get(redisKeyBuilder) == null ? false : true;
	}

	@Override
	public void mutiSet(final Map<RedisKeyBuilder,String> map, final int expiredTimeInMinute) {
		try {
			Collection<Element> elements = new ArrayList<>();
			for (Map.Entry<RedisKeyBuilder, ? extends String> entry: map.entrySet()) {
				Element element = new Element(entry.getKey().getKey(), entry.getValue());
				element.setTimeToLive(expiredTimeInMinute * 60);
				elements.add(element);
			}
			cache.putAll(elements);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public List<String> mutiGet(Collection<RedisKeyBuilder> redisKeyBuilders) {
		Collection<String> keys = new ArrayList<>();
		for (RedisKeyBuilder redisKeyBuilder:redisKeyBuilders) {
			keys.add(redisKeyBuilder.getKey());
		}
		Map<Object, Element> elementMap = cache.getAll(keys);
		List<String> values = new ArrayList<>();
		for (RedisKeyBuilder redisKeyBuilder:redisKeyBuilders) {
			Element element = elementMap.get(redisKeyBuilder.getKey());
			if(element==null){
				values.add(null);
			}else{
				values.add((String)element.getObjectValue());
			}
		}
		return values;
	}

	@Override
	public boolean setBitSet(RedisKeyBuilder redisKeyBuilder, BitSet bitSet, int expiredTimeInMinute) {
		try {
			Element element = new Element(redisKeyBuilder.getKey(),bitSet);
			element.setTimeToLive(expiredTimeInMinute * 60);
			cache.put(element);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return false;
		}
	}

	@Override
	public Map<Integer, Boolean> getFromBitSet(RedisKeyBuilder key, Collection<Integer> offsets){
		try {
			Element e = this.cache.get(key.getKey());
			if(e==null){
				return null;
			}
			BitSet cacheBitSet = (BitSet)e.getObjectValue();
			Map<Integer, Boolean> results = new HashMap<>();
			for (Integer offset:offsets) {
				results.put(offset,cacheBitSet.get(offset));
			}
			return results;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
}