From 426d413b3a559a2a4e63fd997c6843069cd2c321 Mon Sep 17 00:00:00 2001 From: New Future Date: Mon, 5 Dec 2016 11:02:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AF=94=E8=BE=83=E9=85=8D=E7=BD=AE=E5=92=8C?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.py | 8 +++++--- util/cache.py | 28 ++++++++++++++++++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/run.py b/run.py index 25aa380..fcdf376 100755 --- a/run.py +++ b/run.py @@ -25,7 +25,8 @@ def get_config(key=None, default=None, path="config.json"): try: with open(path) as configfile: get_config.config = json.load(configfile) - except IOError: + get_config.time = os.stat(path).st_mtime + except Exception: exit('fail to load config from file: %s' % path) if key: return get_config.config.get(key, default) @@ -45,7 +46,6 @@ def update_ip(ip_type, cache, dns): index = get_config('index' + ip_type) or "default" if str(index).isdigit(): value = getattr(ip, "local_v" + ip_type)(index) - else: value = getattr(ip, index + "_v" + ip_type)() @@ -68,6 +68,7 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument('-c', default="config.json") get_config(path=parser.parse_args().c) + if get_config('dns', 'dnspod').startswith('ali'): dns = alidns else: @@ -77,7 +78,8 @@ def main(): ip.DEBUG = get_config('debug') cache = Cache(CACHE_FILE) - if len(cache) < 1: + if len(cache) < 1 or get_config.time >= cache.time: + cache.clear() print "=" * 25 + " " + time.ctime() + " " + "=" * 25 update_ip('4', cache, dns) diff --git a/util/cache.py b/util/cache.py index 8dde6bc..5390035 100644 --- a/util/cache.py +++ b/util/cache.py @@ -25,6 +25,7 @@ class Cache(MutableMapping): self.__data = {} self.__filename = path self.__sync = sync + self.__time = time.time() self.__changed = False self.load() @@ -72,6 +73,7 @@ class Cache(MutableMapping): with open(self.__filename, 'w') as data: pickle.dump(self.__data, data) LOG.debug('save cache data to %s', self.__filename) + self.__time = time.time() self.__changed = False return self @@ -86,19 +88,27 @@ class Cache(MutableMapping): del self.__time self.__sync = False + def __update(self): + self.__changed = True + if self.__sync: + self.sync() + else: + self.__time = time.time() + + def clear(self): + if self.data(): + self.__data = {} + self.__update() + def __setitem__(self, key, value): if self.data(key) != value: self.__data[key] = value - self.__changed = True - if self.__sync: - self.sync() + self.__update() def __delitem__(self, key): if key in self.data(): del self.__data[key] - self.__changed = True - if self.__sync: - self.sync() + self.__update() def __getitem__(self, key): return self.data(key) @@ -125,13 +135,11 @@ def main(): test """ LOG.basicConfig(level=LOG.DEBUG) - # LOG.debug('test log') cache = Cache('test.txt') - cache['s'] = ['a', 's'] print cache - print cache['s'] print cache.time - cache['t'] = '哈哈' + cache['s'] = 'a' + print cache['s'] print cache.time