org.mmbase.cache
Class Cache<K,V>

java.lang.Object
  extended by org.mmbase.cache.Cache<K,V>
All Implemented Interfaces:
Map<K,V>, CacheMBean, SizeMeasurable
Direct Known Subclasses:
AbstractImages.CKeyCache, AttributeCache, BlobCache, Caches.OperationsCache, FactoryCache, NodeCache, QueryResultCache, RelationsCache, ResultCache, TemplateCache, URLCache

public abstract class Cache<K,V>
extends Object
implements SizeMeasurable, Map<K,V>, CacheMBean

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

Version:
$Id: Cache.java 41989 2010-04-20 16:45:39Z michiel $
Author:
Michiel Meeuwissen

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  Object lock
           
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<Map.Entry<K,V>> entrySet()
           
 boolean equals(Object o)
           
 V get(Object key)
          Like 'get' of Maps but considers if the cache is active or not, and the cache policy of the key.
 double getAvarageValueLength()
          Returns the average 'length' of the values in the cache.
 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.
 int getCheapByteSize()
          Returns the sum of bytesizes of every key and value.
 int getCount(K 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.
 long getHits()
          Returns the number of times an element was succesfully retrieved from the table.
 Class<?> getImplementation()
           
 Object getLock()
          If you want to structurally modify this cache, synchronize on this object.
 int getMaxEntrySize()
          Return the maximum entry size for the cache in bytes.
 int getMaxSize()
           
 long getMisses()
          Returns the number of times an element cpould not be retrieved from the table.
 String getName()
          Returns a name for this cache type.
 long getPuts()
          Returns the number of times an element was committed to the table.
 double getRatio()
          Returns the ratio of hits and misses.
 int getSize()
           
 String getStats()
          Returns statistics on this table.
 int hashCode()
           
 boolean isActive()
          Wether this cache is active or not.
 boolean isEmpty()
           
 Set<K> keySet()
           
 int maxSize()
           
 V put(K key, V value)
          Like 'put' of LRUHashtable but considers if the cache is active or not.
 void putAll(Map<? extends K,? extends V> t)
           
 Cache<K,V> putCache()
          Puts this cache in the caches repository.
 V remove(Object key)
           
 void reset()
          Reset 'puts', 'misses' and 'puts' to 0.
 void setActive(boolean a)
          Sets this cache to active or passive.
(package private)  void setImplementation(String clazz, Map<String,String> configValues)
           
 void setMaxEntrySize(int i)
           
 void setMaxSize(int size)
           
 int size()
           
 String toString()
           
 Collection<V> values()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

maxEntrySize

protected int maxEntrySize

lock

protected volatile Object lock
Constructor Detail

Cache

public Cache(int size)
Method Detail

setImplementation

void setImplementation(String clazz,
                       Map<String,String> configValues)

getLock

public final Object getLock()
If you want to structurally modify this cache, synchronize on this object.

Since:
MMBase-1.8.6

getName

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

Specified by:
getName in interface CacheMBean

getDescription

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

Specified by:
getDescription in interface CacheMBean

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.

Specified by:
getMaxEntrySize in interface CacheMBean
See Also:
getMaxEntrySize()

setMaxEntrySize

public void setMaxEntrySize(int i)
Specified by:
setMaxEntrySize in interface CacheMBean
Since:
MMBase-1.9.2
See Also:
setMaxEntrySize(int)

getAvarageValueLength

public double getAvarageValueLength()
Returns the average 'length' of the values in the cache. Whatever that may mean. For a QueryResultCache the length obviously is the length of the cached lists. May return NaN if unknown or undetermined.

Specified by:
getAvarageValueLength in interface CacheMBean
Since:
MMBase-1.9.2

getDefaultMaxEntrySize

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


entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>

getImplementation

public Class<?> getImplementation()
Since:
MMBase-1.8.6

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 V 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<K,V>

put

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

Specified by:
put in interface Map<K,V>

getHits

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

Specified by:
getHits in interface CacheMBean
See Also:
getHits()

getMisses

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

Specified by:
getMisses in interface CacheMBean
See Also:
getMisses()

getPuts

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

Specified by:
getPuts in interface CacheMBean
See Also:
getPuts()

reset

public void reset()
Reset 'puts', 'misses' and 'puts' to 0.

Specified by:
reset in interface CacheMBean
Since:
MMBase-1.9.2
See Also:
reset()

setMaxSize

public void setMaxSize(int size)
Specified by:
setMaxSize in interface CacheMBean

maxSize

public int maxSize()

getMaxSize

public int getMaxSize()
Specified by:
getMaxSize in interface CacheMBean
See Also:
getMaxSize()

size

public int size()
Specified by:
size in interface Map<K,V>
See Also:
Map.size()

getSize

public int getSize()
Specified by:
getSize in interface CacheMBean
See Also:
getSize()

contains

public boolean contains(Object key)

getCount

public int getCount(K 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.

Specified by:
getRatio in interface CacheMBean
Returns:
A double between 0 and 1 or NaN.
See Also:
getRatio()

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)

Specified by:
setActive in interface CacheMBean
See Also:
isActive()

toString

public String toString()
Overrides:
toString in class Object

isActive

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

Specified by:
isActive in interface CacheMBean

getByteSize

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

Specified by:
getByteSize in interface CacheMBean
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<K,V>
Specified by:
clear in interface CacheMBean
See Also:
Map.clear()

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>
See Also:
Map.containsKey(java.lang.Object)

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>
See Also:
Map.containsValue(java.lang.Object)

equals

public boolean equals(Object o)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object
See Also:
Map.equals(java.lang.Object)

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object
See Also:
Map.hashCode()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>
See Also:
Map.isEmpty()

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>
See Also:
Map.keySet()

putAll

public void putAll(Map<? extends K,? extends V> t)
Specified by:
putAll in interface Map<K,V>
See Also:
Map.putAll(java.util.Map)

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>
See Also:
Map.remove(java.lang.Object)

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>
See Also:
Map.values()

putCache

public Cache<K,V> putCache()
Puts this cache in the caches repository.

See Also:
CacheManager.putCache(Cache)


MMBase 2.0-SNAPSHOT - null