When performing canonization of a JSON-LD document, it is necessary to perform compact, expand and normalise operations. This would involve resolving multiple remote contexts. Since the contexts are located in a remote host, the availability of the resource cannot be garunteed. Fetching a remote resource adds significant latency if the remote server doesn't respond fast.
Implement a cache for remote contexts. This can be done by caching the responses from the document loader used by PyLD. A simple cache snippet using python dictionary for PyLD is given below.
from pyld import jsonld
cache = {}
def caching_document_loader(url, options):
loader = jsonld.requests_document_loader()
if url in cache:
return cache[url]
resp = loader(url)
cache[url] = resp
return resp
jsonld.set_document_loader(caching_document_loader)