public class MruCacheStorage extends java.lang.Object implements CacheStorageWithGetSize
StrongCacheStorage
and
SoftCacheStorage
- the effect of both of them can be achieved by
setting one maximum to zero and the other to the largest positive integer.
On the other hand, if you wish to use this storage in a strong-only mode, or
in a soft-only mode, you might consider using StrongCacheStorage
or
SoftCacheStorage
instead, as they can be used by
TemplateCache
concurrently without any synchronization on a 5.0 or
later JRE.
This class is NOT thread-safe. If it's accessed from multiple
threads concurrently, proper synchronization must be provided by the callers.
Note that TemplateCache
, the natural user of this class provides the
necessary synchronizations when it uses the class.
Also you might consider whether you need this sort of a mixed storage at all
in your solution, as in most cases SoftCacheStorage can also be sufficient.
SoftCacheStorage will use Java soft references, and they already use access
timestamps internally to bias the garbage collector against clearing
recently used references, so you can get reasonably good (and
memory-sensitive) most-recently-used caching through
SoftCacheStorage
as well.
Constructor and Description |
---|
MruCacheStorage(int strongSizeLimit,
int softSizeLimit)
Creates a new MRU cache storage with specified maximum cache sizes.
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
java.lang.Object |
get(java.lang.Object key) |
int |
getSize()
Returns a close approximation of the current number of cache entries.
|
int |
getSoftSize()
Returns a close approximation of the current number of soft cache entries.
|
int |
getSoftSizeLimit()
Returns the configured upper limit of the number of soft cache entries.
|
int |
getStrongSize()
Returns the current number of strong cache entries.
|
int |
getStrongSizeLimit()
Returns the configured upper limit of the number of strong cache entries.
|
void |
put(java.lang.Object key,
java.lang.Object value) |
void |
remove(java.lang.Object key) |
public MruCacheStorage(int strongSizeLimit, int softSizeLimit)
Integer.MAX_VALUE
.strongSizeLimit
- the maximum number of strongly referenced templates; when exceeded, the entry used
the least recently will be moved into the soft cache.softSizeLimit
- the maximum number of softly referenced templates; when exceeded, the entry used
the least recently will be discarded.public java.lang.Object get(java.lang.Object key)
get
in interface CacheStorage
public void put(java.lang.Object key, java.lang.Object value)
put
in interface CacheStorage
public void remove(java.lang.Object key)
remove
in interface CacheStorage
public void clear()
clear
in interface CacheStorage
public int getStrongSizeLimit()
public int getSoftSizeLimit()
public int getStrongSize()
getStrongSizeLimit()
public int getSoftSize()
getSoftSizeLimit()
public int getSize()
getSize
in interface CacheStorageWithGetSize
getStrongSize()
,
getSoftSize()