cmsflexcache.java

来自「java 编写的程序」· Java 代码 · 共 627 行 · 第 1/2 页

JAVA
627
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/cache/Attic/CmsFlexCache.java,v $
 * Date   : $Date: 2002/05/10 21:07:31 $
 * Version: $Revision: 1.1.2.1.2.1 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2002  The OpenCms Group
 *
 * 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 OpenCms, please see the
 * OpenCms 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
 *
 * First created on 9. April 2002, 12:40
 */


package com.opencms.flex.cache;

import java.util.HashMap;
import com.opencms.file.CmsObject;

/** 
 * This class implements the FlexCache.<p>
 *
 * The data structure used is a two-level hashtable.
 * This is optimized for the structure of the keys that are used to describe the
 * caching behaviour of the entries.
 * The first hash-level is calculated from the resource name, i.e. the
 * name of the resource as it is referred to in the VFS of OpenCms.
 * A suffix [online] or [offline] is appended to te resource name 
 * to distinguish between the online and offline projects of OpenCms.
 * The second hash-level is calculated from the cache-key of the resource,
 * which also is a String representing the specifc variation of the cached entry.<p>
 *
 * Entries in the first level of the cache are of type CmsFlexCacheVariation, 
 * which is a sub-class of CmsFlexCache. 
 * This class is a simple data type that contains of a Map of CmsFlexCacheEntries, 
 * with variations - Strings as keys.<p>
 *
 * Here's a short summary of used terms:
 * <ul>
 * <li><b>key:</b> 
 * A combination of a resource name and a variation.
 * The data structure used is CmsFlexCacheKey.
 * <li><b>resource:</b>
 * A String with the resource name and an appended [online] of [offline] suffix.
 * <li><b>variation:</b>
 * A String describing a variation of a cached entry in the CmsFlexCache language.
 * <li><b>entry:</b>
 * A CmsFlexCacheEntry data structure which is describes a cached OpenCms resource.
 * For every entry a key is saved which contains the resource name and the variation.
 * </ul>
 *
 * <b>TODO:</b> 
 * Implement cache aging, so that old entries are
 * removed if cache size grows to big. 
 * Alternative: Swap to disk if memory runs short, should be more efficient still 
 * then asking the database and re-calculating again.<br>
 * <b>TODO:</b> 
 * Currenty the whole cache is flushed if something is published.
 * Implement partial cache flushing, i.e. remove only changed elements at publish 
 * or change event (in case of offline resources).<br>
 *
 * @author Alexander Kandzior (a.kandzior@alkacon.com)
 * @version $Revision: 1.1.2.1.2.1 $
 * @see com.opencms.flex.cache.CmsFlexCacheKey
 * @see com.opencms.flex.cache.CmsFlexCacheEntry
 */
public class CmsFlexCache implements com.opencms.flex.I_CmsEventListener {
    
    /** Initial Cache size, this should be a prime number of best results in the hash algorithms */
    public static final int C_INITIAL_CAPACITY_CACHE = 509;
    // Alternatives: 127 257 509 1021 2039 4099 8191
    // TODO: Check is this should be a constructor variable, 
    // maybe related to the number of resources in the site.
    
    /** Initial size for variation lists */
    public static final int C_INITIAL_CAPACITY_VARIATIONS = 7;
    // Alternatives: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
    
    /** Suffix to append to online cache entries */
    public static String C_CACHE_ONLINESUFFIX = " [online]";
    
    /** Suffix to append to online cache entries */
    public static String C_CACHE_OFFLINESUFFIX = " [offline]";
        
    /** Hashmap to store the Entries for fast lookup */
    private java.util.Map m_resourceMap;
    
    /** Counter for the size */
    private int m_size;
    
    /** Indicates if the cache is enabled or not */
    private boolean m_enabled;
    
    /** Indicates if offline resources should be cached or not */
    private boolean m_cacheOffline;
    
    /** Debug switch */
    private int DEBUG = 0;

    /**
     * Constructor for class CmsFlexCache.<p>
     *
     * The parameter "enabled" is used to control if the cache is
     * actually on or off. Even if you don't need the cache, you still
     * have to create an instance of it with enabled=false.
     * This is because you need some of the FlexCache data structures
     * for JSP inclusion buffering.
     *
     * @param enabled Indicates if the cache is enabled or not
     * @param cacheOffline Indicates if offline resources should be cached or not
     */
    public CmsFlexCache(boolean enabled, boolean cacheOffline) {
        // TODO: Implement cache - aging & cleanup
        m_enabled = enabled;
        m_cacheOffline = cacheOffline;
        if (m_enabled) {
            clear();
            com.opencms.core.A_OpenCms.addCmsEventListener(this);
        }
        if (DEBUG > 0) System.err.println("FlexCache: Initializing with parameters enabled=" + m_enabled + " cacheOffline=" + m_cacheOffline);
    }
    
    /**
     * Indicates if the cache is enabled (i.e. actually
     * caching entries) or not.
     *
     * @return true if the cache is enabled, false if not
     */    
    public boolean isEnabled() {
        return m_enabled;
    }
    
