diff --git a/src/moin/storage/middleware/indexing.py b/src/moin/storage/middleware/indexing.py index a928050c4..ab10f857d 100644 --- a/src/moin/storage/middleware/indexing.py +++ b/src/moin/storage/middleware/indexing.py @@ -457,6 +457,8 @@ def __init__(self, index_storage, backend, wiki_name=None, acl_rights_contents=[ self.schemas[ALL_REVS] = all_revisions_schema self.schemas[LATEST_REVS] = latest_revisions_schema + self.last_docs = {} + # Define dynamic fields dynamic_fields = [ ("*_id", ID(stored=True)), @@ -932,12 +934,27 @@ def document(self, idx_name=LATEST_REVS, **kw): item = Item(self, latest_doc=latest_doc, itemid=doc[ITEMID]) return item.get_revision(doc[REVID], doc=doc) + @staticmethod + def build_key(idx_name, **kw): + key_str = idx_name + for key, value in kw.items(): + key_str += f" {key} {value}" + return key_str + def _document(self, idx_name=LATEST_REVS, **kw): """ Return a document matching the kw args (internal use only). - """ - with self.ix[idx_name].searcher() as searcher: - return searcher.document(**kw) + cache the 20 latest documents in the self.last_docs dict + """ + key_str = self.build_key(idx_name, **kw) + if key_str not in self.last_docs: + if len(self.last_docs) > 20: + logging.debug(self.last_docs.keys()) + self.last_docs = {} + logging.debug("Cleaning last_docs cache") + with self.ix[idx_name].searcher() as searcher: + self.last_docs[key_str] = searcher.document(**kw) + return self.last_docs[key_str] def has_item(self, name): # TODO: Add fqname support to this method