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

📄 lrumemorystore.java

📁 EasyDBO v0.4 是一个非常适合中小型软件数据库开发的数据持久层框架
💻 JAVA
字号:
/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 - 2004 Greg Luck.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, if *    any, must include the following acknowlegement: *       "This product includes software developed by Greg Luck *       (http://sourceforge.net/users/gregluck) and contributors. *       See http://sourceforge.net/project/memberlist.php?group_id=93232 *       for a list of contributors" *    Alternately, this acknowledgement may appear in the software itself, *    if and wherever such third-party acknowlegements normally appear. * * 4. The names "EHCache" must not be used to endorse or promote products *    derived from this software without prior written permission. For written *    permission, please contact Greg Luck (gregluck at users.sourceforge.net). * * 5. Products derived from this software may not be called "EHCache" *    nor may "EHCache" appear in their names without prior written *    permission of Greg Luck. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL GREG LUCK OR OTHER * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by contributors * individuals on behalf of the EHCache project.  For more * information on EHCache, please see <http://ehcache.sourceforge.net/>. * */package com.easyjf.cache.store;/** * 这里直接使用的Apache开源缓存ECache中的算法 */import com.easyjf.cache.*;import org.apache.log4j.Logger;import java.util.Map;/** * An implementation of a LruMemoryStore. * <p/> * This uses {@link java.util.LinkedHashMap} as its backing map. It uses the {@link java.util.LinkedHashMap} LRU * feature. LRU for this implementation means least recently accessed. * * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a> * @version $Id: LruMemoryStore.java,v 1.8 2005/10/14 02:12:58 gregluck Exp $ */public class LruMemoryStore extends MemoryStore {	private final static Logger logger = Logger.getLogger(LruMemoryStore.class);    /**     * Constructor for the LruMemoryStore object     * The backing {@link java.util.LinkedHashMap} is created with LRU by access order.     */    public LruMemoryStore(ICache cache) {        super(cache);        try {            map = loadMapInstance();        } catch (CacheException e) {            logger.error(cache.getName() + "Cache: Cannot start LruMemoryStore", e);        }    }    /**     * Tries to load a {@link java.util.LinkedHashMap} (JDK1.4) and then     * tries to load an {@link org.apache.commons.collections.LRUMap}.     * <p/>     * This way applications running JDK1.4 do not have a dependency     * on Apache commons-collections.     *     * @return a Map, being either {@link java.util.LinkedHashMap} or     */    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.");        }    }    /**     * An LRU Map implementation based on Apache Commons LRUMap.     * <p/>     * This is used if {@link java.util.LinkedHashMap} is not found in the classpath.     * LinkedHashMap is part of JDK     */    public class SpoolingLRUMap extends org.apache.commons.collections.LRUMap {    	private static final long serialVersionUID=1994584985l;        /**         * 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());        }        /**         * Called after the element has been removed.         * <p/>         * Our choices are to do nothing or spool the element to disk.         * <p/>         * Note that value will be null when the memory size is set to 0. Thus a null guard is used.         *         * @param key         * @param value         */        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;            }        }    }    /**     * An extension of LinkedHashMap which overrides {@link #removeEldestEntry}     * to persist cache entries to the auxiliary cache before they are removed.     * <p/>     * This implementation also provides LRU by access order.     */    public class SpoolingLinkedHashMap extends java.util.LinkedHashMap {    	private static final long serialVersionUID=84758345l;        private static final int INITIAL_CAPACITY = 100;        private static final float GROWTH_FACTOR = .75F;        /**         * Default constructor.         * Will create an initial capacity of 100, a loading of .75 and         * LRU by access order.         */        public SpoolingLinkedHashMap() {            super(INITIAL_CAPACITY, GROWTH_FACTOR, true);        }        /**         * Returns <tt>true</tt> if this map should remove its eldest entry.         * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after         * inserting a new entry into the map.  It provides the implementer         * with the opportunity to remove the eldest entry each time a new one         * is added.  This is useful if the map represents a cache: it allows         * the map to reduce memory consumption by deleting stale entries.         * <p/>         * Will return true if:         * <ol>         * <li> the element has expired         * <li> the cache size is greater than the in-memory actual.         * In this case we spool to disk before returning.         * </ol>         *         * @param eldest The least recently inserted entry in the map, or if         *               this is an access-ordered map, the least recently accessed         *               entry.  This is the entry that will be removed it this         *               method returns <tt>true</tt>.  If the map was empty prior         *               to the <tt>put</tt> or <tt>putAll</tt> invocation resulting         *               in this invocation, this will be the entry that was just         *               inserted; in other words, if the map contains a single         *               entry, the eldest entry is also the newest.         * @return true if the eldest entry should be removed         *         from the map; <tt>false</t> if it should be retained.         */        protected boolean removeEldestEntry(Map.Entry eldest) {            Element element = (Element) eldest.getValue();            return removeLeastRecentlyUsedElement(element);        }        /**         * Relies on being called from a synchronized method         *         * @param element         * @return true if the LRU element should be removed         */        private boolean removeLeastRecentlyUsedElement(Element element) {            //check for expiry and remove before going to the trouble of spooling it            if (cache.isExpired(element)) {                              return true;            }            if (isFull()) {                             return true;            } else {                return false;            }        }    }}

⌨️ 快捷键说明

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