From d300e77affd755514ab0695d9e816d187b2862ea Mon Sep 17 00:00:00 2001 From: Faik Uygur Date: Wed, 15 Jul 2009 17:28:02 +0000 Subject: [PATCH] Check installdb cache's validity before using cache file. --- pisi/db/installdb.py | 2 +- pisi/db/lazydb.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pisi/db/installdb.py b/pisi/db/installdb.py index 583cf65c..914c1b52 100644 --- a/pisi/db/installdb.py +++ b/pisi/db/installdb.py @@ -65,7 +65,7 @@ class InstallInfo: class InstallDB(lazydb.LazyDB): def __init__(self): - lazydb.LazyDB.__init__(self, cacheable=True) + lazydb.LazyDB.__init__(self, cacheable=True, cachedir=ctx.config.packages_dir()) def init(self): self.installed_db = self.__generate_installed_pkgs() diff --git a/pisi/db/lazydb.py b/pisi/db/lazydb.py index 8a546ee9..a8475e0f 100644 --- a/pisi/db/lazydb.py +++ b/pisi/db/lazydb.py @@ -11,6 +11,8 @@ # import os +import time +import stat import cPickle import time import pisi.context as ctx @@ -35,10 +37,11 @@ class Singleton(object): del self._the_instances[type(self).__name__] class LazyDB(Singleton): - def __init__(self, cacheable=False): + def __init__(self, cacheable=False, cachedir=None): if not self.__dict__.has_key("initialized"): self.initialized = False self.cacheable = cacheable + self.cachedir = cachedir def is_initialized(self): return self.initialized @@ -51,8 +54,18 @@ class LazyDB(Singleton): cPickle.dump(self._instance().__dict__, file(self.__cache_file(), 'wb'), 1) + def cache_valid(self): + if not self.cachedir: + return True + if not os.path.exists(self.cachedir): + return False + cache_modified = os.stat(self.__cache_file()).st_mtime + cache_dir_modified = os.stat(self.cachedir).st_mtime + valid = cache_modified > cache_dir_modified + return valid + def cache_load(self): - if os.path.exists(self.__cache_file()): + if os.path.exists(self.__cache_file()) and self.cache_valid(): try: self._instance().__dict__ = cPickle.load(file(self.__cache_file(), 'rb')) return True