public class DefaultCache<K,V> extends Object implements Cache<K,V>
DefaultCache
is a Cache
implementation that uses a backing Map
instance to store
and retrieve cached data.
Thread Safety
This implementation is thread-safe only if the backing map is thread-safe.Modifier and Type | Class and Description |
---|---|
static class |
DefaultCache.Entry<V>
An Entry is a wrapper that encapsulates the actual
value stored in the cache as well as
creationTimeMillis and lastAccessTimeMillis
metadata about the entry itself. |
Constructor and Description |
---|
DefaultCache(String name)
Creates a new
DefaultCache instance with the specified name , expected to be unique among all
other caches in the parent CacheManager . |
DefaultCache(String name,
Map<K,DefaultCache.Entry<V>> backingMap)
Creates a new
DefaultCache instance with the specified name , storing entries in the specified
backingMap . |
DefaultCache(String name,
Map<K,DefaultCache.Entry<V>> backingMap,
Duration timeToLive,
Duration timeToIdle)
Creates a new
DefaultCache instance with the specified name , storing entries in the specified
backingMap , using the specified timeToLive and timeToIdle settings. |
Modifier and Type | Method and Description |
---|---|
protected static void |
assertTti(Duration tti) |
protected static void |
assertTtl(Duration ttl) |
void |
clear()
Removes all entries from this cache.
|
V |
get(K key)
Returns the cached value stored under the specified
key or
null if there is no cache entry for that key . |
long |
getAccessCount()
Returns the number of attempts to return a cache entry.
|
long |
getHitCount()
Returns the total number of times an access attempt successfully returned a cache entry.
|
double |
getHitRatio()
Returns the ratio of
hitCount to accessCount . |
long |
getMissCount()
Returns the total number of times an access attempt did not return a cache entry.
|
String |
getName()
Returns this cache instance's name.
|
Duration |
getTimeToIdle()
Returns the amount of time a cache entry may be idle - unused (not accessed) - before it will expire and
no longer be available.
|
Duration |
getTimeToLive()
Returns the amount of time a cache entry may exist after first being created before it will expire and no
longer be available.
|
V |
put(K key,
V value)
Adds a cache entry.
|
V |
remove(K key)
Removes the cached value stored under the specified
key . |
void |
setTimeToIdle(Duration timeToIdle)
Sets the amount of time a cache entry may be idle - unused (not accessed) - before it will expire and
no longer be available.
|
void |
setTimeToLive(Duration timeToLive)
Sets the amount of time a cache entry may exist after first being created before it will expire and no
longer be available.
|
int |
size()
Returns the total number of cache entries currently available in this cache.
|
String |
toString() |
public DefaultCache(String name)
DefaultCache
instance with the specified name
, expected to be unique among all
other caches in the parent CacheManager
.
This constructor uses a SoftHashMap
instance as the cache's backing map, which is thread-safe and
auto-sizes itself based on the application's memory constraints.
Finally, the timeToIdle
and
timeToLive
settings are both null
,
indicating that cache entries will live indefinitely (except due to memory constraints as managed by the
SoftHashMap
).
name
- the name to assign to this instance, expected to be unique among all other caches in the parent
CacheManager
.SoftHashMap
,
setTimeToIdle(Duration)
,
setTimeToLive(Duration)
public DefaultCache(String name, Map<K,DefaultCache.Entry<V>> backingMap)
DefaultCache
instance with the specified name
, storing entries in the specified
backingMap
. It is expected that the backingMap
implementation be thread-safe and preferrably
auto-sizing based on memory constraints (see SoftHashMap
for such an implementation).
The timeToIdle
and
timeToLive
settings are both null
,
indicating that cache entries will live indefinitely (except due to memory constraints as managed by the
backingMap
instance).
name
- name to assign to this instance, expected to be unique among all other caches in the parent
CacheManager
.backingMap
- the (ideally thread-safe) map instance to store the Cache entries.SoftHashMap
,
setTimeToIdle(Duration)
,
setTimeToLive(Duration)
public DefaultCache(String name, Map<K,DefaultCache.Entry<V>> backingMap, Duration timeToLive, Duration timeToIdle)
DefaultCache
instance with the specified name
, storing entries in the specified
backingMap
, using the specified timeToLive
and timeToIdle
settings.
It is expected that the backingMap
implementation be thread-safe and preferrably
auto-sizing based on memory constraints (see SoftHashMap
for such an implementation).
name
- name to assign to this instance, expected to be unique among all other caches in the parent
CacheManager
.backingMap
- the (ideally thread-safe) map instance to store the Cache entries.timeToIdle
- the amount of time cache entries may remain idle until they should be removed from the cache.timeToLive
- the amount of time cache entries may exist until they should be removed from the cache.IllegalArgumentException
- if either timeToLive
or timeToIdle
are non-null and
represent a non-positive (zero or negative) value. This is only enforced for
non-null values - null
values are allowed for either argument.setTimeToIdle(Duration)
,
setTimeToLive(Duration)
protected static void assertTtl(Duration ttl)
protected static void assertTti(Duration tti)
public V get(K key)
Cache
key
or
null
if there is no cache entry for that key
.public V remove(K key)
Cache
key
.public Duration getTimeToLive()
public void setTimeToLive(Duration timeToLive)
timeToLive
- the amount of time a cache entry may exist after first being created before it will expire and
no longer be available.public Duration getTimeToIdle()
public void setTimeToIdle(Duration timeToIdle)
timeToIdle
- the amount of time a cache entry may be idle - unused (not accessed) - before it will expire
and no longer be available.public long getAccessCount()
remove(Object)
will return
a value, calls to both get(Object)
and remove(Object)
will increment this number.getHitCount()
,
getMissCount()
,
getHitRatio()
public long getHitCount()
getMissCount()
,
getHitRatio()
public long getMissCount()
getHitCount()
,
getHitRatio()
public double getHitRatio()
hitCount
to accessCount
. The closer this
number is to 1.0
, the more effectively the cache is being used. The closer this number is to
{code 0.0}, the less effectively the cache is being used.hitCount
to accessCount
.public void clear()
public int size()
public String getName()
Copyright © 2017–2023 Okta. All rights reserved.