cmsflexcache.java
来自「java 编写的程序」· Java 代码 · 共 627 行 · 第 1/2 页
JAVA
627 行
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;
return v.key;
}
return null;
}
/**
* Returns the total number of entries in the cache.
*
* @return The number of entries in the cache
*/
public int size() {
return m_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.
*
* @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>
*
* <b>TODO:</b>
* Implement "partial" cache clearing, clear only items that have changed etc.
*
* @param event CmsEvent that has occurred
*/
public void cmsEvent(com.opencms.flex.CmsEvent event) {
if (! isEnabled()) return;
if (event.getType() == com.opencms.flex.I_CmsEventListener.EVENT_PUBLISH_PROJECT) {
if (DEBUG > 0) System.err.println("FlexCache: Got publish event, clearing cache!");
clear();
}
}
/**
* Lookup a specific entry in the cache.
* 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).
*
* @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);
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!");
v.map.remove(variation);
this.decSize();
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 structre for a given esource name.
*
* @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);
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.
*
* @param key The key to add to the cache.
*/
void putKey(CmsFlexCacheKey key) {
if (! isEnabled()) return;
Object o = m_resourceMap.get(key.Resource);
if (o == null) {
// No variation map for this resource yet, so create one
m_resourceMap.put(key.Resource, new CmsFlexCacheVariation(key));
if (DEBUG > 1) System.err.println("FlexCache: Added pre-calculated key for resource " + key.Resource);
}
// If != null the key is already in the cache, so we just do nothing
}
/**
* This method adds new entries to the cache.<p>
*
* The key describes the conditions under which the value can be cached.
* Usually the key belongs to the response.
* The variation describes the conditions under which the
* etry was created. This is usually calculated from the request.
* If the variation is != null, the entry is cachable.
*
* @param key The key for the new value entry. Usually calculated from the response.
* @param entry The CmsFlexCacheEntry to store in the cache.
* @param variation The pre-calculated variation for the entry.
* @return true if the value was added to the cache, false otherwise.
*/
boolean put(CmsFlexCacheKey key, CmsFlexCacheEntry entry, String variation) {
if (! isEnabled()) return false;
if (DEBUG > 1) System.err.println("FlexCache: Trying to add entry for resource " + key.Resource);
// String variation = key.matchRequestKey(compare_key);
if (variation != null) {
// This is a cachable result
key.Variation = variation;
if (DEBUG > 1) System.err.println("FlexCache: Adding entry for resource " + key.Resource + " with variation:" + key.Variation);
put(key, entry);
// Note that duplicates are NOT checked, it it assumed that this is done beforehand,
// while checking if the entry is already in the cache or not.
return true;
} else {
// Result is not cachable
if (DEBUG > 1) System.err.println("FlexCache: Nothing added because resource is not cachable for this request!");
return false;
}
}
/**
* Removes an entry from the cache.
*
* @param key The key which describes the entry to remove from the cache
*/
void remove(CmsFlexCacheKey key) {
if (! isEnabled()) return;
Object o = m_resourceMap.get(key.Resource);
if (o != null) {
Object old = ((HashMap)o).remove(key.Variation);
if (old != null) decSize();
};
}
/**
* Checks if the cache is empty or if at last one element is contained.
*
* @return true if the cache is empty, false otherwise
*/
boolean isEmpty() {
if (! isEnabled()) return true;
return m_resourceMap.isEmpty();
}
/**
* Emptys the cache completly.
*/
private synchronized void clear() {
if (! isEnabled()) return;
m_resourceMap = java.util.Collections.synchronizedMap(new HashMap(C_INITIAL_CAPACITY_CACHE));
m_size = 0;
}
/**
* Save a value to the cache.
*
* @param key The key under shich the value is saved.
* @param value The value to save in the cache.
*/
private void put(CmsFlexCacheKey key, CmsFlexCacheEntry value) {
Object o = m_resourceMap.get(key.Resource);
if (key.m_timeout > 0) value.setTimeout(key.m_timeout * 60000);
if (o != null) {
// We already have a variation map for this resource
java.util.Map m = ((CmsFlexCacheVariation)o).map;
if (! m.containsKey(key.Variation)) incSize();
m.put(key.Variation, value);
} else {
// No variation map for this resource yet, so create one
CmsFlexCacheVariation list = new CmsFlexCacheVariation(key);
list.map.put(key.Variation, value);
m_resourceMap.put(key.Resource, list);
incSize();
}
if (DEBUG > 0) System.err.println("FlexCache: Entry " + m_size + " added for resource " + key.Resource + " with variation " + key.Variation);
}
/**
* This assures increasing the size is done synchronized.
*/
private synchronized void incSize() {
m_size++;
}
/**
* This assures decreasing the size is done synchronized
*/
private synchronized void decSize() {
m_size--;
}
/**
* Internal method to determine if a user has Administration permissions.
*/
private boolean isAdmin(CmsObject cms) {
boolean result;
try {
result = cms.getRequestContext().isAdmin();
} catch (Exception e) {
result = false;
}
return result;
}
/**
* Internal method to perform cache clearance.
* It clears "one half" of the cache, i.e. either
* the online or the offline parts.
* Another parameter is used to indicate if only
* the entries or keys and entries are to be cleared.
*/
private synchronized void clearOneHalf(String suffix, boolean entriesOnly) {
java.util.Set keys = new java.util.HashSet(m_resourceMap.keySet());
java.util.Iterator i = keys.iterator();
while (i.hasNext()) {
String s = (String)i.next();
if (s.endsWith(suffix)) {
CmsFlexCacheVariation v = (CmsFlexCacheVariation)m_resourceMap.get(s);
if (entriesOnly) {
// Clear only entry
m_size -= v.map.size();
v.map = java.util.Collections.synchronizedMap(new HashMap(C_INITIAL_CAPACITY_VARIATIONS));
} else {
// Clear key and entry
m_size -= v.map.size();
v.map = null;
v.key = null;
m_resourceMap.remove(s);
}
}
}
}
/**
* Internal "quick and dirty" data structure to poupate the cache with.
*/
private class CmsFlexCacheVariation {
/** The key belonging to the resource */
public CmsFlexCacheKey key;
/** Maps variations to CmsFlexCacheEntries */
public java.util.Map map;
/**
* Generates a new instance of CmsFlexCacheVariation
*
* @param k The (resource) key to contruct this variation list for
*/
public CmsFlexCacheVariation(CmsFlexCacheKey k) {
key = k;
map = java.util.Collections.synchronizedMap(new HashMap(C_INITIAL_CAPACITY_VARIATIONS));
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?