📄 cmsflexcache.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/cache/Attic/CmsFlexCache.java,v $
* Date : $Date: 2003/03/31 16:49:19 $
* Version: $Revision: 1.17 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2002 - 2003 Alkacon Software (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, 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 com.opencms.flex.cache;
import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.file.CmsObject;
import com.opencms.flex.util.CmsFlexLruCache;
import com.opencms.flex.util.CmsLruHashMap;
import com.opencms.flex.util.I_CmsFlexLruCacheObject;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
/**
* 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>
*
* 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).<p>
*
* @author Alexander Kandzior (a.kandzior@alkacon.com)
* @author Thomas Weckert (t.weckert@alkacon.com)
*
* @version $Revision: 1.17 $
*
* @see com.opencms.flex.cache.CmsFlexCacheKey
* @see com.opencms.flex.cache.CmsFlexCacheEntry
* @see com.opencms.flex.util.CmsFlexLruCache
* @see com.opencms.flex.util.I_CmsFlexLruCacheObject
*/
public class CmsFlexCache extends java.lang.Object implements com.opencms.flex.I_CmsEventListener {
/** Initial Cache size, this should be a power of 2 because of the Java collections implementation */
public static final int C_INITIAL_CAPACITY_CACHE = 512;
// Some prime numbers: 127 257 509 1021 2039 4099 8191
/** Initial size for variation lists, should be a power of 2 */
public static final int C_INITIAL_CAPACITY_VARIATIONS = 8;
// Some prime numbers: 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 static final int DEBUG = 0;
/** Static ints to trigger clearcache events */
public static final int C_CLEAR_ALL = 0;
public static final int C_CLEAR_ENTRIES = 1;
public static final int C_CLEAR_ONLINE_ALL = 2;
public static final int C_CLEAR_ONLINE_ENTRIES = 3;
public static final int C_CLEAR_OFFLINE_ALL = 4;
public static final int C_CLEAR_OFFLINE_ENTRIES = 5;
/** The LRU cache to organize the cached entries. */
private CmsFlexLruCache m_EntryLruCache;
/** The LRU cache to organize the cached resources. */
private CmsFlexLruCache m_VariationCache;
/**
* 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.<p>
*
* @param openCms the OpenCms instance
*/
public CmsFlexCache( com.opencms.core.OpenCms openCms ) {
source.org.apache.java.util.Configurations opencmsProperties = openCms.getConfiguration();
m_enabled = opencmsProperties.getBoolean( "flex.cache.enabled", true );
m_cacheOffline = opencmsProperties.getBoolean( "flex.cache.offline", true );
boolean forceGC = opencmsProperties.getBoolean( "flex.cache.forceGC", false );
int maxCacheBytes = opencmsProperties.getInteger( "flex.cache.maxCacheBytes", 2000000 );
int avgCacheBytes = opencmsProperties.getInteger( "flex.cache.avgCacheBytes", 1500000 );
int maxEntryBytes = opencmsProperties.getInteger( "flex.cache.maxEntryBytes", 400000 );
int maxVariations = opencmsProperties.getInteger( "flex.cache.maxEntries", 4000 );
int maxKeys = opencmsProperties.getInteger( "flex.cache.maxKeys", 4000 );
this.m_EntryLruCache = new CmsFlexLruCache( maxCacheBytes, avgCacheBytes, maxEntryBytes, forceGC );
this.m_VariationCache = new CmsFlexLruCache( maxVariations, (int)(maxVariations*0.75), -1, false );
if (m_enabled) {
this.m_resourceMap = java.util.Collections.synchronizedMap( new CmsLruHashMap(CmsFlexCache.C_INITIAL_CAPACITY_CACHE,maxKeys) );
//this.m_resourceMap = java.util.Collections.synchronizedMap( new HashMap(CmsFlexCache.C_INITIAL_CAPACITY_CACHE) );
A_OpenCms.addCmsEventListener(this);
}
// make the flex cache available to other classes through the runtime properties
openCms.setRuntimeProperty( com.opencms.flex.I_CmsResourceLoader.C_LOADER_CACHENAME, this );
if (DEBUG > 0) System.err.println("FlexCache: Initializing with parameters enabled=" + m_enabled + " cacheOffline=" + m_cacheOffline);
}
/**
* Clears the cache for finalization.<p>
*/
protected void finalize() throws java.lang.Throwable {
this.clear();
this.m_EntryLruCache = null;
this.m_VariationCache = null;
this.m_resourceMap = null;
this.m_resourceMap = null;
super.finalize();
}
/**
* Indicates if the cache is enabled (i.e. actually
* caching entries) or not.<p>
*
* @return true if the cache is enabled, false if not
*/
public boolean isEnabled() {
return m_enabled;
}
/**
* Indicates if offline project resources are cached.<p>
*
* @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.<p>
*
* @param cms the CmsObject used for user authorization
*/
private 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.<p>
*
* @param cms the CmsObject used for user authorization
*/
private 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());
java.util.Iterator allEntries = v.map.values().iterator();
while (allEntries.hasNext()) {
I_CmsFlexLruCacheObject nextObject = (I_CmsFlexLruCacheObject)allEntries.next();
allEntries.remove();
this.m_EntryLruCache.remove( nextObject );
}
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.<p>
*
* @param cms the CmsObject used for user authorization
*/
private 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.<p>
*
* @param cms the CmsObject used for user authorization
*/
private 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.<p>
*
* @param cms the CmsObject used for user authorization
*/
private 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -