Skip to content

Commit

Permalink
DRAFT: add simple caching in IndexingMiddleware:_document
Browse files Browse the repository at this point in the history
  • Loading branch information
UlrichB22 committed Oct 11, 2024
1 parent 26bd5dc commit 3e933ed
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/moin/storage/middleware/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e933ed

Please sign in to comment.