📄 lrumemorystore.java
字号:
package com.easyjf.cache.store;//~--- non-JDK imports --------------------------------------------------------import com.easyjf.cache.*;import org.apache.log4j.Logger;//~--- JDK imports ------------------------------------------------------------import java.util.*;//~--- classes ----------------------------------------------------------------/** * * <p> * Title: * </p> * * <p> * Description: * </p> * * <p> * Copyright: Copyright (c) 2006 * </p> * * <p> * Company: * </p> * * @author not attributable * @version 1.0 */public class LruMemoryStore extends MemoryStore { private final static Logger logger = Logger.getLogger(LruMemoryStore.class); // ~--- constructors ------------------------------------------------------- /** * * @param cache * ICache */ public LruMemoryStore(ICache cache) { super(cache); try { map = loadMapInstance(); } catch (CacheException e) { logger.error(cache.getName() + "Cache: Cannot start LruMemoryStore", e); } } // ~--- methods ------------------------------------------------------------ /** * * @return Map * @throws CacheException */ public Map loadMapInstance() throws CacheException { // First try to load java.util.LinkedHashMap, which is preferred, but // only if not overriden if (System.getProperty("net.sf.ehcache.useLRUMap") == null) { try { Class.forName("java.util.LinkedHashMap"); Map candidateMap = new SpoolingLinkedHashMap(); logger.debug(cache.getName()+ " Cache: Using SpoolingLinkedHashMap implementation"); return candidateMap; } catch (Exception e) { logger.debug(cache.getName()+ " Cache: Cannot find java.util.LinkedHashMap"); } } // Secondly, try and load org.apache.commons.collections.LRUMap try { Class.forName("org.apache.commons.collections.LRUMap"); Map candidateMap = new SpoolingLRUMap(); logger.debug(cache.getName() + " Cache: Using SpoolingLRUMap implementation"); return candidateMap; } catch (Exception e) { // Give up throw new CacheException( cache.getName() + "Cache: Cannot find org.apache.commons.collections.LRUMap."); } } // ~--- inner classes ------------------------------------------------------ /** * * <p> * Title: * </p> * * <p> * Description: * </p> * * <p> * Copyright: Copyright (c) 2006 * </p> * * <p> * Company: * </p> * * @author not attributable * @version 1.0 */ public class SpoolingLRUMap extends org.apache.commons.collections.LRUMap { /** * Constructor. The maximum size is set to * {@link Cache#getMaxElementsInMemory}. If the LRUMap gets bigger than * this, {@link #processRemovedLRU} is called. */ public SpoolingLRUMap() { setMaximumSize(cache.getMaxElemnetCount()); } // ~--- methods -------------------------------------------------------- /** * * @param key * Object * @param value * Object */ protected void processRemovedLRU(Object key, Object value) { // Already removed from the map at this point Element element = (Element) value; // When max size is 0 if (element == null) { return; } } } /** * * <p> * Title: * </p> * * <p> * Description: * </p> * * <p> * Copyright: Copyright (c) 2006 * </p> * * <p> * Company: * </p> * * @author not attributable * @version 1.0 */ public class SpoolingLinkedHashMap extends java.util.LinkedHashMap { private static final int INITIAL_CAPACITY = 100; private static final float GROWTH_FACTOR = .75F; /** * */ public SpoolingLinkedHashMap() { super(INITIAL_CAPACITY, GROWTH_FACTOR, true); } /** * * @param eldest * Entry * @return boolean */ protected boolean removeEldestEntry(Map.Entry eldest) { Element element = (Element) eldest.getValue(); return removeLeastRecentlyUsedElement(element); } /** * * @param element * Element * @return boolean */ private boolean removeLeastRecentlyUsedElement(Element element) { if (cache.isExpired(element)) { return true; } if (isFull()) { return true; } else { return false; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -