Interface AccessTokenStore

All Known Implementing Classes:
InMemoryAccessTokenStore

public interface AccessTokenStore
Caches the platform access token of one channel credential.

Channel token providers consult this store before fetching a token from the platform and write each fetched token back. The provider owns the policy — refresh timing, response parsing, and error-code invalidation — and treats the store as an opaque cache slot: one store instance serves one credential, and the store is not keyed, so an instance shared across providers built from different credentials would silently mix their tokens.

The default InMemoryAccessTokenStore keeps the token in the JVM heap, so it is not shared across processes. Deployments running multiple channel instances should provide an implementation backed by shared storage (for example Redis keyed by channel id) and inject it through the channel factories: one instance's refresh or invalidation then serves the whole deployment.

Implementations must be thread-safe: providers invoke these methods from concurrent reactive pipelines. Concurrent refreshes are expected (there is no cross-instance lock); providers store fetched tokens with putIfNewer(io.agentscope.extensions.channel.common.CachedAccessToken) so racing refreshes cannot regress the cache to an earlier refresh deadline, while put(io.agentscope.extensions.channel.common.CachedAccessToken) replaces unconditionally.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Drops the cached token so the owning provider fetches a fresh one on its next call.
    get()
    Returns the cached token.
    void
    Caches token, replacing any previous entry unconditionally.
    default boolean
    Caches token only when it is fresher than the entry currently held (strictly later CachedAccessToken.refreshAtMs()), so racing refreshes — expected, as there is no cross-instance lock — cannot pin an older token with an earlier deadline.
  • Method Details

    • get

      Returns the cached token.

      The store holds the token of a single credential: sharing one instance across providers built from different credentials mixes their tokens.

      Returns:
      the last stored token, or null when none is cached; entries are returned regardless of age — the owning provider applies the refresh deadline carried in CachedAccessToken.refreshAtMs()
    • put

      void put(CachedAccessToken token)
      Caches token, replacing any previous entry unconditionally.

      The store holds the token of a single credential: do not share one instance across providers built from different credentials.

      Parameters:
      token - the token to cache
    • putIfNewer

      default boolean putIfNewer(CachedAccessToken token)
      Caches token only when it is fresher than the entry currently held (strictly later CachedAccessToken.refreshAtMs()), so racing refreshes — expected, as there is no cross-instance lock — cannot pin an older token with an earlier deadline.

      The default implementation is a non-atomic read-compare-write; implementations backed by shared storage should override it with an atomic conditional write (for example a compare-and-set script) to keep the guarantee under concurrency.

      Parameters:
      token - the token to cache; the single-credential rule of put(io.agentscope.extensions.channel.common.CachedAccessToken) applies
      Returns:
      true when token was stored
    • clear

      void clear()
      Drops the cached token so the owning provider fetches a fresh one on its next call.