Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.opencms.flex.cache
Class CmsFlexCache  view CmsFlexCache download CmsFlexCache.java

java.lang.Object
  extended bycom.opencms.flex.cache.CmsFlexCache
All Implemented Interfaces:
com.opencms.flex.I_CmsEventListener

public class CmsFlexCache
extends java.lang.Object
implements com.opencms.flex.I_CmsEventListener

This class implements the FlexCache.

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.

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.

Here's a short summary of used terms:

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).

Version:
$Revision: 1.17 $

Nested Class Summary
(package private)  class CmsFlexCache.CmsFlexCacheVariation
          A simple data container class for the FlexCache variations.
 
Field Summary
static java.lang.String C_CACHE_OFFLINESUFFIX
          Suffix to append to online cache entries
static java.lang.String C_CACHE_ONLINESUFFIX
          Suffix to append to online cache entries
static int C_CLEAR_ALL
          Static ints to trigger clearcache events
static int C_CLEAR_ENTRIES
           
static int C_CLEAR_OFFLINE_ALL
           
static int C_CLEAR_OFFLINE_ENTRIES
           
static int C_CLEAR_ONLINE_ALL
           
static int C_CLEAR_ONLINE_ENTRIES
           
static int C_INITIAL_CAPACITY_CACHE
          Initial Cache size, this should be a power of 2 because of the Java collections implementation
static int C_INITIAL_CAPACITY_VARIATIONS
          Initial size for variation lists, should be a power of 2
private static int DEBUG
          Debug switch
private  boolean m_cacheOffline
          Indicates if offline resources should be cached or not
private  boolean m_enabled
          Indicates if the cache is enabled or not
private  com.opencms.flex.util.CmsFlexLruCache m_EntryLruCache
          The LRU cache to organize the cached entries.
private  java.util.Map m_resourceMap
          Hashmap to store the Entries for fast lookup
private  int m_size
          Counter for the size
private  com.opencms.flex.util.CmsFlexLruCache m_VariationCache
          The LRU cache to organize the cached resources.
 
Fields inherited from interface com.opencms.flex.I_CmsEventListener
EVENT_CLEAR_CACHES, EVENT_FLEX_CACHE_CLEAR, EVENT_FLEX_CLUSTER_CHECK_SOURCE, EVENT_FLEX_CLUSTER_HOOK, EVENT_FLEX_PURGE_JSP_REPOSITORY, EVENT_LOGIN_USER, EVENT_PUBLISH_BO_RESOURCE, EVENT_PUBLISH_PROJECT, EVENT_PUBLISH_RESOURCE, EVENT_STATIC_EXPORT
 
Constructor Summary
CmsFlexCache(com.opencms.core.OpenCms openCms)
          Constructor for class CmsFlexCache.
 
Method Summary
 boolean cacheOffline()
          Indicates if offline project resources are cached.
private  void clear()
          Emptys the cache completely.
private  void clear(com.opencms.file.CmsObject cms)
          Clears all entries and all keys in the cache, online or offline.
private  void clearEntries(com.opencms.file.CmsObject cms)
          Clears all entries in the cache, online or offline.
private  void clearOffline(com.opencms.file.CmsObject cms)
          Clears all entries and all keys from offline projects in the cache.
private  void clearOfflineEntries(com.opencms.file.CmsObject cms)
          Clears all entries from offline projects in the cache.
private  void clearOneHalf(java.lang.String suffix, boolean entriesOnly)
          Internal method to perform cache clearance.
private  void clearOnline(com.opencms.file.CmsObject cms)
          Clears all entries and all keys from the online project in the cache.
private  void clearOnlineEntries(com.opencms.file.CmsObject cms)
          Clears all entries from the online project in the cache.
 void cmsEvent(com.opencms.flex.CmsEvent event)
          Implements the CmsEvent interface, the FlexCache uses the events to clear itself in case a project is published.
(package private)  boolean containsKey(CmsFlexCacheKey key)
          This method checks if a given key is already contained in the cache.
protected  void finalize()
          Clears the cache for finalization.
(package private)  CmsFlexCacheEntry get(CmsFlexCacheKey key)
          Looks up a specific entry in the cache.
 CmsFlexCacheKey getCachedKey(java.lang.String key, com.opencms.file.CmsObject cms)
          Returns the CmsFlexCacheKey data structure for a given key (i.e.
 java.util.Set getCachedResources(com.opencms.file.CmsObject cms)
          Returns a set of all cached resource names.
 java.util.Set getCachedVariations(java.lang.String key, com.opencms.file.CmsObject cms)
          Returns all variations in the cache for a given resource name.
 com.opencms.flex.util.CmsFlexLruCache getEntryLruCache()
          Returns the LRU cache where the CacheEntries are cached.
(package private)  CmsFlexCacheKey getKey(java.lang.String resource)
          Returns the CmsFlexCacheKey data structure for a given resource name.
private  boolean isAdmin(com.opencms.file.CmsObject cms)
          Internal method to determine if a user has Administration permissions.
(package private)  boolean isEmpty()
          Checks if the cache is empty or if at last one element is contained.
 boolean isEnabled()
          Indicates if the cache is enabled (i.e.
 int keySize()
          Returns the total number of cached resource keys.
private  void purgeDirectory(java.io.File d)
          Deletes a directory in the file system and all subfolders of the directory.
private  void purgeJspRepository(com.opencms.file.CmsObject cms)
          This method purges the JSP repository dirs, i.e.
private  void put(CmsFlexCacheKey key, CmsFlexCacheEntry theCacheEntry)
          Save a value to the cache.
(package private)  boolean put(CmsFlexCacheKey key, CmsFlexCacheEntry entry, java.lang.String variation)
          This method adds new entries to the cache.
(package private)  void putKey(CmsFlexCacheKey key)
          Adds a key with a new, empty variation map to the cache.
(package private)  void remove(CmsFlexCacheKey key)
          Removes an entry from the cache.
 int size()
          Returns the total number of entries in the cache.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

C_INITIAL_CAPACITY_CACHE

public static final int C_INITIAL_CAPACITY_CACHE
Initial Cache size, this should be a power of 2 because of the Java collections implementation

See Also:
Constant Field Values

C_INITIAL_CAPACITY_VARIATIONS

public static final int C_INITIAL_CAPACITY_VARIATIONS
Initial size for variation lists, should be a power of 2

See Also:
Constant Field Values

C_CACHE_ONLINESUFFIX

public static java.lang.String C_CACHE_ONLINESUFFIX
Suffix to append to online cache entries


C_CACHE_OFFLINESUFFIX

public static java.lang.String C_CACHE_OFFLINESUFFIX
Suffix to append to online cache entries


m_resourceMap

private java.util.Map m_resourceMap
Hashmap to store the Entries for fast lookup


m_size

private int m_size
Counter for the size


m_enabled

private boolean m_enabled
Indicates if the cache is enabled or not


m_cacheOffline

private boolean m_cacheOffline
Indicates if offline resources should be cached or not


DEBUG

private static final int DEBUG
Debug switch

See Also:
Constant Field Values

C_CLEAR_ALL

public static final int C_CLEAR_ALL
Static ints to trigger clearcache events

See Also:
Constant Field Values

C_CLEAR_ENTRIES

public static final int C_CLEAR_ENTRIES
See Also:
Constant Field Values

C_CLEAR_ONLINE_ALL

public static final int C_CLEAR_ONLINE_ALL
See Also:
Constant Field Values

C_CLEAR_ONLINE_ENTRIES

public static final int C_CLEAR_ONLINE_ENTRIES
See Also:
Constant Field Values

C_CLEAR_OFFLINE_ALL

public static final int C_CLEAR_OFFLINE_ALL
See Also:
Constant Field Values

C_CLEAR_OFFLINE_ENTRIES

public static final int C_CLEAR_OFFLINE_ENTRIES
See Also:
Constant Field Values

m_EntryLruCache

private com.opencms.flex.util.CmsFlexLruCache m_EntryLruCache
The LRU cache to organize the cached entries.


m_VariationCache

private com.opencms.flex.util.CmsFlexLruCache m_VariationCache
The LRU cache to organize the cached resources.

Constructor Detail

CmsFlexCache

public CmsFlexCache(com.opencms.core.OpenCms openCms)
Constructor for class CmsFlexCache.

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.

Method Detail

finalize

protected void finalize()
                 throws java.lang.Throwable
Clears the cache for finalization.


isEnabled

public boolean isEnabled()
Indicates if the cache is enabled (i.e. actually caching entries) or not.


cacheOffline

public boolean cacheOffline()
Indicates if offline project resources are cached.


clear

private void clear(com.opencms.file.CmsObject cms)
Clears all entries and all keys in the cache, online or offline.

Only users with administrator permissions are allowed to perform this operation.


clearEntries

private void clearEntries(com.opencms.file.CmsObject cms)
Clears all entries in the cache, online or offline. The keys are not cleared.

Only users with administrator permissions are allowed to perform this operation.


clearOffline

private void clearOffline(com.opencms.file.CmsObject cms)
Clears all entries and all keys from offline projects in the cache. Cached resources from the online project are not touched.

Only users with administrator permissions are allowed to perform this operation.


clearOfflineEntries

private void clearOfflineEntries(com.opencms.file.CmsObject cms)
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.

Only users with administrator permissions are allowed to perform this operation.


clearOnline

private void clearOnline(com.opencms.file.CmsObject cms)
Clears all entries and all keys from the online project in the cache. Cached resources from the offline projects are not touched.

Only users with administrator permissions are allowed to perform this operation.


clearOnlineEntries

private void clearOnlineEntries(com.opencms.file.CmsObject cms)
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.

Only users with administrator permissions are allowed to perform this operation.


purgeJspRepository

private void purgeJspRepository(com.opencms.file.CmsObject cms)
This method purges the JSP repository dirs, i.e. it deletes all JSP files that OpenCms has written to the real FS.

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.


purgeDirectory

private void purgeDirectory(java.io.File d)
Deletes a directory in the file system and all subfolders of the directory.


getCachedResources

public java.util.Set getCachedResources(com.opencms.file.CmsObject cms)
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.

Only users with administrator permissions are allowed to perform this operation.


getCachedVariations

public java.util.Set getCachedVariations(java.lang.String key,
                                         com.opencms.file.CmsObject cms)
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.

Only users with administrator permissions are allowed to perform this operation.


getCachedKey

public CmsFlexCacheKey getCachedKey(java.lang.String key,
                                    com.opencms.file.CmsObject cms)
Returns the CmsFlexCacheKey data structure 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.

Only users with administrator permissions are allowed to perform this operation.


size

public int size()
Returns the total number of entries in the cache.


keySize

public int keySize()
Returns the total number of cached resource keys.


containsKey

boolean containsKey(CmsFlexCacheKey key)
This method checks if a given key is already contained in the cache.


cmsEvent

public void cmsEvent(com.opencms.flex.CmsEvent event)
Implements the CmsEvent interface, the FlexCache uses the events to clear itself in case a project is published.

Specified by:
cmsEvent in interface com.opencms.flex.I_CmsEventListener

get

CmsFlexCacheEntry get(CmsFlexCacheKey key)
Looks up 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).


getKey

CmsFlexCacheKey getKey(java.lang.String resource)
Returns the CmsFlexCacheKey data structure for a given resource name.


putKey

void putKey(CmsFlexCacheKey key)
Adds a key with a new, empty variation map to the cache.


put

boolean put(CmsFlexCacheKey key,
            CmsFlexCacheEntry entry,
            java.lang.String variation)
This method adds new entries to the cache.

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 entry was created. This is usually calculated from the request. If the variation is != null, the entry is cachable.


remove

void remove(CmsFlexCacheKey key)
Removes an entry from the cache.


isEmpty

boolean isEmpty()
Checks if the cache is empty or if at last one element is contained.


clear

private void clear()
Emptys the cache completely.


put

private void put(CmsFlexCacheKey key,
                 CmsFlexCacheEntry theCacheEntry)
Save a value to the cache.


isAdmin

private boolean isAdmin(com.opencms.file.CmsObject cms)
Internal method to determine if a user has Administration permissions.


clearOneHalf

private void clearOneHalf(java.lang.String suffix,
                          boolean entriesOnly)
Internal method to perform cache clearance.

It clears "one half" of the cache, i.e. either the online or the offline parts. A parameter is used to indicate if only the entries or keys and entries are to be cleared.


getEntryLruCache

public com.opencms.flex.util.CmsFlexLruCache getEntryLruCache()
Returns the LRU cache where the CacheEntries are cached.