]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/smb: add ObjectCachingEntry class to config_store.py
authorJohn Mulligan <jmulligan@redhat.com>
Mon, 13 May 2024 20:29:22 +0000 (16:29 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Sat, 6 Jul 2024 14:00:18 +0000 (10:00 -0400)
Add a general store entry class that caches objects so that if an
object needs to be "gotten" from the store multiple times we may
avoid actually calling the store entry get method. This will be
used by a future function for finding particular items in a store.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/pybind/mgr/smb/config_store.py

index 17cae3af44ec0c740707ff113b939482c97f1f37..742c23f6436cef7c4c2f67adfc648ded19dae980 100644 (file)
@@ -1,4 +1,4 @@
-from typing import Collection, Dict, Iterator
+from typing import Collection, Dict, Iterator, Optional
 
 from .proto import ConfigEntry, EntryKey, Simplified
 
@@ -97,3 +97,46 @@ class EntryCache:
 
     def __iter__(self) -> Iterator[EntryKey]:
         return iter(self._entries.keys())
+
+
+class ObjectCachingEntry:
+    """A config entry that wraps a different ConfigEntry and caches the
+    simplified object. If the object is set the cache will be updated. If the
+    object is removed the cached object will be forgotten. The cached object
+    can be manually reset with the `clear_cached_obj` method.
+    """
+
+    def __init__(
+        self, base_entry: ConfigEntry, *, obj: Optional[Simplified] = None
+    ) -> None:
+        self._base = base_entry
+        self._obj = obj
+
+    def clear_cached_obj(self) -> None:
+        self._obj = None
+
+    def set(self, obj: Simplified) -> None:
+        self._obj = None  # if base.set fails, obj will be left unset
+        self._base.set(obj)
+        self._obj = obj
+
+    def get(self) -> Simplified:
+        if self._obj is not None:
+            return self._obj
+        self._obj = self._base.get()
+        return self._obj
+
+    def remove(self) -> bool:
+        self._obj = None
+        return self._base.remove()
+
+    def exists(self) -> bool:
+        return self._base.exists()
+
+    @property
+    def uri(self) -> str:
+        return self._base.uri
+
+    @property
+    def full_key(self) -> EntryKey:
+        return self._base.full_key