public class CacheMap
- Object
- CacheMap
A cache map is essentially a hashtable that indexes entries based on age and is
limited to a fixed size. Hence when an entry is placed into the cache map and the
cache size needs to increase, the least referenced entry is removed.
A cache hit is made both on fetching and putting, hence frequently fetched elements
will never be removed from a sufficiently large cache.
Cache can work purely in memory or swap data into storage based on user definitions.
Notice that this class isn’t threadsafe.
Constructors
public CacheMap() | Default constructor |
public CacheMap(String prefix) | Creates a cache map with a prefix string |
Methods
public int getCacheSize() | Indicates the size of the memory cache after which the cache won’t grow further Size is indicated by number of elements stored and not by KB or similar benchmark! |
public void setCacheSize(int cacheSize) | Indicates the size of the memory cache after which the cache won’t grow further Size is indicated by number of elements stored and not by KB or similar benchmark! |
public void put(Object key, Object value) | Puts the given key/value pair in the cache |
public void delete(Object key) | Deletes a cached entry |
public Object get(Object key) | Returns the object matching the given key |
public void clearAllCache() | Clears the caches for this cache object |
public void clearMemoryCache() | Clears the memory cache |
public Vector getKeysInCache() | Returns the keys for all the objects currently in cache, this is useful to traverse all the objects and refresh them without actually deleting the cache and fetching them from scratch. |
public void clearStorageCache() | Clears the storage cache |
public int getStorageCacheSize() | Indicates the size of the storage cache after which the cache won’t grow further Size is indicated by number of elements stored and not by KB or similar benchmark! |
public void setStorageCacheSize(int storageCacheSize) | Indicates the size of the storage cache after which the cache won’t grow further Size is indicated by number of elements stored and not by KB or similar benchmark! |
public String getCachePrefix() | A prefix prepended to storage entries to differentiate them |
public void setCachePrefix(String cachePrefix) | A prefix prepended to storage entries to differentiate them |
public boolean isAlwaysStore() | When set to true indicates that all entries should be persisted to storage for a constantly persisting cache |
public void setAlwaysStore(boolean alwaysStore) | When set to true indicates that all entries should be persisted to storage for a constantly persisting cache |
Inherited methods
Constructor details
CacheMap
public CacheMap()Default constructor
CacheMap
public CacheMap(String prefix)Creates a cache map with a prefix string
Parameters
prefixString- string to prepend to the cache entries in storage
Method details
getCacheSize
public int getCacheSize()Indicates the size of the memory cache after which the cache won’t grow further
Size is indicated by number of elements stored and not by KB or similar benchmark!
Returns
the cacheSize
setCacheSize
public void setCacheSize(int cacheSize)Indicates the size of the memory cache after which the cache won’t grow further
Size is indicated by number of elements stored and not by KB or similar benchmark!
Parameters
cacheSizeint- the cacheSize to set
put
public void put(Object key, Object value)Puts the given key/value pair in the cache
Parameters
keyObject- the key
valueObject- the value
delete
public void delete(Object key)Deletes a cached entry
Parameters
keyObject- entry to remove from the cache
get
public Object get(Object key)Returns the object matching the given key
Parameters
keyObject- key object
Returns
value from a previous put or null
clearAllCache
public void clearAllCache()Clears the caches for this cache object
clearMemoryCache
public void clearMemoryCache()Clears the memory cache
getKeysInCache
public Vector getKeysInCache()Returns the keys for all the objects currently in cache, this is useful to traverse all the objects and refresh them without actually deleting the cache and fetching them from scratch.
Important this vector is a copy of a current state, keys might not exist anymore or might change, others might be added in the interim.
Returns
a vector containing a snapshot of the current elements within the
cache.
clearStorageCache
public void clearStorageCache()Clears the storage cache
getStorageCacheSize
public int getStorageCacheSize()Indicates the size of the storage cache after which the cache won’t grow further
Size is indicated by number of elements stored and not by KB or similar benchmark!
Returns
the storageCacheSize
setStorageCacheSize
public void setStorageCacheSize(int storageCacheSize)Indicates the size of the storage cache after which the cache won’t grow further
Size is indicated by number of elements stored and not by KB or similar benchmark!
Parameters
storageCacheSizeint- the storageCacheSize to set
getCachePrefix
public String getCachePrefix()A prefix prepended to storage entries to differentiate them
Returns
the cachePrefix
setCachePrefix
public void setCachePrefix(String cachePrefix)A prefix prepended to storage entries to differentiate them
Parameters
cachePrefixString- the cachePrefix to set
isAlwaysStore
public boolean isAlwaysStore()When set to true indicates that all entries should be persisted to storage
for a constantly persisting cache
Returns
the alwaysStore
setAlwaysStore
public void setAlwaysStore(boolean alwaysStore)When set to true indicates that all entries should be persisted to storage
for a constantly persisting cache
Parameters
alwaysStoreboolean- the alwaysStore to set