From 8adba38036bfc73554ce48d5a567975070cf58ab Mon Sep 17 00:00:00 2001 From: Goffi Date: Sun, 7 Apr 2019 18:44:57 +0200 Subject: [PATCH] core (memory/persistent): added clear method + initialise with a dict: A "clear" method has been added to delete all data for a specific namespace (and profile). Internal cache (self._cache) is not initialised with an empty dict instead of None to allow using methods like __setitem__ or clear without having to do a potentially expansive `load()` before. The only counterpart is that we won't have an exception anymore if developer forget to load() data before using them, which can make debugging a bit more difficult. --- sat/memory/persistent.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sat/memory/persistent.py b/sat/memory/persistent.py index b939ae3b..c0425190 100644 --- a/sat/memory/persistent.py +++ b/sat/memory/persistent.py @@ -44,7 +44,7 @@ class PersistentDict(object): if not self.storage: log.error(_("PersistentDict can't be used before memory initialisation")) raise MemoryNotInitializedError - self._cache = None + self._cache = {} self.namespace = namespace self.profile = profile @@ -111,13 +111,19 @@ class PersistentDict(object): return self._cache.__getitem__(key) def __setitem__(self, key, value): - self.storage.setPrivateValue(self.namespace, key, value, self.binary, self.profile) + self.storage.setPrivateValue(self.namespace, key, value, self.binary, + self.profile) return self._cache.__setitem__(key, value) def __delitem__(self, key): self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile) return self._cache.__delitem__(key) + def clear(self): + """Delete all values from this namespace""" + self._cache.clear() + return self.storage.delPrivateNamespace(self.namespace, self.binary, self.profile) + def get(self, key, default=None): return self._cache.get(key, default) @@ -133,7 +139,8 @@ class PersistentDict(object): @return: deferred fired when data is actually saved """ - return self.storage.setPrivateValue(self.namespace, name, self._cache[name], self.binary, self.profile) + return self.storage.setPrivateValue(self.namespace, name, self._cache[name], + self.binary, self.profile) class PersistentBinaryDict(PersistentDict):