uk.co.mullsoft.jean.components
Class CacheTable

java.lang.Object
  extended by java.util.Dictionary
      extended by java.util.Hashtable
          extended by uk.co.mullsoft.jean.components.CacheTable
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map

public class CacheTable
extends java.util.Hashtable

This class extends Hashtable to provide a persistent cache. If objects of classes that do not implement the Cacheable Interface are placed in here then it behaves as a normal Hashtable with no persistence. If Cacheable objects are put in here then as well as being stored as in a normal Hashtable their selector part is placed in a separate Hashtable and their data part is written to disk. At system exit the save() method will save the selector Hashtable to disk. At system startup the selector hashtable is read in.

When a call is made to get() then the underlying Hahstable is accessed. If the item is not there then a check is made in the selector hashtable. If it is there then a new Cacheable object is created using the selector and the loaded data. If it is not there then null is returned as usual.

When a cacheable object is put into the hashtable then a reference is placed in both tables and the data part written out to disk (in a separate thread). When a Cacheable object is removed from the table then it is only removed from the underlying table. It is retained in the selectors hashtable but its data part is set to null so as to conserve space. When the table is saved

Author:
mike.curtis
See Also:
Cacheable, Serialized Form

Constructor Summary
CacheTable()
          Creates a new instance of CacheTable with name "default"
CacheTable(java.lang.String name)
          Creates a new instance of CacheTable
 
Method Summary
static void clearCacheDirectory()
          clear out the cache directory.
protected static void decrementCacheSize(long by)
          increment the currentCacheSize
 java.util.Enumeration elements()
          Returns an enumeration of the values in this hashtable.
 void finalize()
          Try to catch anybody who has exited without explicitly calling save().
 void forceRemove(java.lang.Object key)
           
 java.lang.Object get(java.lang.Object key)
          Returns the value to which the specified key is mapped in this hashtable
static java.io.File getCacheDirectory()
          get the cache directory
static long getCurrentCacheSize()
          Getter for property currentCacheSize.
static long getMaximumCacheSize()
          Getter for property maximumCacheSize.
static int getNumberOfFilesToDelete()
          Getter for property numberOfFilesToDelete.
 java.lang.Object getUninflated(java.lang.Object key)
          Returns the value to which the specified key is mapped in this hashtable
protected static void incrementCacheSize(long by)
          increment the currentCacheSize
 void inflate(Cacheable ch)
           
 void loadItems(java.lang.Object[] keys, Cacheable[] values)
          load a set of objects into the local cache but not into the memory cache
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Maps the specified key to the specified value in this hashtable.
 java.lang.Object remove(java.lang.Object key)
          Removes the key (and its corresponding value) from this hashtable.
 void save()
          save the selector Hashtable to disk using its name and the extension ".cache" in the cacheDirectory.
static void setCacheDirectory(java.io.File newCacheDirectory)
          set a new value for the cache directory
protected static void setCurrentCacheSize(long newCurrentCacheSize)
          Setter for property currentCacheSize.
 void setExample(Cacheable ex)
          set an example of the class of object being cached that can be used to create new ones.
static void setMaximumCacheSize(long newMaximumCacheSize)
          Setter for property maximumCacheSize.
static void setNumberOfFilesToDelete(int newNumberOfFilesToDelete)
          Setter for property numberOfFilesToDelete.
 
Methods inherited from class java.util.Hashtable
clear, clone, contains, containsKey, containsValue, entrySet, equals, hashCode, isEmpty, keys, keySet, putAll, rehash, size, toString, values
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CacheTable

public CacheTable()
Creates a new instance of CacheTable with name "default"


CacheTable

public CacheTable(java.lang.String name)
Creates a new instance of CacheTable

Parameters:
name - the name of this cache table.
Method Detail

getCacheDirectory

public static java.io.File getCacheDirectory()
get the cache directory

Returns:
the cache directory

setCacheDirectory

public static void setCacheDirectory(java.io.File newCacheDirectory)
set a new value for the cache directory

Parameters:
newCacheDirectory - the new value for the cache directory. It must be an existing directory

clearCacheDirectory

public static void clearCacheDirectory()
clear out the cache directory. All cached files and subdirectories are deleted


getMaximumCacheSize

public static long getMaximumCacheSize()
Getter for property maximumCacheSize.

Returns:
Value of property maximumCacheSize.

setMaximumCacheSize

public static void setMaximumCacheSize(long newMaximumCacheSize)
Setter for property maximumCacheSize.

Parameters:
maximumCacheSize - New value of property maximumCacheSize.

getCurrentCacheSize

public static long getCurrentCacheSize()
Getter for property currentCacheSize.

Returns:
Value of property currentCacheSize.

setCurrentCacheSize

protected static void setCurrentCacheSize(long newCurrentCacheSize)
Setter for property currentCacheSize.

Parameters:
currentCacheSize - New value of property currentCacheSize.

incrementCacheSize

protected static void incrementCacheSize(long by)
increment the currentCacheSize

Parameters:
the - amount to add on

decrementCacheSize

protected static void decrementCacheSize(long by)
increment the currentCacheSize

Parameters:
the - amount to add on

getNumberOfFilesToDelete

public static int getNumberOfFilesToDelete()
Getter for property numberOfFilesToDelete.

Returns:
Value of property numberOfFilesToDelete.

setNumberOfFilesToDelete

public static void setNumberOfFilesToDelete(int newNumberOfFilesToDelete)
Setter for property numberOfFilesToDelete.

Parameters:
numberOfFilesToDelete - New value of property numberOfFilesToDelete.

loadItems

public void loadItems(java.lang.Object[] keys,
                      Cacheable[] values)
load a set of objects into the local cache but not into the memory cache


put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.Hashtable
Parameters:
key - the hashtable key.
value - the value

inflate

public void inflate(Cacheable ch)

getUninflated

public java.lang.Object getUninflated(java.lang.Object key)
Returns the value to which the specified key is mapped in this hashtable

Parameters:
key - a key in the hashtable
Returns:
the value to which the key is mapped in this hashtable; null if the key is not mapped to any value in this hashtable.

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which the specified key is mapped in this hashtable

Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.Hashtable
Parameters:
key - a key in the hashtable
Returns:
the value to which the key is mapped in this hashtable; null if the key is not mapped to any value in this hashtable.

save

public void save()
save the selector Hashtable to disk using its name and the extension ".cache" in the cacheDirectory.


finalize

public void finalize()
Try to catch anybody who has exited without explicitly calling save().

Overrides:
finalize in class java.lang.Object

elements

public java.util.Enumeration elements()
Returns an enumeration of the values in this hashtable. Use the Enumeration methods on the returned object to fetch the elements sequentially

Overrides:
elements in class java.util.Hashtable
Returns:
an enumeration of the values in this hashtable

remove

public java.lang.Object remove(java.lang.Object key)
Removes the key (and its corresponding value) from this hashtable. This method does nothing if the key is not in the hashtable

Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.Hashtable
Parameters:
key - the key that needs to be removed
Returns:
the value to which the key had been mapped in this hashtable, or null if the key did not have a mapping

forceRemove

public void forceRemove(java.lang.Object key)

setExample

public void setExample(Cacheable ex)
set an example of the class of object being cached that can be used to create new ones.