V - data type to be returned by the cache.public class HeapCacheProvider<V> extends AbstractCacheProvider<V>
CacheProvider interface
that uses the local heap as the cache storage.
This implementation uses a LinkedHashMap
as cache engine and the local heap memory as storage.
This class was first implemented using ReadWriteLock
but benchmarks showed that, in this case, synchronization is
6 times faster because the overhead of acquiring the lock is
dominant related to the time requested by read/write operations.
So, even if ReadWriteLocks allow concurrent reads, the time to
acquire the lock is greater than the time spent to perform a synchronized read.
| Constructor and Description |
|---|
HeapCacheProvider()
Default constructor.
|
HeapCacheProvider(int size)
Constructor with parameters.
|
| Modifier and Type | Method and Description |
|---|---|
int |
capacity()
Returns the capacity of the cache i.e. the maximum number of
entries this cache is able to keep.
|
LinkedHashMap |
createStorage()
Returns a new
spooling LinkedHashMap i.e
a LinkedHashMap removing the oldest element
when the capacity has been reached. |
protected void |
doClear()
Removes all keys and related entries from the cache.
|
protected CacheEntry<V> |
doGet(CacheKey key)
Returns the cache entry related to the given key.
|
protected void |
doPut(CacheKey key,
CacheEntry<V> entry,
long duration)
Binds the given entry to the given key and
put it into the underlying cache.
|
protected void |
doRemove(CacheKey key)
Removes the given key and the related entry from the cache.
|
protected boolean |
doTouch(CacheKey key,
long duration)
Defers the expiration time of the cache entry related to the given key.
|
int |
size()
Returns the number of entries in the cache.
|
public static final Logger logger
By default the logging level is Level.INFO and will log exceptions
and creation parameters.
If you want to dig into deeper detail about cache loading and updating operations,
including the data that is going to be cached, you need to set the logging level to Level.FINE.
If you trust this class to work properly and you do not want your logs to get dirty
you can turn off this log by setting the logging level to Level.OFF.
To change the Logger levels and outputs follow the
Java documentation.
public HeapCacheProvider()
public HeapCacheProvider(int size)
size - the number of entries the cache is able to store.public int size()
public int capacity()
public LinkedHashMap createStorage()
spooling LinkedHashMap i.e
a LinkedHashMap removing the oldest element
when the capacity has been reached.SpoolingLinkedHashMapprotected CacheEntry<V> doGet(CacheKey key)
If the given key was never being cached null
will be returned, otherwise will be returned a
CacheEntry containing the cached value
and the expiration time.
doGet in class AbstractCacheProvider<V>key - the cache key to search for.null otherwise.protected boolean doTouch(CacheKey key, long duration)
If no entry is present for the given key a RequirementFailure
should be thrown.
Only the first thread calling this method for the given key should be successful,
any other thread should fail. This method returns true if the operation
was successful and false otherwise.
doTouch in class AbstractCacheProvider<V>key - key to update.duration - number of milliseconds until expiration.true if the related entry has been updated.protected void doPut(CacheKey key, CacheEntry<V> entry, long duration)
If the given key is already present into the cache will be replaced.
doPut in class AbstractCacheProvider<V>key - key to be cached.entry - entry to be cached.duration - number of milliseconds until expiration.protected void doRemove(CacheKey key)
If the key is not present nothing will be done.
doRemove in class AbstractCacheProvider<V>key - key to be removed.protected void doClear()
If cache is already empty nothing will be done.
doClear in class AbstractCacheProvider<V>Copyright © 2011–2020 Nerd4j. All rights reserved.