Skip to content

Commit a043a35

Browse files
committed
Add an example on using SqliteCacheRequestsDocumentLoader with contextloader.
1 parent d3c31c8 commit a043a35

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json
2+
from pathlib import Path
3+
4+
from cachetools import LRUCache
5+
6+
from pyld import ContextResolver, SqliteCacheRequestsDocumentLoader, jsonld
7+
8+
loader = SqliteCacheRequestsDocumentLoader(
9+
sqlite_file_path=Path("/tmp/pyld_example_context_cache.sqlite"),
10+
)
11+
resolver = ContextResolver(LRUCache(maxsize=1000), loader)
12+
13+
doc = {
14+
"@context": "https://schema.org/",
15+
"name": "Example Person",
16+
}
17+
options = {"documentLoader": loader, "contextResolver": resolver}
18+
19+
expanded = jsonld.expand(doc, options=options)
20+
21+
print(json.dumps(expanded, indent=2))

docs/reference/context-resolver.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ documents, so pass the same loader with `documentLoader`.
1616

1717
{{ example('context_resolver.py', output_syntax='json') }}
1818

19+
Use the built-in `SqliteCacheRequestsDocumentLoader` when you want the resolver
20+
to combine PyLD's in-memory resolved-context cache with persistent HTTP caching
21+
for fetched remote contexts.
22+
23+
{{ example('context_resolver_sqlite_cache.py', output_syntax='json') }}
24+
1925
The `shared_cache` object must behave like a mutable mapping. PyLD uses
2026
`cachetools.LRUCache` by default, but applications can provide another mapping
2127
when they need a different eviction policy.

0 commit comments

Comments
 (0)