From 8b53b6daa87c993f96783c2b70f953ae3c088b66 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 20 Jan 2013 11:48:11 -0500 Subject: [PATCH] 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. --- mutt-ldap.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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)] = { -- 2.26.2