Authorization context cache
rn_forge.auth.context_cache
In-process cache of authorization contexts.
AccountStateWriter
Bases: Protocol
Persists the account state that an authorization context carries.
bump_session_version(account_id)
Increase the session version of account_id and return the new one.
set_active(account_id, active)
Store whether account_id may authenticate.
AuthorizationContextCache(provider, *, ttl=timedelta(seconds=60), versions=None, writer=None, clock=time.monotonic)
An :class:AuthorizationContextProvider that caches another one.
Each account has one entry, valid while the account and global versions are
those it was computed at and it is younger than ttl. Changes made
through :meth:invalidate, :meth:invalidate_all, :meth:set_active and
:meth:bump_session_version apply on the next :meth:resolve. A change
made any other way applies once the entry's ttl has passed. The cache is
safe to share between threads and is local to the process.
Expired entries are kept until the next miss. The number held is bounded by the accounts that missed within one ttl. Version counters are not pruned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider
|
AuthorizationContextProvider
|
The provider that computes contexts. |
required |
ttl
|
timedelta
|
The longest an entry is served. Must be positive. |
timedelta(seconds=60)
|
versions
|
AuthorizationVersions | None
|
The version counters. Defaults to a new
:class: |
None
|
writer
|
AccountStateWriter | None
|
Persists account state for :meth: |
None
|
clock
|
Callable[[], float]
|
Returns the current time in seconds, monotonic. |
monotonic
|
Raises:
| Type | Description |
|---|---|
ValueError
|
ttl is not positive. |
bump_session_version(account_id)
Increase the account's session version and invalidate its context.
Returns:
| Type | Description |
|---|---|
int
|
The new session version. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
No writer was configured. |
invalidate(account_id)
Make the next :meth:resolve of account_id recompute.
invalidate_all()
Make the next :meth:resolve of every account recompute.
resolve(account_id)
Return the context of account_id, computing it on a miss.
set_active(account_id, active)
Store the account's active flag and invalidate its context.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
No writer was configured. |
AuthorizationVersions
Bases: Protocol
Counters that say when cached authorization contexts are stale.
bump(account_id)
Increase the version of account_id.
bump_all()
Increase the global version, which every account shares.
current(account_id)
Return the account's version and the global version.
InMemoryAuthorizationVersions()
:class:AuthorizationVersions held in process memory.
Thread-safe. The counters are not shared across processes, so a bump is seen only by caches that use the same instance.
bump(account_id)
Increase the version of account_id.
bump_all()
Increase the global version.
current(account_id)
Return the account's version and the global version.