niquests-cache¶
Cached niquests sessions with pluggable storage backends.
Example usage¶
The cached_session helper returns a
CachedSession or
AsyncCachedSession whose cache database is
platformdirs.user_cache_path(app_name, appauthor=False) / 'http.sqlite'. If you omit
app_name, niquests-cache is used. Only successful GET and HEAD responses are
stored; the default TTL is 10 minutes (expire_after= on the helper, or per request—see
below).
Sync helper (default application name and TTL):
from niquests_cache import cached_session
session = cached_session()
response = session.get('https://httpbin.org/get')
response.raise_for_status()
Custom app_name for platformdirs.user_cache_path:
from niquests_cache import cached_session
session = cached_session(app_name='my-application')
response = session.get('https://httpbin.org/get')
response.raise_for_status()
Plain niquests session with no caching:
from niquests_cache import cached_session
session = cached_session(no_cache=True)
Construct CachedSession when you need an explicit cache name or backend:
from pathlib import Path
from niquests_cache import CachedSession
with CachedSession(cache_name=Path('.cache') / 'http') as session:
response = session.get('https://httpbin.org/get')
response.raise_for_status()
Async helper (async context manager):
import asyncio
from datetime import timedelta
from niquests_cache import cached_session
async def main() -> None:
async with cached_session(aio=True, expire_after=timedelta(minutes=30)) as session:
response = await session.get('https://httpbin.org/get')
response.raise_for_status()
asyncio.run(main())
Or construct AsyncCachedSession directly:
import asyncio
from pathlib import Path
from niquests_cache import AsyncCachedSession
async def main() -> None:
async with AsyncCachedSession(cache_name=Path('.cache') / 'http') as session:
response = await session.get('https://httpbin.org/get')
response.raise_for_status()
asyncio.run(main())
Pick a non-default backend by alias or instance:
from niquests_cache import CachedSession
from niquests_cache.backends import FileCache, MemoryBackend
# Built-in aliases: 'sqlite' (default), 'filesystem', 'memory'.
session = CachedSession(backend='filesystem', cache_name='./fs-cache')
# Or pass a backend instance directly.
session = CachedSession(backend=MemoryBackend())
# FileCache with a non-default serialiser:
session = CachedSession(backend=FileCache('./fs-cache', serializer='pickle'))
To bypass the cache read for one request, pass force_refresh=True to request(); to return
a synthesised 504 when no cached entry exists, pass only_if_cached=True.
API reference¶
Cached niquests sessions matching requests_cache.CachedSession’s API.
-
class niquests_cache.session.AsyncCachedSession(cache_name: StrPath =
'http_cache', backend: BaseBackend | BackendAlias | None =None, *, serializer: Serializer | str | None =None, expire_after: ExpireAfter =-1, urls_expire_after: Mapping[str | re.Pattern[str], ExpireAfter] | None =None, cache_control: bool =False, content_root_key: str | None =None, allowable_codes: Iterable[int] =(200,), allowable_methods: Iterable[str] =('GET', 'HEAD'), always_revalidate: bool =False, ignored_parameters: Iterable[str] =('Authorization', 'If-Modified-Since', 'If-None-Match', 'X-API-KEY', 'access_token', 'api_key'), match_headers: bool | Iterable[str] =False, filter_fn: collections.abc.Callable[..., bool] | None =None, key_fn: collections.abc.Callable[..., str] | None =None, read_only: bool =False, stale_if_error: bool | int =False, autoclose: bool =True, **kwargs: Any)¶ Asynchronous cached
niquestssession.- cache_disabled() AsyncIterator[None]¶
Temporarily disable caching for the duration of the
async withblock.- Yields:¶
None – Control returns to the caller while caching is disabled.
-
async request(method: str, url: str, *args: Any, headers: Mapping[str, str] | None =
None, expire_after: ExpireAfter =None, only_if_cached: bool =False, refresh: bool =False, force_refresh: bool =False, **kwargs: Any) niquests.Response¶ Send an async request, returning a cached response when available.
- Parameters:¶
- method : str¶
The HTTP method.
- url : str¶
The URL.
- *args : Any¶
Positional arguments forwarded to the parent.
- headers : Mapping[str, str] | None¶
Per-request headers.
- expire_after : ExpireAfter¶
Per-request override of the cache expiry.
- only_if_cached : bool¶
If
True, return a synthesised504when the entry is missing.- refresh : bool¶
Currently treated like
force_refresh; reserved for revalidation.- force_refresh : bool¶
If
True, bypass the cache read and replace the stored entry.- **kwargs : Any¶
Additional keyword arguments forwarded to the parent.
- Returns:¶
The HTTP response.
- Return type:¶
-
class niquests_cache.session.CacheMixin(cache_name: StrPath =
'http_cache', backend: BaseBackend | BackendAlias | None =None, *, serializer: Serializer | str | None =None, expire_after: ExpireAfter =-1, urls_expire_after: Mapping[str | re.Pattern[str], ExpireAfter] | None =None, cache_control: bool =False, content_root_key: str | None =None, allowable_codes: Iterable[int] =(200,), allowable_methods: Iterable[str] =('GET', 'HEAD'), always_revalidate: bool =False, ignored_parameters: Iterable[str] =('Authorization', 'If-Modified-Since', 'If-None-Match', 'X-API-KEY', 'access_token', 'api_key'), match_headers: bool | Iterable[str] =False, filter_fn: collections.abc.Callable[..., bool] | None =None, key_fn: collections.abc.Callable[..., str] | None =None, read_only: bool =False, stale_if_error: bool | int =False, autoclose: bool =True, **kwargs: Any)¶ Shared state and helpers between sync and async cached sessions.
- property backend : BaseBackend¶
Alias for
cache.
- property cache : BaseBackend¶
The storage backend in use.
- settings : CacheSettings¶
Mutable cache settings for this session.
-
class niquests_cache.session.CachedSession(cache_name: StrPath =
'http_cache', backend: BaseBackend | BackendAlias | None =None, *, serializer: Serializer | str | None =None, expire_after: ExpireAfter =-1, urls_expire_after: Mapping[str | re.Pattern[str], ExpireAfter] | None =None, cache_control: bool =False, content_root_key: str | None =None, allowable_codes: Iterable[int] =(200,), allowable_methods: Iterable[str] =('GET', 'HEAD'), always_revalidate: bool =False, ignored_parameters: Iterable[str] =('Authorization', 'If-Modified-Since', 'If-None-Match', 'X-API-KEY', 'access_token', 'api_key'), match_headers: bool | Iterable[str] =False, filter_fn: collections.abc.Callable[..., bool] | None =None, key_fn: collections.abc.Callable[..., str] | None =None, read_only: bool =False, stale_if_error: bool | int =False, autoclose: bool =True, **kwargs: Any)¶ Synchronous cached
niquestssession.- cache_disabled() Iterator[None]¶
Temporarily disable caching for the duration of the
withblock.- Yields:¶
None – Control returns to the caller while caching is disabled.
-
request(method: str, url: str, *args: Any, headers: Mapping[str, str] | None =
None, expire_after: ExpireAfter =None, only_if_cached: bool =False, refresh: bool =False, force_refresh: bool =False, **kwargs: Any) niquests.Response¶ Send a request, returning a cached response when one is available.
- Parameters:¶
- method : str¶
The HTTP method.
- url : str¶
The URL.
- *args : Any¶
Positional arguments forwarded to the parent.
- headers : Mapping[str, str] | None¶
Per-request headers.
- expire_after : ExpireAfter¶
Per-request override of the cache expiry.
- only_if_cached : bool¶
If
True, return a synthesised504when the entry is missing.- refresh : bool¶
Currently treated like
force_refresh; reserved for revalidation.- force_refresh : bool¶
If
True, bypass the cache read and replace the stored entry.- **kwargs : Any¶
Additional keyword arguments forwarded to the parent.
- Returns:¶
The HTTP response.
- Return type:¶
-
niquests_cache.session.cached_session(*, aio: False =
False, no_cache: True, app_name: str | None =None, backend: BaseBackend | BackendAlias | None =None, expire_after: timedelta =datetime.timedelta(seconds=600)) Session¶ -
niquests_cache.session.cached_session(*, aio: True, no_cache: True, app_name: str | None =
None, backend: BaseBackend | BackendAlias | None =None, expire_after: timedelta =datetime.timedelta(seconds=600)) AsyncSession -
niquests_cache.session.cached_session(*, aio: True, no_cache: False =
False, app_name: str | None =None, backend: BaseBackend | BackendAlias | None =None, expire_after: timedelta =datetime.timedelta(seconds=600)) AsyncCachedSession -
niquests_cache.session.cached_session(*, aio: False =
False, no_cache: False =False, app_name: str | None =None, backend: BaseBackend | BackendAlias | None =None, expire_after: timedelta =datetime.timedelta(seconds=600)) CachedSession -
niquests_cache.session.cached_session(*, aio: False =
False, no_cache: bool =False, app_name: str | None =None, backend: BaseBackend | BackendAlias | None =None, expire_after: timedelta =datetime.timedelta(seconds=600)) Session -
niquests_cache.session.cached_session(*, aio: True, no_cache: bool =
False, app_name: str | None =None, backend: BaseBackend | BackendAlias | None =None, expire_after: timedelta =datetime.timedelta(seconds=600)) AsyncSession Get a niquests session, optionally with SQLite-backed caching.
The default cache database lives at
<user cache path>/http.sqlite.- Parameters:¶
- aio : bool¶
If
True, return an async session; otherwise a synchronous session.- no_cache : bool¶
If
True, return a plain session without caching.- app_name : str | None¶
First argument to
platformdirs.user_cache_path()for the cache root. IfNone, usesniquests-cache.- backend : BaseBackend | BackendAlias | None¶
Storage backend. May be a
BaseBackendinstance, an alias ('sqlite','filesystem','memory'), orNone(default) to use SQLite at the user-cache path derived fromapp_name.- expire_after : timedelta¶
Cache expiry duration (ignored when
no_cacheisTrue).
- Returns:¶
A plain
SessionorAsyncSessionwhenno_cacheisTrue(depending onaio); otherwise aCachedSessionorAsyncCachedSession. Use an async context manager whenaioisTrue.- Return type:¶
CachedSession | AsyncCachedSession | niquests.Session | niquests.AsyncSession
Abstract base class for cache backends.
- class niquests_cache.backends.base.BaseBackend¶
Abstract storage backend used by
CachedSession.- async aset(key: str, entry: CacheEntry) None¶
Async
set(). Default implementation calls the sync version.- Parameters:¶
- key : str¶
The cache key.
- entry : CacheEntry¶
The entry to store.
- abstractmethod get(key: str) CacheEntry | None¶
Look up a cached entry.
Filesystem cache backend matching the requests_cache.FileCache public API.
-
class niquests_cache.backends.file.FileCache(cache_name: StrPath =
'http_cache', *, use_temp: bool =False, use_cache_dir: bool =False, extension: str ='', lock: AbstractContextManager[Any] | None =None, serializer: Serializer | str | None =None, **kwargs: Any)¶ Cache responses as serialised files in a directory.
- async aset(key: str, entry: CacheEntry) None¶
Async
set()usinganyiofor non-blocking I/O.- Parameters:¶
- key : str¶
The cache key.
- entry : CacheEntry¶
The entry to store.
- get(key: str) CacheEntry | None¶
Look up a cached entry.
- property serializer : Serializer¶
The serialiser used to encode entries to bytes.
In-memory cache backend.
- class niquests_cache.backends.memory.MemoryBackend¶
Cache responses in an in-process dictionary.
- get(key: str) CacheEntry | None¶
Look up a cached entry.
SQLite cache backend storing entries in typed columns.
-
class niquests_cache.backends.sqlite.SQLiteBackend(database: StrPath =
':memory:', *, table_name: str ='niquests_cache')¶ Cache responses in a SQLite database with typed columns.
- async aget(key: str) CacheEntry | None¶
- async aset(key: str, entry: CacheEntry) None¶
-
- Parameters:¶
- key : str¶
The cache key.
- entry : CacheEntry¶
The entry to store.
- property database : StrPath¶
SQLite database path (or
':memory:').
- get(key: str) CacheEntry | None¶
Look up a cached entry.
Built-in serialisers for cache entries.
- class niquests_cache.serializers.JSONSerializer¶
Encodes
CacheEntryas UTF-8 JSON. Binarycontentis base64-encoded.- dumps(entry: CacheEntry) bytes¶
Serialise
entryto JSON-encoded bytes.
- loads(data: bytes) CacheEntry¶
Deserialise a JSON-encoded payload.
- class niquests_cache.serializers.PickleSerializer¶
Encodes
CacheEntryusingpickle.- dumps(entry: CacheEntry) bytes¶
Serialise
entryusingpickle.dumps().
- loads(data: bytes) CacheEntry¶
Deserialise a pickled payload.
- niquests_cache.serializers.resolve_serializer(value: str | Serializer | None) Serializer¶
Resolve a
serializerargument.
Cache settings dataclass for CachedSession.
-
class niquests_cache.settings.CacheSettings(allowable_codes: tuple[int, ...] =
(200,), allowable_methods: tuple[str, ...] =('GET', 'HEAD'), always_revalidate: bool =False, autoclose: bool =True, cache_control: bool =False, content_root_key: str | None =None, disabled: bool =False, expire_after: ExpireAfter =-1, filter_fn: collections.abc.Callable[..., bool] | None =None, ignored_parameters: tuple[str, ...] =('Authorization', 'X-API-KEY', 'access_token', 'api_key'), key_fn: collections.abc.Callable[..., str] | None =None, match_headers: bool | tuple[str, ...] =False, read_only: bool =False, serializer: Serializer | None =None, stale_if_error: bool | int =False, urls_expire_after: Mapping[str | re.Pattern[str], ExpireAfter] | None =None)¶ Mutable settings controlling
CachedSessionbehaviour.-
cache_control : bool =
False¶ Honour
Cache-Controlresponse headers (currently stored, not enforced).
-
expire_after : ExpireAfter =
-1¶ Default TTL;
-1orNonemeans never expire.
-
filter_fn : Callable[..., bool] | None =
None¶ Predicate deciding whether a response should be cached.
-
ignored_parameters : tuple[str, ...] =
('Authorization', 'X-API-KEY', 'access_token', 'api_key')¶ Header / query-parameter names removed before computing the cache key.
-
match_headers : bool | tuple[str, ...] =
False¶ only those names.
- Type:¶
Falseignores headers;Trueincludes all; iterable
-
serializer : Serializer | None =
None¶ Resolved serialiser used by the session to encode and decode entries.
-
urls_expire_after : Mapping[str | re.Pattern[str], ExpireAfter] | None =
None¶ Per-URL-pattern TTL overrides.
-
cache_control : bool =
Typing helpers for niquests_cache.
- niquests_cache.typing.BackendAlias¶
Built-in backend alias names accepted by
CachedSession.alias of
Literal[‘sqlite’, ‘memory’, ‘filesystem’]
- class niquests_cache.typing.CacheEntry¶
A cached HTTP response entry.
-
niquests_cache.typing.ExpireAfter : TypeAlias =
int | float | str | datetime.datetime | datetime.timedelta | None¶ Accepted types for the
expire_afterparameter.
- class niquests_cache.typing.Serializer(*args, **kwargs)¶
Structural type for serialiser objects with
dumps/loadsmethods.- dumps(entry: CacheEntry) bytes¶
Serialise a
CacheEntryto bytes.
- loads(data: bytes) CacheEntry¶
Deserialise bytes back into a
CacheEntry.