From c395fd478a573288eaab51400f5d7dc4af5d2613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fatih=20A=C5=9F=C4=B1c=C4=B1?= Date: Thu, 13 May 2010 11:23:02 +0000 Subject: [PATCH] db/lazydb: Store a version string to track changes in cache file structure This is needed when new class members added or the type of a variable is changed. --- pisi/db/lazydb.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pisi/db/lazydb.py b/pisi/db/lazydb.py index 7b3771ca..9258d393 100644 --- a/pisi/db/lazydb.py +++ b/pisi/db/lazydb.py @@ -36,6 +36,9 @@ class Singleton(object): del self._the_instances[type(self).__name__] class LazyDB(Singleton): + + cache_version = "2.2" + def __init__(self, cacheable=False, cachedir=None): if not self.__dict__.has_key("initialized"): self.initialized = False @@ -48,8 +51,21 @@ class LazyDB(Singleton): def __cache_file(self): return util.join_path(ctx.config.cache_root_dir(), "%s.cache" % self.__class__.__name__.translate(lower_map)) + def __cache_version_file(self): + return "%s.version" % self.__cache_file() + + def __cache_file_version(self): + try: + return open(self.__cache_version_file()).read().strip() + except IOError: + return "2.2" + def cache_save(self): if os.access(ctx.config.cache_root_dir(), os.W_OK) and self.cacheable: + with open(self.__cache_version_file(), "w") as f: + f.write(LazyDB.cache_version) + f.flush() + os.fsync(f.fileno()) cPickle.dump(self._instance().__dict__, file(self.__cache_file(), 'wb'), 1) @@ -58,6 +74,9 @@ class LazyDB(Singleton): return True if not os.path.exists(self.cachedir): return False + if self.__cache_file_version() != LazyDB.cache_version: + return False + cache_modified = os.stat(self.__cache_file()).st_mtime cache_dir_modified = os.stat(self.cachedir).st_mtime return cache_modified > cache_dir_modified