mirror of
https://github.com/NewFuture/DDNS.git
synced 2026-10-20 00:16:45 +00:00
比较配置和缓存时间
This commit is contained in:
@@ -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)
|
||||
|
||||
+18
-10
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user