class TokenCache: (source)
Constructor: TokenCache(cache_file, path_create_mode, file_create_mode, flock_timeout)
A simple asynchronous file-based token cache.
The cache stores named tokens in a YAML document on disk and uses a file lock to serialize concurrent access. Tokens are automatically filtered for expiration when reading and writing.
| Parameters | |
| cache | Filesystem path to the YAML cache file. When a
string is supplied it is expanded with pathlib.Path.expanduser. |
| path | Mode used when creating parent directories (default 0o750). |
| file | Mode used when creating the cache file (default 0o600). |
| flock | Timeout in seconds to wait for the file lock when accessing the cache. None means wait indefinitely. |
| Method | __init__ |
Create or reference a token cache file. |
| Async Method | get |
Return the token with the given name or None. |
| Async Method | remove |
Remove the token with the specified name from the cache. |
| Async Method | remove |
Remove the token only if its value equals the provided token. |
| Async Method | set |
Store a token in the cache under the given name. |
| Instance Variable | cache |
Path to the YAML cache file. |
| Instance Variable | file |
Mode used when creating the cache file. |
| Instance Variable | flock |
Timeout in seconds to wait for the file lock. None means wait indefinitely. |
| Instance Variable | path |
Mode used when creating parent directories. |
| Method | _yaml |
Serialize tokens to a YAML document. |
| Method | _yaml |
Parse YAML content and return a mapping of token name -> Token. |
str | Path = Path(int = 488, file_create_mode: int = 384, flock_timeout: float | None = 5.0):
(source)
¶
Create or reference a token cache file.
Return the token with the given name or None.
The function opens the cache file under a shared/read lock and parses the YAML content. If the token is present and not expired it is returned. Expired tokens are removed from the cache.
| Parameters | |
name:str | Token name to lookup. |
| Returns | |
Token | None | Token instance or None when not found or expired. |
Remove the token with the specified name from the cache.
The function opens the cache under an exclusive lock, removes the entry when present and writes the updated YAML document back to disk.
| Parameters | |
name:str | Token name to remove. |
| Raises | |
ValueError | If the cache cannot be parsed or written. |
Remove the token only if its value equals the provided token.
The comparison uses Token.__eq__.
| Parameters | |
name:str | Token name to remove. |
token:Token | Token value to compare against. |
| Raises | |
ValueError | If the cache cannot be parsed or written. |
Store a token in the cache under the given name.
The cache file and its parent directory are created with the configured permissions if necessary. The function acquires an exclusive lock while reading and writing the YAML document.
| Parameters | |
name:str | Name under which to store the token. |
token:Token | Token instance to store. |
| Raises | |
ValueError | If the token cannot be serialized or written. |
Parse YAML content and return a mapping of token name -> Token.
The function expects a YAML mapping with a top-level tokens key
containing a mapping of string names to token dictionaries produced
by Token.to_dict.
| Parameters | |
data:str | YAML document as a string. |
| Returns | |
dict[ | Mapping from token name to Token. |
| Raises | |
ValueError | When the YAML is invalid or has an unexpected structure. |