Package com.okta.sdk.impl.cache
Class DefaultCache<K,V>
java.lang.Object
com.okta.sdk.impl.cache.DefaultCache<K,V>
- All Implemented Interfaces:
Cache<K,
V>
A
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.- Since:
- 0.5.0
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
An Entry is a wrapper that encapsulates the actualvalue
stored in the cache as well ascreationTimeMillis
andlastAccessTimeMillis
metadata about the entry itself. -
Constructor Summary
ConstructorDescriptionDefaultCache
(String name) Creates a newDefaultCache
instance with the specifiedname
, expected to be unique among all other caches in the parentCacheManager
.DefaultCache
(String name, Map<K, DefaultCache.Entry<V>> backingMap) Creates a newDefaultCache
instance with the specifiedname
, storing entries in the specifiedbackingMap
.DefaultCache
(String name, Map<K, DefaultCache.Entry<V>> backingMap, Duration timeToLive, Duration timeToIdle) Creates a newDefaultCache
instance with the specifiedname
, storing entries in the specifiedbackingMap
, using the specifiedtimeToLive
andtimeToIdle
settings. -
Method Summary
Modifier and TypeMethodDescriptionprotected static void
protected static void
void
clear()
Removes all entries from this cache.Returns the cached value stored under the specifiedkey
ornull
if there is no cache entry for thatkey
.long
Returns the number of attempts to return a cache entry.long
Returns the total number of times an access attempt successfully returned a cache entry.double
Returns the ratio ofhitCount
toaccessCount
.long
Returns the total number of times an access attempt did not return a cache entry.getName()
Returns this cache instance's name.Returns the amount of time a cache entry may be idle - unused (not accessed) - before it will expire and no longer be available.Returns the amount of time a cache entry may exist after first being created before it will expire and no longer be available.Adds a cache entry.Removes the cached value stored under the specifiedkey
.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.toString()
-
Constructor Details
-
DefaultCache
Creates a newDefaultCache
instance with the specifiedname
, expected to be unique among all other caches in the parentCacheManager
.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
andtimeToLive
settings are bothnull
, indicating that cache entries will live indefinitely (except due to memory constraints as managed by theSoftHashMap
).- Parameters:
name
- the name to assign to this instance, expected to be unique among all other caches in the parentCacheManager
.- See Also:
-
DefaultCache
Creates a newDefaultCache
instance with the specifiedname
, storing entries in the specifiedbackingMap
. It is expected that thebackingMap
implementation be thread-safe and preferrably auto-sizing based on memory constraints (seeSoftHashMap
for such an implementation).The
timeToIdle
andtimeToLive
settings are bothnull
, indicating that cache entries will live indefinitely (except due to memory constraints as managed by thebackingMap
instance).- Parameters:
name
- name to assign to this instance, expected to be unique among all other caches in the parentCacheManager
.backingMap
- the (ideally thread-safe) map instance to store the Cache entries.- See Also:
-
DefaultCache
public DefaultCache(String name, Map<K, DefaultCache.Entry<V>> backingMap, Duration timeToLive, Duration timeToIdle) Creates a newDefaultCache
instance with the specifiedname
, storing entries in the specifiedbackingMap
, using the specifiedtimeToLive
andtimeToIdle
settings.It is expected that the
backingMap
implementation be thread-safe and preferrably auto-sizing based on memory constraints (seeSoftHashMap
for such an implementation).- Parameters:
name
- name to assign to this instance, expected to be unique among all other caches in the parentCacheManager
.backingMap
- the (ideally thread-safe) map instance to store the Cache entries.timeToLive
- the amount of time cache entries may exist until they should be removed from the cache.timeToIdle
- the amount of time cache entries may remain idle until they should be removed from the cache.- Throws:
IllegalArgumentException
- if eithertimeToLive
ortimeToIdle
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.- See Also:
-
-
Method Details
-
assertTtl
-
assertTti
-
get
Description copied from interface:Cache
Returns the cached value stored under the specifiedkey
ornull
if there is no cache entry for thatkey
. -
put
Description copied from interface:Cache
Adds a cache entry. -
remove
Description copied from interface:Cache
Removes the cached value stored under the specifiedkey
. -
getTimeToLive
Returns the amount of time a cache entry may exist after first being created before it will expire and no longer be available. If a cache entry ever becomes older than this amount of time (regardless of how often it is accessed), it will be removed from the cache as soon as possible.- Returns:
- the amount of time a cache entry may exist after first being created before it will expire and no longer be available.
-
setTimeToLive
Sets the amount of time a cache entry may exist after first being created before it will expire and no longer be available. If a cache entry ever becomes older than this amount of time (regardless of how often it is accessed), it will be removed from the cache as soon as possible.- Parameters:
timeToLive
- the amount of time a cache entry may exist after first being created before it will expire and no longer be available.
-
getTimeToIdle
Returns the amount of time a cache entry may be idle - unused (not accessed) - before it will expire and no longer be available. If a cache entry is not accessed at all after this amount of time, it will be removed from the cache as soon as possible.- Returns:
- the amount of time a cache entry may be idle - unused (not accessed) - before it will expire and no longer be available.
-
setTimeToIdle
Sets the amount of time a cache entry may be idle - unused (not accessed) - before it will expire and no longer be available. If a cache entry is not accessed at all after this amount of time, it will be removed from the cache as soon as possible.- Parameters:
timeToIdle
- the amount of time a cache entry may be idle - unused (not accessed) - before it will expire and no longer be available.
-
getAccessCount
public long getAccessCount()Returns the number of attempts to return a cache entry. Note that becauseremove(Object)
will return a value, calls to bothget(Object)
andremove(Object)
will increment this number.- Returns:
- the number of attempts to return a cache entry
- See Also:
-
getHitCount
public long getHitCount()Returns the total number of times an access attempt successfully returned a cache entry.- Returns:
- the total number of times an access attempt successfully returned a cache entry.
- See Also:
-
getMissCount
public long getMissCount()Returns the total number of times an access attempt did not return a cache entry.- Returns:
- the total number of times an access attempt successfully returned a cache entry.
- See Also:
-
getHitRatio
public double getHitRatio()Returns the ratio ofhitCount
toaccessCount
. The closer this number is to1.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.- Returns:
- the ratio of
hitCount
toaccessCount
.
-
clear
public void clear()Removes all entries from this cache. -
size
public int size()Returns the total number of cache entries currently available in this cache.- Returns:
- the total number of cache entries currently available in this cache.
-
getName
Returns this cache instance's name.- Returns:
- this cache instance's name.
-
toString
-