📄 cmsflexcache.java
字号:
*
* Only users with administrator permissions are allowed
* to perform this operation.<p>
*
* @param cms the CmsObject used for user authorization
*/
private 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);
}
/**
* This method purges the JSP repository dirs,
* i.e. it deletes all JSP files that OpenCms has written to the
* real FS.<p>
*
* Obviously this method must be used with caution.
* Purpose of this method is to allow
* a complete purge of all JSP pages on a machine after
* a major update of JSP templates was made.<p>
*
* @param cms the CmsObject used for user authorization
*/
private synchronized void purgeJspRepository(CmsObject cms) {
if (!isAdmin(cms) && !cms.getRequestContext().isEventControlled()) return;
if (DEBUG > 0) System.err.println("FlexCache.purgeJspRepository() purging JSP repositories!");
File d;
d = new java.io.File(com.opencms.flex.CmsJspLoader.getJspRepository() + "online" + java.io.File.separator);
purgeDirectory(d);
d = new java.io.File(com.opencms.flex.CmsJspLoader.getJspRepository() + "offline" + java.io.File.separator);
purgeDirectory(d);
clear();
if (I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_OPENCMS_INFO))
A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_INFO, "JSP repository purged - purgeJspRepository() called");
}
/**
* Deletes a directory in the file system and all subfolders of the directory.<p>
*
* @param d the directory to delete
*/
private void purgeDirectory(File d) {
if (d.canRead() && d.isDirectory()) {
java.io.File files[] = d.listFiles();
if (DEBUG > 0) {
System.err.println("FlexCache.purgeDirectory() Deleting directory = " + d.getAbsolutePath());
System.err.println("FlexCache.purgeDirectory() Files in directory = " + files.length);
}
for (int i = 0; i<files.length; i++) {
File f = files[i];
if (f.isDirectory()) {
purgeDirectory(f);
}
if (f.canWrite()) {
f.delete();
} else if (DEBUG > 0) {
System.err.println("FlexCache.purgeDirectory() could not delete file = " + f);
}
}
} else if (DEBUG > 0) {
System.err.println("FlexCache.purgeDirectory() could not access directory: " + d);
System.err.println("FlexCache.purgeDirectory() d.canWrite() = " + d.canWrite());
System.err.println("FlexCache.purgeDirectory() d.canWrite() = " + d.canWrite());
System.err.println("FlexCache.purgeDirectory() d.isDirectory() = " + d.isDirectory());
}
}
/**
* 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.<p>
*
* @param cms the CmsObject used for user authorization
* @return a 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.<p>
*
* 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.<p>
*
* @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;
this.m_VariationCache.touch( (I_CmsFlexLruCacheObject)o );
return v.map.keySet();
}
return null;
}
/**
* Returns the CmsFlexCacheKey data structure for a given
* key (i.e. resource name).<p>
*
* 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.<p>
*
* @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
*/
public CmsFlexCacheKey getCachedKey(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;
this.m_VariationCache.touch( (I_CmsFlexLruCacheObject)o );
return v.key;
}
return null;
}
/**
* Returns the total number of entries in the cache.<p>
*
* @return the number of entries in the cache
*/
public int size() {
return this.m_EntryLruCache.size();
}
/**
* Returns the total number of cached resource keys.
*
* @return the number of resource keys in the cache
*/
public int keySize() {
if (! isEnabled()) return 0;
return m_resourceMap.size();
}
/**
* This method checks if a given key
* is already contained in the cache.<p>
*
* @return true if key is in the cache, false otherwise
* @param key the key to look for
*/
boolean containsKey(CmsFlexCacheKey key) {
if (! isEnabled()) return false;
return (get(key) != null);
}
/**
* Implements the CmsEvent interface,
* the FlexCache uses the events to clear itself in case a project is published.<p>
*
* @param event CmsEvent that has occurred
*/
public void cmsEvent(com.opencms.flex.CmsEvent event) {
if (! isEnabled()) return;
switch (event.getType()) {
case com.opencms.flex.I_CmsEventListener.EVENT_PUBLISH_PROJECT:
case com.opencms.flex.I_CmsEventListener.EVENT_CLEAR_CACHES:
if (DEBUG > 0) System.err.println("FlexCache: Recieved event, clearing cache!");
clear();
break;
case com.opencms.flex.I_CmsEventListener.EVENT_FLEX_PURGE_JSP_REPOSITORY:
if (DEBUG > 0) System.err.println("FlexCache: Recieved event, purging JSP repository!");
purgeJspRepository(event.getCmsObject());
break;
case com.opencms.flex.I_CmsEventListener.EVENT_FLEX_CACHE_CLEAR:
if (DEBUG > 0) System.err.println("FlexCache: Recieved event, clearing part of cache!");
java.util.Map m = event.getData();
if (m == null) break;
Integer it = null;
try {
it = (Integer)m.get("action");
} catch (Exception e) {}
if (it == null) break;
int i = it.intValue();
switch (i) {
case C_CLEAR_ALL:
clear(event.getCmsObject());
break;
case C_CLEAR_ENTRIES:
clearEntries(event.getCmsObject());
break;
case C_CLEAR_ONLINE_ALL:
clearOnline(event.getCmsObject());
break;
case C_CLEAR_ONLINE_ENTRIES:
clearOnlineEntries(event.getCmsObject());
break;
case C_CLEAR_OFFLINE_ALL:
clearOffline(event.getCmsObject());
break;
case C_CLEAR_OFFLINE_ENTRIES:
clearOfflineEntries(event.getCmsObject());
break;
}
}
}
/**
* Looks up a specific entry in the cache.<p>
*
* In case a found entry has a timeout set, it will be checked upon lookup.
* In case the timeout of the entry has been reached, it will be removed from
* the cache (and null will be returend in this case).<p>
*
* @param key The key to look for in the cache
* @return the entry found for the key, or null if key is not in the cache
*/
CmsFlexCacheEntry get(CmsFlexCacheKey key) {
if (! isEnabled()) return null;
if (DEBUG > 0) System.err.println("FlexCache: Trying to get entry for resource " + key.Resource);
Object o = m_resourceMap.get(key.Resource);
if (o != null) {
CmsFlexCacheVariation v = (CmsFlexCacheVariation)o;
String variation = v.key.matchRequestKey(key);
this.m_VariationCache.touch( (I_CmsFlexLruCacheObject)o );
if (DEBUG > 0) {
if (variation != null) {
CmsFlexCacheEntry e = (CmsFlexCacheEntry)v.map.get(variation);
if (e != null) {
System.err.println("FlexCache: Found entry for variation " + variation);
} else {
System.err.println("FlexCache: Did not find entry for variation " + variation);
}
} else {
System.err.println("FlexCache: Found nothing because resource is not cachable for this request!");
}
}
if (variation == null) return null;
if (v.key.m_timeout < 0) {
// No timeout for this resource is specified
return (CmsFlexCacheEntry)v.map.get(variation);
} else {
// Check for possible timeout of entry
CmsFlexCacheEntry e = (CmsFlexCacheEntry)v.map.get(variation);
if (e == null) return null;
if (DEBUG > 1) System.err.println("FlexCache: Checking timeout for resource " + key.Resource);
if (e.getTimeout() < key.m_timeout) {
if (DEBUG > 1) System.err.println("FlexCache: Resource has reached timeout, removing from cache!");
this.m_EntryLruCache.remove( (I_CmsFlexLruCacheObject)e );
return null;
}
if (DEBUG > 1) System.err.println("FlexCache: Resource timeout not reached!");
return e;
}
} else if (DEBUG > 0) {
System.err.println("FlexCache: Did not find any entry for resource" );
return null;
} else return null;
}
/**
* Returns the CmsFlexCacheKey data structure for a given resource name.<p>
*
* @param resource the resource name for which to look up the key for
* @return the CmsFlexCacheKey data structure found for the resource
*/
CmsFlexCacheKey getKey(String resource) {
if (! isEnabled()) return null;
Object o = m_resourceMap.get(resource);
if (o != null) {
if (DEBUG > 1) System.err.println("FlexCache: Found pre-calculated key for resource " + resource);
this.m_VariationCache.touch( (I_CmsFlexLruCacheObject)o );
return ((CmsFlexCacheVariation)o).key;
} else {
if (DEBUG > 1) System.err.println("FlexCache: Did not find pre-calculated key for resource " + resource);
return null;
}
}
/**
* Adds a key with a new, empty variation map to the cache.<p>
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -