jodd.cache
Class AbstractCacheMap<K,V>

java.lang.Object
  extended by jodd.cache.AbstractCacheMap<K,V>
All Implemented Interfaces:
Cache<K,V>
Direct Known Subclasses:
FIFOCache, LFUCache, LRUCache, TimedCache

public abstract class AbstractCacheMap<K,V>
extends java.lang.Object
implements Cache<K,V>

Default implementation of timed and size cache map. Implementations should:


Field Summary
protected  java.lang.Object cacheLock
           
protected  java.util.Map<K,jodd.cache.AbstractCacheMap.CacheObject<K,V>> cacheMap
           
protected  int cacheSize
           
protected  boolean existCustomTimeout
          Identifies if objects has custom timeouts.
protected  long timeout
           
 
Constructor Summary
AbstractCacheMap()
           
 
Method Summary
 void clear()
          Clears current cache.
 V get(K key)
          Retrieves an object from the cache.
 int getCacheSize()
          Returns cache size or 0 if there is no size limit.
 long getCacheTimeout()
          Returns default cache timeout or 0 if it is not set.
 boolean isEmpty()
          Returns true if cache is empty.
 boolean isFull()
          Returns true if max cache capacity has been reached only if cache is size limited.
protected  boolean isPruneExpiredActive()
          Returns true if prune of expired objects should be invoked.
 java.util.Iterator<V> iterator()
          Returns iterator over non-expired values.
 int prune()
          Prunes objects from cache and returns the number of removed objects.
protected abstract  int pruneCache()
          Prune implementation.
 void put(K key, V object)
          Adds an object to the cache with default timeout.
 void put(K key, V object, long timeout)
          Adds an object to the cache with specified timeout after which it becomes expired.
 void remove(K key)
          Removes an object from the cache.
 int size()
          Returns current cache size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cacheMap

protected java.util.Map<K,jodd.cache.AbstractCacheMap.CacheObject<K,V>> cacheMap

cacheLock

protected final java.lang.Object cacheLock

cacheSize

protected int cacheSize

timeout

protected long timeout

existCustomTimeout

protected boolean existCustomTimeout
Identifies if objects has custom timeouts. Should be used to determine if prune for existing objects is needed.

Constructor Detail

AbstractCacheMap

public AbstractCacheMap()
Method Detail

getCacheSize

public int getCacheSize()
Returns cache size or 0 if there is no size limit.

Specified by:
getCacheSize in interface Cache<K,V>

getCacheTimeout

public long getCacheTimeout()
Returns default cache timeout or 0 if it is not set. Timeout can be set individually for each object.

Specified by:
getCacheTimeout in interface Cache<K,V>

isPruneExpiredActive

protected boolean isPruneExpiredActive()
Returns true if prune of expired objects should be invoked. For internal use.


put

public void put(K key,
                V object)
Adds an object to the cache with default timeout.

Specified by:
put in interface Cache<K,V>
See Also:
Cache.put(Object, Object, long)

put

public void put(K key,
                V object,
                long timeout)
Adds an object to the cache with specified timeout after which it becomes expired. If cache is full, Cache.prune() is invoked to make room for new object.

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

get

public V get(K key)
Retrieves an object from the cache. Returns null if object is not longer in cache or if it is expired.

Specified by:
get in interface Cache<K,V>

iterator

public java.util.Iterator<V> iterator()
Returns iterator over non-expired values.

Specified by:
iterator in interface Cache<K,V>

pruneCache

protected abstract int pruneCache()
Prune implementation.


prune

public final int prune()
Prunes objects from cache and returns the number of removed objects. Used strategy depends on cache implementation.

Specified by:
prune in interface Cache<K,V>

isFull

public boolean isFull()
Returns true if max cache capacity has been reached only if cache is size limited.

Specified by:
isFull in interface Cache<K,V>

remove

public void remove(K key)
Removes an object from the cache.

Specified by:
remove in interface Cache<K,V>

clear

public void clear()
Clears current cache.

Specified by:
clear in interface Cache<K,V>

size

public int size()
Returns current cache size.

Specified by:
size in interface Cache<K,V>

isEmpty

public boolean isEmpty()
Returns true if cache is empty.

Specified by:
isEmpty in interface Cache<K,V>


Copyright © 2003-2012 Jodd Team