Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
2889:28fe69b87ba4 | 2890:c652d079a9a1 |
---|---|
42 @param profile(unicode, None): profile which *MUST* exists, or None for general values | 42 @param profile(unicode, None): profile which *MUST* exists, or None for general values |
43 """ | 43 """ |
44 if not self.storage: | 44 if not self.storage: |
45 log.error(_("PersistentDict can't be used before memory initialisation")) | 45 log.error(_("PersistentDict can't be used before memory initialisation")) |
46 raise MemoryNotInitializedError | 46 raise MemoryNotInitializedError |
47 self._cache = None | 47 self._cache = {} |
48 self.namespace = namespace | 48 self.namespace = namespace |
49 self.profile = profile | 49 self.profile = profile |
50 | 50 |
51 def _setCache(self, data): | 51 def _setCache(self, data): |
52 self._cache = data | 52 self._cache = data |
109 | 109 |
110 def __getitem__(self, key): | 110 def __getitem__(self, key): |
111 return self._cache.__getitem__(key) | 111 return self._cache.__getitem__(key) |
112 | 112 |
113 def __setitem__(self, key, value): | 113 def __setitem__(self, key, value): |
114 self.storage.setPrivateValue(self.namespace, key, value, self.binary, self.profile) | 114 self.storage.setPrivateValue(self.namespace, key, value, self.binary, |
115 self.profile) | |
115 return self._cache.__setitem__(key, value) | 116 return self._cache.__setitem__(key, value) |
116 | 117 |
117 def __delitem__(self, key): | 118 def __delitem__(self, key): |
118 self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile) | 119 self.storage.delPrivateValue(self.namespace, key, self.binary, self.profile) |
119 return self._cache.__delitem__(key) | 120 return self._cache.__delitem__(key) |
121 | |
122 def clear(self): | |
123 """Delete all values from this namespace""" | |
124 self._cache.clear() | |
125 return self.storage.delPrivateNamespace(self.namespace, self.binary, self.profile) | |
120 | 126 |
121 def get(self, key, default=None): | 127 def get(self, key, default=None): |
122 return self._cache.get(key, default) | 128 return self._cache.get(key, default) |
123 | 129 |
124 def setdefault(self, key, default): | 130 def setdefault(self, key, default): |
131 def force(self, name): | 137 def force(self, name): |
132 """Force saving of an attribute to storage | 138 """Force saving of an attribute to storage |
133 | 139 |
134 @return: deferred fired when data is actually saved | 140 @return: deferred fired when data is actually saved |
135 """ | 141 """ |
136 return self.storage.setPrivateValue(self.namespace, name, self._cache[name], self.binary, self.profile) | 142 return self.storage.setPrivateValue(self.namespace, name, self._cache[name], |
143 self.binary, self.profile) | |
137 | 144 |
138 | 145 |
139 class PersistentBinaryDict(PersistentDict): | 146 class PersistentBinaryDict(PersistentDict): |
140 """Persistent dict where value can be any python data (instead of string only)""" | 147 """Persistent dict where value can be any python data (instead of string only)""" |
141 binary = True | 148 binary = True |