com.holmos.cache.node.StandAloneCacheManager Maven / Gradle / Ivy
package com.holmos.cache.node;
import java.util.ArrayList;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.holmos.cache.cache.Cache;
import com.holmos.cache.cache.CacheFactory;
import com.holmos.cache.config.CacheConfig;
import com.holmos.cache.config.CacheConfigComponent;
import com.holmos.cache.exception.CacheException;
/**
* 单机缓存Manager,负责管理调度和调用部署在一台物理机器上的所有层次的缓存
*
* 不能有重名的缓存
* 不能有位于同一layer的缓存
*
* @author: 吴银龙([email protected])
* @version: 2013-3-12 下午9:26:18
*/
public class StandAloneCacheManager {
private final static Logger logger = LoggerFactory.getLogger(StandAloneCacheManager.class);
/**一个缓存节点的缓存配置*/
private CacheConfig config;
/**缓存节点信息*/
private ClusterNode nodeInfo;
private HashMap cachesOfOneNodeWithLayer;
private HashMap cachesOfOneNodeWithName;
private HashMap invalidCacheofOneNodeWithLayer;
private HashMap invalidCacheOfOneNodeWithName;
private ArrayList cachesOrderByLayer;
public StandAloneCacheManager(){
cachesOfOneNodeWithLayer = new HashMap();
cachesOfOneNodeWithName = new HashMap();
cachesOrderByLayer = new ArrayList();
}
public StandAloneCacheManager(CacheConfig config){
this();
this.setConfig(config);
}
public CacheConfig getConfig() {
return config;
}
public void setConfig(CacheConfig config) {
this.config = config;
}
/**
* 初始化单机缓存
* */
public void init(){
if( null == config)
throw new CacheException(nodeInfo.toString()+"没有加载缓存配置文件或者缓存配置文件设置错误!");
for(CacheConfigComponent component : config.getCacheComponents()){
if(cachesOfOneNodeWithLayer.get(component.getLayer()) != null)
throw new CacheException(nodeInfo.toString()+"存在不同的缓存具有相同的层次或者该层缓存已失效!");
if(cachesOfOneNodeWithName.get(component.getName()) != null)
throw new CacheException(nodeInfo.toString()+"存在不同的缓存具有相同的名字或者改名字缓存已失效!");
Cache cache = CacheFactory.createCache(component);
cachesOfOneNodeWithLayer.put(component.getLayer(), cache);
cachesOfOneNodeWithName.put(component.getName(), cache);
}
logger.info(nodeInfo.toString()+"上缓存启动成功!");
}
public void addCache(Cache cache){
if(cachesOfOneNodeWithLayer.get(cache.getLayer()) != null)
throw new CacheException(nodeInfo.toString()+"存在不同的缓存具有相同的层次!");
if(cachesOfOneNodeWithName.get(cache.getName()) != null)
throw new CacheException(nodeInfo.toString()+"存在不同的缓存具有相同的名字!");
cachesOfOneNodeWithLayer.put(cache.getLayer(), cache);
cachesOfOneNodeWithName.put(cache.getName(), cache);
}
public Cache getCacheByLayer(int layer){
return cachesOfOneNodeWithLayer.get(layer);
}
public Cache getCacheByName(String name){
return cachesOfOneNodeWithName.get(name);
}
public void invalidCacheByLayer(int layer){
Cache cache = cachesOfOneNodeWithLayer.get(layer);
if(null == cache)
throw new CacheException(nodeInfo.toString()+"不存在层数为["+layer+"]的缓存!");
removeCacheFromWorkList(cache);
addCacheToInvialidList(cache);
logger.debug(nodeInfo.toString()+"第["+layer+"]层缓存已失效!");
}
public void invalidCacheByName(String name){
Cache cache = cachesOfOneNodeWithName.get(name);
if(null == cache)
throw new CacheException(nodeInfo.toString()+"不存在名字为["+name+"]的缓存!");
removeCacheFromWorkList(cache);
addCacheToInvialidList(cache);
logger.debug(nodeInfo.toString()+"名字为["+name+"]的缓存已失效!");
}
public void recoverCacheByLayer(int layer){
Cache cache = invalidCacheofOneNodeWithLayer.get(layer);
if(null == cache)
throw new CacheException("在失效的缓存列表里面没有层数为["+layer+"]的缓存!");
addCacheToWorkList(cache);
removeCacheFormInvialidList(cache);
logger.debug(nodeInfo.toString()+"第["+layer+"]层缓存已重新失效!");
}
public void recoverCacheByName(String name){
Cache cache = invalidCacheOfOneNodeWithName.get(name);
if(null == cache)
throw new CacheException("在失效的缓存列表里面没有名字为["+name+"]的缓存!");
addCacheToWorkList(cache);
removeCacheFormInvialidList(cache);
logger.debug(nodeInfo.toString()+"名字为["+name+"]的缓存已重新失效!");
}
private void addCacheToWorkList(Cache cache){
if(cachesOfOneNodeWithLayer.get(cache.getLayer()) != null)
throw new CacheException("恢复的缓存层次数和现有的工作着的一个缓存相同!");
else if(cachesOfOneNodeWithName.get(cache.getName()) != null)
throw new CacheException("恢复的缓存层名字和现有的工作着的一个缓存相同!");
cachesOfOneNodeWithLayer.put(cache.getLayer(), cache);
cachesOfOneNodeWithName.put(cache.getName(), cache);
addCacheByLayerOrder(cache);
}
private void removeCacheFromWorkList(Cache cache){
cachesOfOneNodeWithLayer.remove(cache.getLayer());
cachesOfOneNodeWithName.remove(cache.getName());
cachesOrderByLayer.remove(cache);
}
private void addCacheToInvialidList(Cache cache){
invalidCacheofOneNodeWithLayer.put(cache.getLayer(),cache);
invalidCacheOfOneNodeWithName.put(cache.getName(),cache);
}
private void removeCacheFormInvialidList(Cache cache){
invalidCacheofOneNodeWithLayer.remove(cache.getLayer());
invalidCacheOfOneNodeWithName.remove(cache.getName());
}
private void addCacheByLayerOrder(Cache cache){
for(int i = 0; i < cachesOrderByLayer.size(); i++){
if(cache.getLayer() < cachesOrderByLayer.get(i).getLayer())
cachesOrderByLayer.add(i, cache);
}
}
}