CachedLDAPConnection: Drop the old cache on version missmatch
authorW. Trevor King <wking@tremily.us>
Sun, 20 Jan 2013 16:48:11 +0000 (11:48 -0500)
committerW. Trevor King <wking@tremily.us>
Sun, 20 Jan 2013 18:05:19 +0000 (13:05 -0500)
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

index 84c59eea3a9c4d59a7aee2efa7ec5b5b056e233e..d8c55a3958ec66c5d10b8d4b0610e010bd74b1ff 100755 (executable)
@@ -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)] = {