All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.hmtools.servicer.ehCache2_10.EhCacheServicer Maven / Gradle / Ivy

There is a newer version: 0.0.2.20180312
Show newest version
package top.hmtools.servicer.ehCache2_10;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;

/**
 * 基于ehcache2.10.4实现
 * @author HyboJ
 *
 */
public class EhCacheServicer {

	/**
	 * ehcache
	 */
	public Cache cache;
	
	public EhCacheServicer() {
		CacheConfiguration cacheConfiguration = new CacheConfiguration("metaCache", 10000);
		cacheConfiguration.maxEntriesLocalHeap(1000);
		cacheConfiguration.maxEntriesLocalDisk(100000);
		cacheConfiguration.eternal(false);
		cacheConfiguration.diskSpoolBufferSizeMB(20);
		cacheConfiguration.timeToIdleSeconds(600);
		cacheConfiguration.timeToLiveSeconds(600);
		cacheConfiguration.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU);
		cacheConfiguration.transactionalMode("off");
		
		this.init(cacheConfiguration);
	}

	public EhCacheServicer(CacheConfiguration cacheConfiguration) {
		this.init(cacheConfiguration);
	}
	
	public void init(CacheConfiguration cacheConfiguration){
		Configuration ehConfig = new Configuration();
		
		//缺省配置
		CacheConfiguration defaultCacheConfig = new CacheConfiguration();
		defaultCacheConfig.maxEntriesLocalHeap(1000);
		defaultCacheConfig.eternal(false);
		defaultCacheConfig.timeToIdleSeconds(120);
		defaultCacheConfig.timeToLiveSeconds(120);
		defaultCacheConfig.diskSpoolBufferSizeMB(30);
		defaultCacheConfig.maxEntriesLocalDisk(10000);
		defaultCacheConfig.diskExpiryThreadIntervalSeconds(120);
		defaultCacheConfig.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU);
		ehConfig.defaultCache(defaultCacheConfig);
		
		//自定义配置
		ehConfig.cache(cacheConfiguration);
		
		//获取缓存
		CacheManager manager = CacheManager.create(ehConfig);  
		cache = manager.getCache(cacheConfiguration.getName());
	}
	
	public void destory() {
		this.cache.removeAll();
	}
	
	public List keys(){
		return this.cache.getKeys();
	}

	public void put(Object key, Object value) {
		Element element = new Element(key, value);
		cache.put(element);
	}

	public void put(Serializable key, Serializable value) {
		Element element = new Element(key, value);
		cache.put(element);
	}

	public Object get(Object key) {
		return this.cache.get(key).getObjectValue();
	}

	public Object get(Serializable key) {
		Element element = this.cache.get(key);
		if(element == null){
			return null;
		}
		return element.getObjectValue();
	}

	public Object replace(Object key, Object value) {
		Element element = new Element(key, value);
		Element replace = cache.replace(element);
		return replace.getObjectValue();
	}

	public void removeAll() {
		this.cache.removeAll();
	}

	public void removeAll(Collection keys) {
		this.cache.removeAll(keys);
	}
	
	public void remove(Object key){
		this.cache.remove(key);
	}
	
	public void remove(Serializable key){
		this.cache.remove(key);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy