Class SecretCache

java.lang.Object
codes.grow.packages.vault.secrets.SecretCache
All Implemented Interfaces:
SecretsProvider, AutoCloseable

public final class SecretCache extends Object implements SecretsProvider, AutoCloseable
LRU cache in front of a SecretsProvider (SPEC section 3).

Reads (get*, require, exists, getMany) are cached, including negative results. A hit older than ttl * refreshAheadFactor starts one background refresh; within ttl + staleWhileRevalidate a stale value is served while one refresh runs; older entries are reloaded synchronously. Writes pass through and invalidate the name. getAll, list and metadata are not cached.

  • Constructor Details

    • SecretCache

      public SecretCache(SecretsProvider provider)
      Creates a cache with the default options.
      Parameters:
      provider - the provider to cache
    • SecretCache

      public SecretCache(SecretsProvider provider, SecretCache.Options options)
      Creates a cache.
      Parameters:
      provider - the provider to cache
      options - cache options
  • Method Details

    • get

      public Optional<SecretValue> get(String name, GetOptions opts)
      Description copied from interface: SecretsProvider
      Reads a secret.
      Specified by:
      get in interface SecretsProvider
      Parameters:
      name - full secret name
      opts - version / path
      Returns:
      the value, or empty when missing
    • invalidate

      public void invalidate(String name)
      Drops every cached entry of a name (all versions and paths).
      Parameters:
      name - full secret name
    • clear

      public void clear()
      Drops every entry.
    • stats

      public CacheStats stats()
      Returns the counters.
      Returns:
      a snapshot
    • getAll

      public Map<String,SecretValue> getAll(String path)
      Description copied from interface: SecretsProvider
      Returns every secret directly under path (not recursive), sorted by name.
      Specified by:
      getAll in interface SecretsProvider
      Parameters:
      path - folder / prefix; empty for the root
      Returns:
      full name to value
    • list

      public List<SecretMetadata> list(String prefixOrPath)
      Description copied from interface: SecretsProvider
      Lists secrets (names and metadata, never values) sorted by name.
      Specified by:
      list in interface SecretsProvider
      Parameters:
      prefixOrPath - prefix or folder; empty for the root
      Returns:
      the metadata
    • set

      public String set(String name, Object value, SetOptions opts)
      Description copied from interface: SecretsProvider
      Writes a secret. A String is stored as UTF-8, byte[] as is, SecretValue / Secret by their content, anything else JSON-encoded.
      Specified by:
      set in interface SecretsProvider
      Parameters:
      name - full secret name
      value - the value
      opts - path / cas / tags / description
      Returns:
      the new version, or "" when the service has no versions
    • delete

      public void delete(String name, DeleteOptions opts)
      Description copied from interface: SecretsProvider
      Deletes a secret.
      Specified by:
      delete in interface SecretsProvider
      Parameters:
      name - full secret name
      opts - path / versions / permanent
    • metadata

      public Optional<SecretMetadata> metadata(String name, GetOptions opts)
      Description copied from interface: SecretsProvider
      Reads a secret's metadata.
      Specified by:
      metadata in interface SecretsProvider
      Parameters:
      name - full secret name
      opts - version / path
      Returns:
      the metadata, or empty when missing
    • watch

      public WatchHandle watch(String name, Duration interval, WatchListener listener, WatchOptions opts)
      Description copied from interface: SecretsProvider
      Polls a secret and reports changes (version, or SHA-256 of the value when unversioned), including deletion. Errors go to WatchOptions.onError() and polling continues.
      Specified by:
      watch in interface SecretsProvider
      Parameters:
      name - full secret name
      interval - polling interval
      listener - change callback
      opts - error callback
      Returns:
      the handle; close() of the provider stops every watch
    • close

      public void close()
      Clears the cache; later calls raise CancelledException. The wrapped provider stays open (close it separately). Idempotent.
      Specified by:
      close in interface AutoCloseable