From 69a5ceb455f34b9af4f9fb2a2b88a5bf0a489dbf Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Wed, 30 May 2018 22:27:07 +0200 Subject: [PATCH] Cache the JSON-LD schemas --- utils/linked_data_sig.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/utils/linked_data_sig.py b/utils/linked_data_sig.py index 9523ed4..834c9bd 100644 --- a/utils/linked_data_sig.py +++ b/utils/linked_data_sig.py @@ -6,6 +6,23 @@ from Crypto.Signature import PKCS1_v1_5 from Crypto.Hash import SHA256 import base64 +from typing import Any, Dict + + +# cache the downloaded "schemas", otherwise the library is super slow +# (https://github.com/digitalbazaar/pyld/issues/70) +_CACHE: Dict[str, Any] = {} +LOADER = jsonld.requests_document_loader() + +def _caching_document_loader(url: str) -> Any: + if url in _CACHE: + return _CACHE[url] + resp = LOADER(url) + _CACHE[url] = resp + return resp + +jsonld.set_document_loader(_caching_document_loader) + def options_hash(doc): doc = dict(doc['signature'])