From 6fd897f7b4699cf5d2834bd201d6adac6be47e61 Mon Sep 17 00:00:00 2001
From: Daniel <d.hornung@indiscale.com>
Date: Thu, 8 Feb 2024 16:21:27 +0100
Subject: [PATCH] ENH: More caching

---
 CHANGELOG.md                             | 1 +
 src/caoscrawler/identifiable_adapters.py | 7 ++++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8eeed54f..13be62db 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 - If a registered identifiable states, that a reference by a Record with parent
   RT1 is needed, then now also references from Records that have a child of RT1
   as parent are accepted.
+- More aggressive caching.
 
 ### Deprecated ###
 
diff --git a/src/caoscrawler/identifiable_adapters.py b/src/caoscrawler/identifiable_adapters.py
index ceaa3bfe..64b9d380 100644
--- a/src/caoscrawler/identifiable_adapters.py
+++ b/src/caoscrawler/identifiable_adapters.py
@@ -27,12 +27,13 @@ from __future__ import annotations
 
 import logging
 from abc import ABCMeta, abstractmethod
+from functools import lru_cache
 from datetime import datetime
 from typing import Any
 
 import caosdb as db
 import yaml
-from caosdb.cached import cached_get_entity_by
+from caosdb.cached import cached_get_entity_by, cached_query
 
 from .identifiable import Identifiable
 from .utils import has_parent
@@ -43,7 +44,7 @@ logger = logging.getLogger(__name__)
 def get_children_of_rt(rtname):
     """Supply the name of a recordtype. This name and the name of all children RTs are returned in
     a list"""
-    return [p.name for p in db.execute_query(f"FIND RECORDTYPE {rtname}")]
+    return [p.name for p in cached_query(f"FIND RECORDTYPE {rtname}")]
 
 
 def convert_value(value: Any):
@@ -510,7 +511,7 @@ class CaosDBIdentifiableAdapter(IdentifiableAdapter):
 
     def retrieve_identified_record_for_identifiable(self, identifiable: Identifiable):
         query_string = self.create_query_for_identifiable(identifiable)
-        candidates = db.execute_query(query_string)
+        candidates = cached_query(query_string)
         if len(candidates) > 1:
             raise RuntimeError(
                 f"Identifiable was not defined unambigiously.\n{query_string}\nReturned the "
-- 
GitLab