Class BoundedLruTokenCache

java.lang.Object
org.apache.juneau.rest.server.auth.oauth.BoundedLruTokenCache
All Implemented Interfaces:
TokenCache

public class BoundedLruTokenCache extends Object implements TokenCache
Thread-safe bounded LRU implementation of TokenCache.

Backed by a LinkedHashMap configured with accessOrder=true so iteration order tracks most-recently-used. Eviction policy is the conjunction of:

  • Size cap — when the entry count would exceed maxEntries, the least-recently-used entry is removed before the new entry is inserted. Default maxEntries=1000.
  • TTL eviction — on lookup, an entry whose stored expiry instant has passed (relative to the supplied now clock) is removed and treated as a miss.
Since:
10.0.0
  • Field Details

  • Constructor Details

    • BoundedLruTokenCache

      protected BoundedLruTokenCache(int maxEntries)
      Constructor.
      Parameters:
      maxEntries - The maximum entry count.
  • Method Details

    • create

      public static BoundedLruTokenCache create()
      Static creator with the default max-entries.
      Returns:
      A new cache.
    • create

      public static BoundedLruTokenCache create(int maxEntries)
      Static creator with a custom max-entries.
      Parameters:
      maxEntries - The maximum entry count. Must be positive.
      Returns:
      A new cache.
    • getMaxEntries

      public int getMaxEntries()
      Returns the configured maximum entry count.
      Returns:
      The maximum entry count.
    • putPrincipal

      public void putPrincipal(String key, Principal principal, Duration ttl)
      Description copied from interface: TokenCache
      Stores or replaces a principal entry.
      Specified by:
      putPrincipal in interface TokenCache
      Parameters:
      key - The cache key (typically the opaque token string). Never null.
      principal - The principal to cache. Never null.
      ttl - The maximum cache lifetime relative to "now". Must be positive.
    • getPrincipal

      Description copied from interface: TokenCache
      Retrieves a non-expired principal entry.
      Specified by:
      getPrincipal in interface TokenCache
      Parameters:
      key - The cache key.
      Returns:
      The cached principal, or Optional.empty() if absent or expired.
    • putToken

      public void putToken(String key, OAuthToken token)
      Description copied from interface: TokenCache
      Stores or replaces a token entry.
      Specified by:
      putToken in interface TokenCache
      Parameters:
      key - The cache key (typically a stable key derived from the flow parameters).
      token - The token to cache. Never null.
    • getToken

      public Optional<OAuthToken> getToken(String key, Instant now, Duration skew)
      Description copied from interface: TokenCache
      Retrieves a non-expired token entry.
      Specified by:
      getToken in interface TokenCache
      Parameters:
      key - The cache key.
      now - The reference instant for expiry comparison.
      skew - Skew tolerance subtracted from the entry's expiry. Must be non-negative.
      Returns:
      The cached token, or Optional.empty() if absent or expired.
    • invalidate

      public void invalidate(String key)
      Description copied from interface: TokenCache
      Removes the entry under the supplied key. No-op if absent.
      Specified by:
      invalidate in interface TokenCache
      Parameters:
      key - The cache key.
    • size

      public int size()
      Description copied from interface: TokenCache
      Returns the current entry count. Primarily for tests and metrics.
      Specified by:
      size in interface TokenCache
      Returns:
      The entry count.