From: W. Trevor King Date: Sun, 20 Jan 2013 16:48:11 +0000 (-0500) Subject: CachedLDAPConnection: Drop the old cache on version missmatch X-Git-Tag: v0.1~13 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8b53b6daa87c993f96783c2b70f953ae3c088b66;p=mutt-ldap.git CachedLDAPConnection: Drop the old cache on version missmatch The expected CachedLDAPConnection._cache_version tracks __version__, so if you bump __version__, you invalidate the existing cache. There shouldn't be anything critical in the cache, and this is safer than trying to remember to bump an independent _cache_version after major changes. --- diff --git a/mutt-ldap.py b/mutt-ldap.py index 84c59ee..d8c55a3 100755 --- a/mutt-ldap.py +++ b/mutt-ldap.py @@ -155,6 +155,8 @@ class LDAPConnection (object): class CachedLDAPConnection (LDAPConnection): + _cache_version = '{0}.0'.format(__version__) + def connect(self): self._load_cache() super(CachedLDAPConnection, self).connect() @@ -183,17 +185,25 @@ class CachedLDAPConnection (LDAPConnection): def _load_cache(self): path = _os_path.expanduser(self.config.get('cache', 'path')) + self._cache = {} try: - self._cache = _pickle.load(open(path, 'rb')) + data = _pickle.load(open(path, 'rb')) except IOError: # probably "No such file" - self._cache = {} + pass except (ValueError, KeyError): # probably a corrupt cache file - self._cache = {} + pass + else: + if data.get('version', None) == self._cache_version: + self._cache = data.get('queries', {}) self._cull_cache() def _save_cache(self): path = _os_path.expanduser(self.config.get('cache', 'path')) - _pickle.dump(self._cache, open(path, 'wb')) + data = { + 'queries': self._cache, + 'version': self._cache_version, + } + _pickle.dump(data, open(path, 'wb')) def _cache_store(self, query, entries): self._cache[self._cache_key(query=query)] = {