⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memorystore.java

📁 EasyDBO v0.4 是一个非常适合中小型软件数据库开发的数据持久层框架
💻 JAVA
字号:
package com.easyjf.cache.store;

import com.easyjf.cache.*;

import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.Serializable;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public abstract class MemoryStore implements Store {
	private final static Logger logger = Logger.getLogger(MemoryStore.class); 
    protected ICache cache;    
    protected Map map;   
    protected int status;
    protected MemoryStore(ICache cache) {
        status = Status.STATUS_UNINITIALISED;
        this.cache = cache;     
        status = Status.STATUS_ALIVE;
    }
    public static MemoryStore create(ICache cache) {
        MemoryStore memoryStore = null;     
        switch(cache.getStorePolicy())
        {
        case(StorePolicy.LRU):
        	memoryStore=new LruMemoryStore(cache);
        	break;
        case(StorePolicy.FIFO):
        	memoryStore=new FifoMemoryStore(cache);
        	break;
        case (StorePolicy.LFU):   
        	memoryStore=new LfuMemoryStore(cache);
        }      
        return memoryStore;
    }

    public synchronized void put(Element element) {
        if (element != null) {        	
            map.put(element.getKey(), element);
            doPut(element);
        }
    }
    protected void doPut(Element element) {      
    }

    public synchronized Element get(Object key) {
        Element element = (Element) map.get(key);
        if (element != null) {
            element.updateAccessStatistics();
            doGetOnFoundElement(element);            
                logger.info(cache.getName() + "Cache: " + cache.getName() + "MemoryStore中找到" + key);
           
        } else logger.info(cache.getName() + "Cache: " + cache.getName() + "MemoryStore找不到" + key);
        
        return element;
    }

   
    protected void doGetOnFoundElement(Element element) {
        //nothing for this class
    }

    
    public synchronized Element getQuiet(Serializable key) {
        Element cacheElement = (Element) map.get(key);
        if (cacheElement != null) {                      
               logger.info(cache.getName() + "Cache: " + cache.getName() + "MemoryStore找到" + key);
        
        } else
              logger.info(cache.getName() + "Cache: " + cache.getName() + "MemoryStore找不到" + key);        
        return cacheElement;
    }

    public synchronized Element remove(Object key) {
        // remove single item.
        Element element = (Element) map.remove(key);
        if (element != null) {
            return element;
        } else {          
               logger.debug(cache.getName() + "Cache:缓存中没有" + key + ",删除失败。");         
            return null;
        }
    }


    public synchronized void removeAll() {       
        clear();
    }

 
    protected void clear() {
        map.clear();
    }


    public synchronized void dispose() {
        if (status==Status.STATUS_SHUTDOWN) {
            return;
        }
        status = Status.STATUS_SHUTDOWN;
        //release reference to cache
        cache = null;
    }

  
    public int getStatus() {
        return status;
    }


    public synchronized Object[] getKeyArray() {
        return map.keySet().toArray();
    }


    public int getSize() {
        return map.size();
    }


    public boolean containsKey(Object key) {
        return map.containsKey(key);
    }


    public synchronized long getSizeInBytes() throws CacheException {
        long sizeInBytes = 0;
        for (Iterator iterator = map.values().iterator(); iterator.hasNext();) {
            Element element = (Element) iterator.next();
            if (element != null) {
                sizeInBytes += element.getSerializedSize();
            }
        }
        return sizeInBytes;
    }

    protected boolean isFull() {
        return map.size() > cache.getMaxElemnetCount();
    }
	public Set getKeySet() {
		// TODO Auto-generated method stub
		return cache.getKeySet();
	}
	public boolean removeElement(Object value) throws IOException {
		// TODO Auto-generated method stub
		return map.values().remove(value);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -