com.opensymphony.oscache.plugins.diskpersistence
Class AbstractDiskPersistenceListener

java.lang.Object
  extended by com.opensymphony.oscache.plugins.diskpersistence.AbstractDiskPersistenceListener
All Implemented Interfaces:
PersistenceListener, Serializable
Direct Known Subclasses:
DiskPersistenceListener, HashDiskPersistenceListener

public abstract class AbstractDiskPersistenceListener
extends Object
implements PersistenceListener, Serializable

Persist the cache data to disk. The code in this class is totally not thread safe it is the resonsibility of the cache using this persistence listener to handle the concurrency.

Author:
Francois Beauregard, Alain Bergevin, Chris Miller, Andres March
See Also:
Serialized Form

Field Summary
protected static String APPLICATION_CACHE_SUBPATH
          Sub path name for application cache
protected static String CACHE_EXTENSION
          File extension for disk cache file
static String CACHE_PATH_KEY
           
private  File cachePath
          Base path where the disk cache reside.
protected static String CONTEXT_TMPDIR
          Property to get the temporary working directory of the servlet container.
private  File contextTmpDir
           
private static int DELETE_COUNT
           
private static long DELETE_THREAD_SLEEP
           
protected static String GROUP_DIRECTORY
          The directory that cache groups are stored under
private static org.apache.commons.logging.Log log
           
private  String root
          Root path for disk cache
protected static String SESSION_CACHE_SUBPATH
          Sub path name for session cache
 
Constructor Summary
AbstractDiskPersistenceListener()
           
 
Method Summary
protected  String adjustFileCachePath(String cachePathStr)
          Allows to translate to the temp dir of the servlet container if cachePathStr is javax.servlet.context.tempdir.
 void clear()
          Clears the whole cache directory, starting from the root
private  void clear(String baseDirName)
          Clears a whole directory, starting from the specified directory
 PersistenceListener configure(Config config)
          Initialises this DiskPersistenceListener using the supplied configuration.
protected  File getCacheFile(String key)
          Build fully qualified cache file for the specified cache entry key.
protected abstract  char[] getCacheFileName(String key)
          Build cache file name for the specified cache entry key.
private  File getCacheGroupFile(String group)
          Builds a fully qualified file name that specifies a cache group entry.
 File getCachePath()
          Get the physical cache path on disk.
 File getContextTmpDir()
          Get the servlet context tmp directory.
private  String getPathPart(int scope)
          This allows to persist different scopes in different path in the case of file caching.
 String getRoot()
          Get the root directory for persisting the cache on disk.
protected  void initFileCaching(String cachePathStr)
          Set caching to file on or off.
 boolean isGroupStored(String group)
          Verify if a group exists in the cache
 boolean isStored(String key)
          Verify if an object is currently stored in the cache
protected  void remove(File file)
           
 void remove(String key)
          Delete a single cache entry.
 void removeGroup(String groupName)
          Deletes an entire group from the cache.
private  Object retrieve(File file)
          Retrives a serialized object from the supplied file, or returns null if the file does not exist.
 Object retrieve(String key)
          Retrieve an object from the disk
 Set retrieveGroup(String groupName)
          Retrieves a group from the cache, or null if the group file could not be found.
protected  void store(File file, Object obj)
          Stores an object using the supplied file object
 void store(String key, Object obj)
          Stores an object in cache
 void storeGroup(String groupName, Set group)
          Stores a group in the persistent cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CACHE_PATH_KEY

public static final String CACHE_PATH_KEY
See Also:
Constant Field Values

CACHE_EXTENSION

protected static final String CACHE_EXTENSION
File extension for disk cache file

See Also:
Constant Field Values

GROUP_DIRECTORY

protected static final String GROUP_DIRECTORY
The directory that cache groups are stored under

See Also:
Constant Field Values

APPLICATION_CACHE_SUBPATH

protected static final String APPLICATION_CACHE_SUBPATH
Sub path name for application cache

See Also:
Constant Field Values

SESSION_CACHE_SUBPATH

protected static final String SESSION_CACHE_SUBPATH
Sub path name for session cache

See Also:
Constant Field Values

CONTEXT_TMPDIR

protected static final String CONTEXT_TMPDIR
Property to get the temporary working directory of the servlet container.

See Also:
Constant Field Values

log

private static final transient org.apache.commons.logging.Log log

cachePath

private File cachePath
Base path where the disk cache reside.


contextTmpDir

private File contextTmpDir

root

private String root
Root path for disk cache


DELETE_THREAD_SLEEP

private static final long DELETE_THREAD_SLEEP
See Also:
Constant Field Values

DELETE_COUNT

private static final int DELETE_COUNT
See Also:
Constant Field Values
Constructor Detail

AbstractDiskPersistenceListener

public AbstractDiskPersistenceListener()
Method Detail

getCachePath

public File getCachePath()
Get the physical cache path on disk.

Returns:
A file representing the physical cache location.

getRoot

public String getRoot()
Get the root directory for persisting the cache on disk. This path includes scope and sessionId, if any.

Returns:
A String representing the root directory.

getContextTmpDir

public File getContextTmpDir()
Get the servlet context tmp directory.

Returns:
A file representing the servlet context tmp directory.

isGroupStored

public boolean isGroupStored(String group)
                      throws CachePersistenceException
Verify if a group exists in the cache

Specified by:
isGroupStored in interface PersistenceListener
Parameters:
group - The group name to check
Returns:
True if it exists
Throws:
CachePersistenceException

isStored

public boolean isStored(String key)
                 throws CachePersistenceException
Verify if an object is currently stored in the cache

Specified by:
isStored in interface PersistenceListener
Parameters:
key - The object key
Returns:
True if it exists
Throws:
CachePersistenceException

clear

public void clear()
           throws CachePersistenceException
Clears the whole cache directory, starting from the root

Specified by:
clear in interface PersistenceListener
Throws:
CachePersistenceException

configure

public PersistenceListener configure(Config config)
Initialises this DiskPersistenceListener using the supplied configuration.

Specified by:
configure in interface PersistenceListener
Parameters:
config - The OSCache configuration

remove

public void remove(String key)
            throws CachePersistenceException
Delete a single cache entry.

Specified by:
remove in interface PersistenceListener
Parameters:
key - The object key to delete
Throws:
CachePersistenceException

removeGroup

public void removeGroup(String groupName)
                 throws CachePersistenceException
Deletes an entire group from the cache.

Specified by:
removeGroup in interface PersistenceListener
Parameters:
groupName - The name of the group to delete
Throws:
CachePersistenceException

retrieve

public Object retrieve(String key)
                throws CachePersistenceException
Retrieve an object from the disk

Specified by:
retrieve in interface PersistenceListener
Parameters:
key - The object key
Returns:
The retrieved object
Throws:
CachePersistenceException

retrieveGroup

public Set retrieveGroup(String groupName)
                  throws CachePersistenceException
Retrieves a group from the cache, or null if the group file could not be found.

Specified by:
retrieveGroup in interface PersistenceListener
Parameters:
groupName - The name of the group to retrieve.
Returns:
A Set containing keys of all of the cache entries that belong to this group.
Throws:
CachePersistenceException

store

public void store(String key,
                  Object obj)
           throws CachePersistenceException
Stores an object in cache

Specified by:
store in interface PersistenceListener
Parameters:
key - The object's key
obj - The object to store
Throws:
CachePersistenceException

storeGroup

public void storeGroup(String groupName,
                       Set group)
                throws CachePersistenceException
Stores a group in the persistent cache. This will overwrite any existing group with the same name

Specified by:
storeGroup in interface PersistenceListener
Parameters:
groupName - The name of the group to persist.
group - A set containing the keys of all the CacheEntry objects that belong to this group.
Throws:
CachePersistenceException

adjustFileCachePath

protected String adjustFileCachePath(String cachePathStr)
Allows to translate to the temp dir of the servlet container if cachePathStr is javax.servlet.context.tempdir.

Parameters:
cachePathStr - Cache path read from the properties file.
Returns:
Adjusted cache path

initFileCaching

protected void initFileCaching(String cachePathStr)
Set caching to file on or off. If the cache.path property exists, we assume file caching is turned on. By the same token, to turn off file caching just remove this property.


remove

protected void remove(File file)
               throws CachePersistenceException
Throws:
CachePersistenceException

store

protected void store(File file,
                     Object obj)
              throws CachePersistenceException
Stores an object using the supplied file object

Parameters:
file - The file to use for storing the object
obj - the object to store
Throws:
CachePersistenceException

getCacheFile

protected File getCacheFile(String key)
Build fully qualified cache file for the specified cache entry key.

Parameters:
key - Cache Entry Key.
Returns:
File reference.

getCacheFileName

protected abstract char[] getCacheFileName(String key)
Build cache file name for the specified cache entry key.

Parameters:
key - Cache Entry Key.
Returns:
char[] file name.

getCacheGroupFile

private File getCacheGroupFile(String group)
Builds a fully qualified file name that specifies a cache group entry.

Parameters:
group - The name of the group
Returns:
A File reference

getPathPart

private String getPathPart(int scope)
This allows to persist different scopes in different path in the case of file caching.

Parameters:
scope - Cache scope.
Returns:
The scope subpath

clear

private void clear(String baseDirName)
            throws CachePersistenceException
Clears a whole directory, starting from the specified directory

Parameters:
baseDirName - The root directory to delete
Throws:
CachePersistenceException

retrieve

private Object retrieve(File file)
                 throws CachePersistenceException
Retrives a serialized object from the supplied file, or returns null if the file does not exist.

Parameters:
file - The file to deserialize
Returns:
The deserialized object
Throws:
CachePersistenceException


Copyright © 2011 OpenSymphony. All Rights Reserved.