SqliteCacheRequestsDocumentLoader¶
Prerequisite
This functionality requires an extra dependency, requests-cache. Install PyLD with:
Remote document loader with persistent SQLite HTTP caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secure
|
bool
|
require all requests to use HTTPS (default: False). |
False
|
sqlite_file_path
|
Path | None
|
absolute path to the |
None
|
When sqlite_file_path is omitted, the cache defaults to the platform user
cache directory:
| OS | Default path |
|---|---|
| Linux | ~/.cache/pyld/http_cache.sqlite |
| macOS | ~/Library/Caches/pyld/http_cache.sqlite |
| Windows | %LOCALAPPDATA%\pyld\http_cache.sqlite |
SqliteCacheRequestsDocumentLoader retrieves remote JSON-LD documents and keeps
them in a persistent SQLite cache. It
is useful for applications that repeatedly load the same remote contexts across
process runs.
HTTP cache headers (Cache-Control, Expires, validators) are honored, so
publishers can control how cached context documents are reused.
JSON-LD Best Practices recommends:
Best Practice 14: Cache JSON-LD Contexts
Services providing a JSON-LD Context SHOULD set HTTP cache-control headers to allow liberal caching of such contexts, and clients SHOULD attempt to use a locally cached version of these documents.
Example sqlite_cache_basic.py
import json
from pathlib import Path
from pyld import SqliteCacheRequestsDocumentLoader, jsonld
doc = {
"@context": {"name": "http://schema.org/name"},
"name": "Earth",
}
loader = SqliteCacheRequestsDocumentLoader(
sqlite_file_path=Path("/tmp/pyld_example_http_cache.sqlite"),
)
# At the first execution, this will perform a network request to https://schema.org,
# but subsequent executions of the same code will not, using a cached response.
result = jsonld.expand(doc, options={"documentLoader": loader})
print(json.dumps(result, indent=2))
Decisions
This page is influenced by the decision to
use requests-cache for persistent HTTP caching in synchronous Python code.