From: John Mulligan Date: Mon, 13 May 2024 20:29:22 +0000 (-0400) Subject: mgr/smb: add ObjectCachingEntry class to config_store.py X-Git-Tag: v20.0.0~1489^2~16 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d28d0c41c73530b3fade7967c0fe458762d36443;p=ceph.git mgr/smb: add ObjectCachingEntry class to config_store.py 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 --- diff --git a/src/pybind/mgr/smb/config_store.py b/src/pybind/mgr/smb/config_store.py index 17cae3af44e..742c23f6436 100644 --- a/src/pybind/mgr/smb/config_store.py +++ b/src/pybind/mgr/smb/config_store.py @@ -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