diff --git a/run.py b/run.py index 2e6a8f76..91a2a710 100755 --- a/run.py +++ b/run.py @@ -11,7 +11,7 @@ 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 98e674b1..50d5da20 100644 --- a/util/cache.py +++ b/util/cache.py @@ -9,7 +9,7 @@ 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 @@ def load(self, file=None): 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 @@ def __update(self): self.__time = time() def clear(self): - if self.data(): + if self.data() is not None: self.__data = {} self.__update()