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.
This commit is contained in:
parent
aac5e58ec6
commit
8adba38036
1 changed files with 10 additions and 3 deletions
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Reference in a new issue