Mercurial > libervia-backend
diff sat/memory/persistent.py @ 2890:c652d079a9a1
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.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 07 Apr 2019 18:44:57 +0200 |
parents | 003b8b4b56a7 |
children | ab2696e34d29 |
line wrap: on
line diff
--- a/sat/memory/persistent.py Sun Apr 07 18:38:18 2019 +0200 +++ b/sat/memory/persistent.py Sun Apr 07 18:44:57 2019 +0200 @@ -44,7 +44,7 @@ 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 @@ 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 @@ @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):