Class ExpirableCache<K,V>
- java.lang.Object
-
- org.owasp.csrfguard.config.overlay.ExpirableCache<K,V>
-
- Type Parameters:
K- key typeV- value type
- All Implemented Interfaces:
Serializable
public class ExpirableCache<K,V> extends Object implements Serializable
This is like a map, but the setters also take an expire time which will mean the items in the cache will be automatically deleted. Also, every so often (e.g. 2 minutes) all items are checked for expiration. If no timetolive is set, it will default to 1 day. The max time to live is one day.
This is synchronized so that all access is safe.
Note: evictions are checked for periodically, but only when the map is accessed (and even then only every few minutes). You can check for evictions externally, or clear the map if you are done with it.
- Version:
- $Id: ExpirableCache.java,v 1.1 2008-11-27 14:25:50 mchyzer Exp $
- Author:
- mchyzer
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classExpirableCache.ExpirableCacheUnitunit of time for expirable cache
-
Constructor Summary
Constructors Constructor Description ExpirableCache()ExpirableCache(int defaultTimeToLiveInMinutes)ExpirableCache(ExpirableCache.ExpirableCacheUnit expirableCacheUnit, int defaultTimeToLive)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheckForEvictions(boolean onlyCheckIfNeeded)check and remove elements that are stalevoidclear()delete the cachestatic voidclearAll()clear out all caches everywhere (session, request, context, etc)Vget(K key)get a value or null if not there or expired this will check for eviction, and evict if evictableintgetCacheEvictions()number of items evicted from cacheintgetCacheHits()number of items successfully retrieved from cacheintgetCacheInserts()number of items inserted into the cachelonggetDefaultTimeToLiveInMillis()expose the length of cachevoidput(K key, V value)put a value into the cache, accept the default time to live for this cachevoidput(K key, V value, int timeToLiveInMinutes)put a value into the cache, accept the default time to live for this cacheintsize(boolean evictEvictables)number of elements in map (and check forstatic StringstaticToString()string representation of cacheStringtoString()string representation of cache
-
-
-
Constructor Detail
-
ExpirableCache
public ExpirableCache()
-
ExpirableCache
public ExpirableCache(int defaultTimeToLiveInMinutes)
- Parameters:
defaultTimeToLiveInMinutes- time in minutes is the default cache time to live for content
-
ExpirableCache
public ExpirableCache(ExpirableCache.ExpirableCacheUnit expirableCacheUnit, int defaultTimeToLive)
- Parameters:
defaultTimeToLive- time in whatever unit is the default cache time to live for contentexpirableCacheUnit- is minutes or seconds
-
-
Method Detail
-
clear
public void clear()
delete the cache
-
getDefaultTimeToLiveInMillis
public long getDefaultTimeToLiveInMillis()
expose the length of cache- Returns:
- length of cache
-
put
public void put(K key, V value)
put a value into the cache, accept the default time to live for this cache- Parameters:
key- key typevalue- value type
-
put
public void put(K key, V value, int timeToLiveInMinutes)
put a value into the cache, accept the default time to live for this cache- Parameters:
key- key typevalue- value typetimeToLiveInMinutes- time to live for this item in minutes. If -1 then use the default
-
clearAll
public static void clearAll()
clear out all caches everywhere (session, request, context, etc)
-
checkForEvictions
public void checkForEvictions(boolean onlyCheckIfNeeded)
check and remove elements that are stale- Parameters:
onlyCheckIfNeeded- true if only check every so often (e.g. every two minutes)
-
get
public V get(K key)
get a value or null if not there or expired this will check for eviction, and evict if evictable- Parameters:
key- key type- Returns:
- the value or null if not there or evicted
-
size
public int size(boolean evictEvictables)
number of elements in map (and check for- Parameters:
evictEvictables- true if we should evict values that are stale (even if recently checked)- Returns:
- the number of elements
-
getCacheInserts
public int getCacheInserts()
number of items inserted into the cache- Returns:
- Returns the cacheInserts.
-
getCacheEvictions
public int getCacheEvictions()
number of items evicted from cache- Returns:
- Returns the cacheEvictions.
-
getCacheHits
public int getCacheHits()
number of items successfully retrieved from cache- Returns:
- Returns the cacheHits.
-
toString
public String toString()
string representation of cache- Overrides:
toStringin classObject- See Also:
Object.toString()
-
staticToString
public static String staticToString()
string representation of cache- Returns:
- the string value
-
-