From f126bf8f84ae2a8ed3b8325d48f7994721c7997b Mon Sep 17 00:00:00 2001 From: NewFututre Date: Mon, 7 Sep 2020 22:08:22 +0800 Subject: [PATCH] update cache #197 --- run.py | 6 ++++-- util/cache.py | 18 +++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/run.py b/run.py index 2e6a8f7..91a2a71 100755 --- a/run.py +++ b/run.py @@ -11,7 +11,7 @@ from json import load as loadjson, dump as dumpjson from time import ctime from os import path, environ, stat, name as os_name from tempfile import gettempdir -from logging import DEBUG, basicConfig, info, warning, error +from logging import DEBUG, basicConfig, info, warning, error, debug from subprocess import check_output import sys @@ -185,9 +185,11 @@ def main(): cache = get_config('cache', True) and Cache(CACHE_FILE) if cache is False: info("Cache is disabled!") - elif len(cache) < 1 or get_config.time >= cache.time: + elif get_config.time >= cache.time: warning("Cache file is out of dated.") cache.clear() + elif not cache: + debug("Cache is empty.") update_ip('4', cache, dns, proxy_list) update_ip('6', cache, dns, proxy_list) diff --git a/util/cache.py b/util/cache.py index 98e674b..50d5da2 100644 --- a/util/cache.py +++ b/util/cache.py @@ -9,7 +9,7 @@ from os import path, stat from pickle import dump, load from time import time -from logging import info, debug, error +from logging import info, debug, warning try: from collections.abc import MutableMapping @@ -51,17 +51,17 @@ class Cache(MutableMapping): try: self.__data = load(data) self.__time = stat(file).st_mtime + return self except ValueError: - self.__data = {} - self.__time = time() + pass except Exception as e: - error(e) - self.__data = {} - self.__time = time() + warning(e) else: info('cache file not exist') - self.__data = {} - self.__time = time() + + self.__data = {} + self.__time = time() + self.__changed = True return self def data(self, key=None, default=None): @@ -106,7 +106,7 @@ class Cache(MutableMapping): self.__time = time() def clear(self): - if self.data(): + if self.data() is not None: self.__data = {} self.__update()