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

com.holmos.cache.store.impl.MemoryCacheStore Maven / Gradle / Ivy

There is a newer version: 1.0.2u10
Show newest version
package com.holmos.cache.store.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.holmos.cache.cache.AbstractCache;
import com.holmos.cache.config.MemoryCacheConfigComponent;
import com.holmos.cache.element.Element;
import com.holmos.cache.exception.CacheException;
import com.holmos.cache.outpolicy.CacheOutPolicy;
import com.holmos.cache.status.CacheStatus;


/**
 * JVM 内部内存存储器
 * 
 * @author: 吴银龙([email protected])
 * @version: 2013-3-19 下午12:55:49 
 */

public class MemoryCacheStore extends AbstractCacheStore{

	public MemoryCacheStore(MemoryCacheConfigComponent configComponent) {
		super(configComponent);
		initializeStore();
	}

	/**
	 * 初始化存储器的map存储结构
	 * */
	private void initializeStore() {
		MemoryCacheConfigComponent config = (MemoryCacheConfigComponent) configComponent;
		cacheStore = new ConcurrentHashMap((int)config.getInitCount(),config.getDilatationSpeed(),
				config.getComplicateCount());
	}

    Map cacheStore = null;
	
	@Override
	public boolean put(Element element) throws CacheException {
		if(null == element)
			return false;
		if(cacheStore.get(element.getKey()) != null){
			update(element);
			return false;
		}
		boolean locked = element.lockWrite();
		if(!locked){
			cacheStore.put(element.getKey(), element);
			element.resetAccessStatistics();
			element.releaseWrite();
			return true;
		}
		return false;
	}

	@Override
	public void putAll(Collection elements) throws CacheException {
		if( null == elements)
			return;
		for(Element element : elements)
			put(element);
	}

	@Override
	public Element get(Object key) {
		Element element = cacheStore.get(key);
		if(null != element)
			element.updateAccessStatistics();
		return element;
	}

	@Override
	public Element getQuite(Object key) {
		return cacheStore.get(key);
	}

	@Override
	public Set getAllKeys() {
		return cacheStore.keySet();
	}

	@Override
	public Element remove(Object key) {
		if( null == key)
			return null;
		return cacheStore.remove(key);
	}

	@Override
	public void removeAll(Collection keys) {
		if(null != keys){
			for(Object key : keys)
				remove(key);
		}
	}

	@Override
	public void clear() throws CacheException {
		cacheStore.clear();
	}

	@Override
	public synchronized void dispose() {
		
	}

	@Override
	public int getCacheSize() {
		
		return AbstractCache.sizeGetter.getDataSize(cacheStore);
	}

	@Override
	public int getElementAmount() {
		return cacheStore.size();
	}

	@Override
	public CacheStatus getStatus() {
		return cacheStatus;
	}

	@Override
	public boolean containsKey(Object key) {
		if( null == key)
			return false;
		return cacheStore.containsKey(key);
	}

	@Override
	public void flush() {
		
	}

	@Override
	public CacheOutPolicy getOutPolicy() {
		return outPolicy;
	}

	@Override
	public void setCacheOutPolicy(CacheOutPolicy outPolicy) {
		this.outPolicy = outPolicy;
	}

	@Override
	public void removeExpireData() {
		for(Element element : cacheStore.values()){
			if(element.isExpired())
				cacheStore.remove(element.getKey());
		}
	}

	@Override
	public List getAll(Collection keys) {
		List elements = new ArrayList();
		for(Object key: keys){
			elements.add(cacheStore.get(key));
		}
		return elements;
	}

	@Override
	public boolean update(Element element) {
		if(null == element)
			return false;
		if(null == cacheStore.get(element.getKey())){
			put(element);
			return false;
		}
		boolean oriWriteLock = element.lockWrite();
		if(!oriWriteLock){
			cacheStore.put(element.getKey(), element);
			element.updateModifyStatistics();
			element.releaseWrite();
			return true;
		}
		return false;
	}

	@Override
	public boolean updateAll(Collection elements) {
		boolean succeed = true;
		for(Element element : elements){
			if(!update(element))
				succeed = false;
		}
		return succeed;
	}
	
}