    /**
     * Indicates if offline project resources are cached.
     *
     * @return true if offline projects are cached, false if not
     */    
    public boolean cacheOffline() {
        return m_cacheOffline;
    }
    
    /**
     * Clears all entries and all keys in the cache, online or offline.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param cms The CmsObject used for user authorization
     */    
    public void clear(CmsObject cms) {
        if (! isEnabled()) return;
        if (! isAdmin(cms)) return;
        if (DEBUG > 0) System.err.println("FlexCache: Clearing complete cache");        
        clear();
    }
    
    /**
     * Clears all entries in the cache, online or offline.
     * The keys are not cleared.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param cms The CmsObject used for user authorization
     */    
    public synchronized void clearEntries(CmsObject cms) {
        if (! isEnabled()) return;
        if (! isAdmin(cms)) return;
        if (DEBUG > 0) System.err.println("FlexCache: Clearing all entries");        
        java.util.Iterator i = m_resourceMap.keySet().iterator();
        while (i.hasNext()) {
             CmsFlexCacheVariation v = (CmsFlexCacheVariation)m_resourceMap.get(i.next());
             v.map = java.util.Collections.synchronizedMap(new HashMap(C_INITIAL_CAPACITY_VARIATIONS));             
        }
        m_size = 0;
    }
    
    /**
     * Clears all entries and all keys from offline projects in the cache.
     * Cached resources from the online project are not touched.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param cms The CmsObject used for user authorization
     */        
    public void clearOffline(CmsObject cms) {
        if (! isEnabled()) return;
        if (! isAdmin(cms)) return;
        if (DEBUG > 0) System.err.println("FlexCache: Clearing offline keys & entries");        
        clearOneHalf(C_CACHE_OFFLINESUFFIX, false);
    }    
    
    /**
     * Clears all entries from offline projects in the cache.
     * The keys from the offline projects are not cleared.
     * Cached resources from the online project are not touched.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param cms The CmsObject used for user authorization
     */        
    public void clearOfflineEntries(CmsObject cms) {
        if (! isEnabled()) return;
        if (! isAdmin(cms)) return;
        if (DEBUG > 0) System.err.println("FlexCache: Clearing offline entries");        
        clearOneHalf(C_CACHE_OFFLINESUFFIX, true);
    }    
        
    /**
     * Clears all entries and all keys from the online project in the cache.
     * Cached resources from the offline projects are not touched.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param cms The CmsObject used for user authorization
     */     
    public void clearOnline(CmsObject cms) {
        if (! isEnabled()) return;
        if (! isAdmin(cms)) return;
        if (DEBUG > 0) System.err.println("FlexCache: Clearing online keys & entries");        
        clearOneHalf(C_CACHE_ONLINESUFFIX, false);
    }
    
    /**
     * Clears all entries from the online project in the cache.
     * The keys from the online project are not cleared.
     * Cached resources from the offline projects are not touched.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param cms The CmsObject used for user authorization
     */
    public void clearOnlineEntries(CmsObject cms) {
        if (! isEnabled()) return;
        if (! isAdmin(cms)) return;
        if (DEBUG > 0) System.err.println("FlexCache: Clearing online entries");        
        clearOneHalf(C_CACHE_ONLINESUFFIX, true);
    }
    
    /** 
     * Returns a set of all cached resource names.
     * Usefull if you want to show a list of all cached resources,
     * like on the FlexCache administration page.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param cms The CmsObject used for user authorization
     * @return The set of cached resource names (which are of type String)
     */    
    public java.util.Set getCachedResources(CmsObject cms) {
        if (! isEnabled()) return null;
        if (! isAdmin(cms)) return null;
        return m_resourceMap.keySet();
    }
    
    /**
     * Returns all variations in the cache for a given resource name.
     * The variations are of type String.
     * Usefull if you want to show a list of all cached entry - variations,
     * like on the FlexCache administration page.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param key The resource name for which to look up the variations for
     * @param cms The CmsObject used for user authorization
     * @return A set of cached variations (which are of type String)   
     */    
    public java.util.Set getCachedVariations(String key, CmsObject cms) {
        if (! isEnabled()) return null;        
        if (! isAdmin(cms)) return null;
        Object o = m_resourceMap.get(key);
        if (o != null) {
            CmsFlexCacheVariation v = (CmsFlexCacheVariation)o;
            return v.map.keySet();
        }
        return null;
    }
    
    /**
     * Returns the CmsFlexCacheKey data structre for a given
     * key (i.e. resource name).
     * Usefull if you want to show the cache key for a resources,
     * like on the FlexCache administration page.<p>
     *
     * Only users with administrator permissions are allowed
     * to perform this operation.
     *
     * @param key The resource name for which to look up the variation for
     * @param cms The CmsObject used for user authorization
     * @return The CmsFlexCacheKey data structure found for the resource
     */    

⌨️ 快捷键说明

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