Class DefaultCache<K,V>

java.lang.Object
com.okta.sdk.impl.cache.DefaultCache<K,V>
All Implemented Interfaces:
Cache<K,V>

public class DefaultCache<K,V> extends Object implements 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 Classes
    Modifier and Type
    Class
    Description
    static class 
    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 Summary

    Constructors
    Constructor
    Description
    Creates a new DefaultCache instance with the specified name, expected to be unique among all other caches in the parent CacheManager.
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static void
     
    protected static void
     
    void
    Removes all entries from this cache.
    get(K key)
    Returns the cached value stored under the specified key or null if there is no cache entry for that key.
    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 of hitCount to accessCount.
    long
    Returns the total number of times an access attempt did not return a cache entry.
    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.
    put(K key, V value)
    Adds a cache entry.
    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
    Returns the total number of cache entries currently available in this cache.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • DefaultCache

      public DefaultCache(String name)
      Creates a new 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).

      Parameters:
      name - the name to assign to this instance, expected to be unique among all other caches in the parent CacheManager.
      See Also:
    • DefaultCache

      public DefaultCache(String name, Map<K,DefaultCache.Entry<V>> backingMap)
      Creates a new 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).

      Parameters:
      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.
      See Also:
    • DefaultCache

      public 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.

      It is expected that the backingMap implementation be thread-safe and preferrably auto-sizing based on memory constraints (see SoftHashMap for such an implementation).

      Parameters:
      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.
      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 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.
      See Also:
  • Method Details

    • assertTtl

      protected static void assertTtl(Duration ttl)
    • assertTti

      protected static void assertTti(Duration tti)
    • get

      public V get(K key)
      Description copied from interface: Cache
      Returns the cached value stored under the specified key or null if there is no cache entry for that key.
      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - the key that the value was previous added with
      Returns:
      the cached object or null if there is no entry for the specified key
    • put

      public V put(K key, V value)
      Description copied from interface: Cache
      Adds a cache entry.
      Specified by:
      put in interface Cache<K,V>
      Parameters:
      key - the key used to identify the object being stored.
      value - the value to be stored in the cache.
      Returns:
      the previous value associated with the given key or null if there was no previous value
    • remove

      public V remove(K key)
      Description copied from interface: Cache
      Removes the cached value stored under the specified key.
      Specified by:
      remove in interface Cache<K,V>
      Parameters:
      key - the key used to identify the object being stored.
      Returns:
      the removed value or null if there was no value cached.
    • getTimeToLive

      public 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. 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

      public 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. 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

      public 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. 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

      public 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. 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 because remove(Object) will return a value, calls to both get(Object) and remove(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 of 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.
      Returns:
      the ratio of hitCount to accessCount.
    • 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

      public String getName()
      Returns this cache instance's name.
      Returns:
      this cache instance's name.
    • toString

      public String toString()
      Overrides:
      toString in class Object