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

📄 cmslrucache.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/cache/CmsLruCache.java,v $
 * Date   : $Date: 2006/03/27 14:52:27 $
 * Version: $Revision: 1.20 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.cache;

import org.opencms.main.CmsLog;

import org.apache.commons.logging.Log;

/**
 * Implements an LRU (last recently used) cache.<p>
 * 
 * The idea of this cache is to separate the caching policy from the data structure
 * where the cached objects are stored. The advantage of doing so is, that the CmsFlexLruCache
 * can identify the last-recently-used object in O(1), whereas you would need at least
 * O(n) to traverse the data structure that stores the cached objects. Second, you can
 * easily use the CmsFlexLruCache to get an LRU cache, no matter what data structure is used to
 * store your objects.
 * <p>
 * The cache policy is affected by the "costs" of the objects being cached. Valuable cache costs
 * might be the byte size of the cached objects for example.
 * <p>
 * To add/remove cached objects from the data structure that stores them, the objects have to
 * implement the methods defined in the interface I_CmsLruCacheObject to be notified when they
 * are added/removed from the CmsFlexLruCache.<p>
 *
 * @see org.opencms.cache.I_CmsLruCacheObject
 * 
 * @author Thomas Weckert 
 * 
 * @version $Revision: 1.20 $
 * 
 * @since 6.0.0
 */
public class CmsLruCache extends java.lang.Object {

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsLruCache.class);

    /** The avg. sum of costs the cached objects. */
    private int m_avgCacheCosts;

    /** The head of the list of double linked LRU cache objects. */
    private I_CmsLruCacheObject m_listHead;

    /** The tail of the list of double linked LRU cache objects. */
    private I_CmsLruCacheObject m_listTail;

    /** The max. sum of costs the cached objects might reach. */
    private int m_maxCacheCosts;

    /** The max. costs of cacheable objects. */
    private int m_maxObjectCosts;

    /** The costs of all cached objects. */
    private int m_objectCosts;

    /** The sum of all cached objects. */
    private int m_objectCount;

    /**
     * The constructor with all options.<p>
     *
     * @param theMaxCacheCosts the max. cache costs of all cached objects
     * @param theAvgCacheCosts the avg. cache costs of all cached objects
     * @param theMaxObjectCosts the max. allowed cache costs per object. Set theMaxObjectCosts to -1 if you don't want to limit the max. allowed cache costs per object
     */
    public CmsLruCache(int theMaxCacheCosts, int theAvgCacheCosts, int theMaxObjectCosts) {

        m_maxCacheCosts = theMaxCacheCosts;
        m_avgCacheCosts = theAvgCacheCosts;
        m_maxObjectCosts = theMaxObjectCosts;

        m_objectCosts = 0;
        m_objectCount = 0;
        m_listHead = null;
        m_listTail = null;
    }

    /**
     * Adds a new object to this cache.<p>
     * 
     * If add the same object more than once,
     * the object is touched instead.<p>
     *
     * @param theCacheObject the object being added to the cache
     * @return true if the object was added to the cache, false if the object was denied because its cache costs were higher than the allowed max. cache costs per object
     */
    public synchronized boolean add(I_CmsLruCacheObject theCacheObject) {

        if (theCacheObject == null) {
            // null can't be added or touched in the cache 
            return false;
        }

        // only objects with cache costs < the max. allowed object cache costs can be cached!
        if ((m_maxObjectCosts != -1) && (theCacheObject.getLruCacheCosts() > m_maxObjectCosts)) {
            if (LOG.isInfoEnabled()) {
                LOG.info(Messages.get().getBundle().key(
                    Messages.LOG_CACHE_COSTS_TOO_HIGH_2,
                    new Integer(theCacheObject.getLruCacheCosts()),
                    new Integer(m_maxObjectCosts)));
            }
            return false;
        }

        if (!isCached(theCacheObject)) {
            // add the object to the list of all cached objects in the cache
            addHead(theCacheObject);
        } else {
            touch(theCacheObject);
        }

        // check if the cache has to trash the last-recently-used objects before adding a new object
        if (m_objectCosts > m_maxCacheCosts) {
            gc();
        }

        return true;
    }

    /**
     * Removes all cached objects in this cache.<p>
     */
    public void clear() {

        // remove all objects from the linked list from the tail to the head:
        I_CmsLruCacheObject currentObject = m_listTail;
        while (currentObject != null) {
            currentObject = currentObject.getNextLruObject();
            removeTail();
        }

        // reset the data structure
        m_objectCosts = 0;
        m_objectCount = 0;
        m_listHead = null;
        m_listTail = null;

    }

    /**
     * Returns the average costs of all cached objects.<p>
     * 
     * @return the average costs of all cached objects
     */
    public int getAvgCacheCosts() {

        return m_avgCacheCosts;
    }

    /**
     * Returns the max costs of all cached objects.<p>
     * 
     * @return the max costs of all cached objects
     */
    public int getMaxCacheCosts() {

        return m_maxCacheCosts;
    }

    /**
     * Returns the max allowed costs per cached object.<p>
     * 
     * @return the max allowed costs per cached object
     */
    public int getMaxObjectCosts() {

        return m_maxObjectCosts;
    }

    /**
     * Returns the current costs of all cached objects.<p>
     * 
     * @return the current costs of all cached objects
     */
    public int getObjectCosts() {

        return m_objectCosts;
    }

    /**
     * Removes an object from the list of all cached objects in this cache,
     * no matter what position it has inside the list.<p>
     *
     * @param theCacheObject the object being removed from the list of all cached objects
     * @return a reference to the object that was removed
     */
    public synchronized I_CmsLruCacheObject remove(I_CmsLruCacheObject theCacheObject) {

        if (!isCached(theCacheObject)) {
            // theCacheObject is null or not inside the cache
            return null;
        }

        // set the list pointers correct
        if (theCacheObject.getNextLruObject() == null) {
            // remove the object from the head pos.
            I_CmsLruCacheObject newHead = theCacheObject.getPreviousLruObject();

            if (newHead != null) {
                // if newHead is null, theCacheObject 
                // was the only object in the cache
                newHead.setNextLruObject(null);
            }

            m_listHead = newHead;
        } else if (theCacheObject.getPreviousLruObject() == null) {
            // remove the object from the tail pos.
            I_CmsLruCacheObject newTail = theCacheObject.getNextLruObject();

            if (newTail != null) {
                // if newTail is null, theCacheObject 
                // was the only object in the cache                
                newTail.setPreviousLruObject(null);
            }

            m_listTail = newTail;
        } else {

⌨️ 快捷键说明

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