Interface AccessTokenStore
- All Known Implementing Classes:
InMemoryAccessTokenStore
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 TypeMethodDescriptionvoidclear()Drops the cached token so the owning provider fetches a fresh one on its next call.get()Returns the cached token.voidput(CachedAccessToken token) Cachestoken, replacing any previous entry unconditionally.default booleanputIfNewer(CachedAccessToken token) Cachestokenonly when it is fresher than the entry currently held (strictly laterCachedAccessToken.refreshAtMs()), so racing refreshes — expected, as there is no cross-instance lock — cannot pin an older token with an earlier deadline.
-
Method Details
-
get
CachedAccessToken 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
nullwhen none is cached; entries are returned regardless of age — the owning provider applies the refresh deadline carried inCachedAccessToken.refreshAtMs()
-
put
Cachestoken, 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
Cachestokenonly when it is fresher than the entry currently held (strictly laterCachedAccessToken.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 ofput(io.agentscope.extensions.channel.common.CachedAccessToken)applies- Returns:
truewhentokenwas stored
-
clear
void clear()Drops the cached token so the owning provider fetches a fresh one on its next call.
-