Skip to content

Commit

Permalink
Merge pull request #203 from NewFuture/update-cache
Browse files Browse the repository at this point in the history
update cache to resolve #197
  • Loading branch information
NewFuture authored Sep 7, 2020
2 parents 4e0de3d + f126bf8 commit 2cefc75
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 4 additions & 2 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
18 changes: 9 additions & 9 deletions util/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 2cefc75

Please sign in to comment.