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
Nested ClassesModifier and TypeClassDescriptionstatic classAn Entry is a wrapper that encapsulates the actualvaluestored in the cache as well ascreationTimeMillisandlastAccessTimeMillismetadata about the entry itself. -
Constructor Summary
ConstructorsConstructorDescriptionDefaultCache(String name) Creates a newDefaultCacheinstance 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 newDefaultCacheinstance with the specifiedname, storing entries in the specifiedbackingMap.DefaultCache(String name, Map<K, DefaultCache.Entry<V>> backingMap, Duration timeToLive, Duration timeToIdle) Creates a newDefaultCacheinstance with the specifiedname, storing entries in the specifiedbackingMap, using the specifiedtimeToLiveandtimeToIdlesettings. -
Method Summary
Modifier and TypeMethodDescriptionprotected static voidprotected static voidvoidclear()Removes all entries from this cache.longReturns the number of attempts to return a cache entry.longReturns the total number of times an access attempt successfully returned a cache entry.doubleReturns the ratio ofhitCounttoaccessCount.longReturns 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.voidsetTimeToIdle(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.voidsetTimeToLive(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.intsize()Returns the total number of cache entries currently available in this cache.toString()
-
Constructor Details
-
DefaultCache
Creates a newDefaultCacheinstance with the specifiedname, expected to be unique among all other caches in the parentCacheManager.This constructor uses a
SoftHashMapinstance as the cache's backing map, which is thread-safe and auto-sizes itself based on the application's memory constraints.Finally, the
timeToIdleandtimeToLivesettings 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 newDefaultCacheinstance with the specifiedname, storing entries in the specifiedbackingMap. It is expected that thebackingMapimplementation be thread-safe and preferrably auto-sizing based on memory constraints (seeSoftHashMapfor such an implementation).The
timeToIdleandtimeToLivesettings are bothnull, indicating that cache entries will live indefinitely (except due to memory constraints as managed by thebackingMapinstance).- 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 newDefaultCacheinstance with the specifiedname, storing entries in the specifiedbackingMap, using the specifiedtimeToLiveandtimeToIdlesettings.It is expected that the
backingMapimplementation be thread-safe and preferrably auto-sizing based on memory constraints (seeSoftHashMapfor 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 eithertimeToLiveortimeToIdleare non-null and represent a non-positive (zero or negative) value. This is only enforced for non-null values -nullvalues are allowed for either argument.- See Also:
-
-
Method Details
-
assertTtl
-
assertTti
-
get
-
put
-
remove
-
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 ofhitCounttoaccessCount. 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
hitCounttoaccessCount.
-
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
-