org.mmbase.cache
Class Cache

java.lang.Object
  extended byorg.mmbase.cache.Cache
All Implemented Interfaces:
Map, SizeMeasurable
Direct Known Subclasses:
AbstractImages.CKeyCache, BlobCache, Contexts.OperationsCache, FactoryCache, NodeCache, QueryResultCache, RelationsCache, ResultCache, TemplateCache, URLCache

public abstract class Cache
extends Object
implements SizeMeasurable, Map

A base class for all Caches. Extend this class for other caches.

Version:
$Id: Cache.java,v 1.35 2006/02/23 17:36:55 michiel Exp $
Author:
Michiel Meeuwissen

Nested Class Summary
 
Nested classes inherited from class java.util.Map
Map.Entry
 
Field Summary
protected  int maxEntrySize
           
 
Constructor Summary
Cache(int size)
           
 
Method Summary
protected  boolean checkCachePolicy(Object key)
          Checks whether the key object should be cached.
 void clear()
           
 boolean contains(Object key)
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set entrySet()
           
 boolean equals(Object o)
           
 Object get(Object key)
          Like 'get' of Maps but considers if the cache is active or not, and the cache policy of the key.
 int getByteSize()
          Determines the byte-size of this object
 int getByteSize(SizeOf sizeof)
          Determines the byte-size of this object using the given SizeOf instance.
static Cache getCache(String name)
           
static Set getCaches()
           
 int getCheapByteSize()
          Returns the sum of bytesizes of every key and value.
 int getCount(Object key)
           
protected  int getDefaultMaxEntrySize()
          This has to be overridden by Caches which support max entry size.
 String getDescription()
          Gives a description for this cache type.
 int getHits()
          Returns the number of times an element was succesfully retrieved from the table.
 int getMaxEntrySize()
          Return the maximum entry size for the cache in bytes.
 int getMisses()
          Returns the number of times an element cpould not be retrieved from the table.
 String getName()
          Returns a name for this cache type.
 int getPuts()
          Returns the number of times an element was committed to the table.
 double getRatio()
          Returns the ratio of hits and misses.
 String getStats()
          Returns statistics on this table.
static int getTotalByteSize()
           
 int hashCode()
           
 boolean isActive()
          Wether this cache is active or not.
 boolean isEmpty()
           
 Set keySet()
           
static void main(String[] args)
           
 int maxSize()
           
 Object put(Object key, Object value)
          Like 'put' of LRUHashtable but considers if the cache is active or not.
 void putAll(Map t)
           
 Cache putCache()
          Puts this cache in the caches repository.
protected static Cache putCache(Cache cache)
           
 Object remove(Object key)
           
 void setActive(boolean a)
          Sets this cache to active or passive.
 void setMaxSize(int size)
           
 int size()
           
 String toString()
           
 Collection values()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

maxEntrySize

protected int maxEntrySize
Constructor Detail

Cache

public Cache(int size)
Method Detail

getName

public String getName()
Returns a name for this cache type. Default it is the class name, but this normally will be overriden.


getDescription

public String getDescription()
Gives a description for this cache type. This can be used in cache overviews.


getMaxEntrySize

public int getMaxEntrySize()
Return the maximum entry size for the cache in bytes. If the cache-type supports it (default no), then no values bigger then this will be stored in the cache.


getDefaultMaxEntrySize

protected int getDefaultMaxEntrySize()
This has to be overridden by Caches which support max entry size.


entrySet

public Set entrySet()
Specified by:
entrySet in interface Map

checkCachePolicy

protected boolean checkCachePolicy(Object key)
Checks whether the key object should be cached. This method returns false if either the current cache is inactive, or the object to cache has a cache policy associated that prohibits caching of the object.

Parameters:
key - the object to be cached
Returns:
true if the object can be cached
Since:
MMBase-1.8

get

public Object get(Object key)
Like 'get' of Maps but considers if the cache is active or not, and the cache policy of the key.

Specified by:
get in interface Map

put

public Object put(Object key,
                  Object value)
Like 'put' of LRUHashtable but considers if the cache is active or not.

Specified by:
put in interface Map

getHits

public int getHits()
Returns the number of times an element was succesfully retrieved from the table.


getMisses

public int getMisses()
Returns the number of times an element cpould not be retrieved from the table.


getPuts

public int getPuts()
Returns the number of times an element was committed to the table.


setMaxSize

public void setMaxSize(int size)

maxSize

public int maxSize()

size

public int size()
Specified by:
size in interface Map
See Also:
Map.size()

contains

public boolean contains(Object key)

getCount

public int getCount(Object key)

getRatio

public double getRatio()
Returns the ratio of hits and misses. The higher the ratio, the more succesfull the table retrieval is. A value of 1 means every attempt to retrieve data is succesfull, while a value nearing 0 means most times the object requested it is not available. Generally a high ratio means the table can be shrunk, while a low ratio means its size needs to be increased.

Returns:
A double between 0 and 1 or NaN.

getStats

public String getStats()
Returns statistics on this table. The information shown includes number of accesses, ratio of misses and hits, current size, and number of puts.


setActive

public void setActive(boolean a)
Sets this cache to active or passive. TODO: Writing back to caches.xml if necessary (if this call was nog caused by change of caches.xml itself)


toString

public String toString()

isActive

public final boolean isActive()
Wether this cache is active or not.


getByteSize

public int getByteSize()
Description copied from interface: SizeMeasurable
Determines the byte-size of this object

Specified by:
getByteSize in interface SizeMeasurable

getByteSize

public int getByteSize(SizeOf sizeof)
Description copied from interface: SizeMeasurable
Determines the byte-size of this object using the given SizeOf instance. A SizeOf instance stores a Set already counted objects. So this method is typically called by SizeOf itself (recursion).

Specified by:
getByteSize in interface SizeMeasurable

getCheapByteSize

public int getCheapByteSize()
Returns the sum of bytesizes of every key and value. This may count too much, because objects (like Nodes) may occur in more then one value, but this is considerably cheaper then getByteSize(), which has to keep a Collection of every counted object.

Since:
MMBase-1.8

clear

public void clear()
Specified by:
clear in interface Map
See Also:
Map.clear()

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map
See Also:
Map.containsKey(java.lang.Object)

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map
See Also:
Map.containsValue(java.lang.Object)

equals

public boolean equals(Object o)
Specified by:
equals in interface Map
See Also:
Map.equals(java.lang.Object)

hashCode

public int hashCode()
Specified by:
hashCode in interface Map
See Also:
Map.hashCode()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map
See Also:
Map.isEmpty()

keySet

public Set keySet()
Specified by:
keySet in interface Map
See Also:
Map.keySet()

putAll

public void putAll(Map t)
Specified by:
putAll in interface Map
See Also:
Map.putAll(java.util.Map)

remove

public Object remove(Object key)
Specified by:
remove in interface Map
See Also:
Map.remove(java.lang.Object)

values

public Collection values()
Specified by:
values in interface Map
See Also:
Map.values()

putCache

public Cache putCache()
Puts this cache in the caches repository.

See Also:
CacheManager.putCache(Cache)

putCache

protected static Cache putCache(Cache cache)
See Also:
CacheManager.getCache(String)

getCache

public static Cache getCache(String name)
See Also:
CacheManager.getCache(String)

getCaches

public static Set getCaches()
See Also:
CacheManager.getCaches()

getTotalByteSize

public static int getTotalByteSize()
See Also:
CacheManager.getTotalByteSize()

main

public static void main(String[] args)


MMBase build 1.8.1.20060716