Added error handling on ovm response
This commit is contained in:
17
cache.py
17
cache.py
@@ -5,6 +5,7 @@ import time
|
||||
import json
|
||||
|
||||
from models.location import JsonEncoder
|
||||
from utils.helperutils import log
|
||||
|
||||
cache = {}
|
||||
|
||||
@@ -16,14 +17,14 @@ class Cache():
|
||||
if(not cache):
|
||||
return None
|
||||
if(cacheobj.isOlderThanHours(notOlderThanHours)):
|
||||
print('removing cacheobject ' + key)
|
||||
log('removing cacheobject ' + key)
|
||||
del cache[key]
|
||||
return None
|
||||
print(str(datetime.now()) + ' returning cacheobject ' + key)
|
||||
log( 'returning cacheobject ' + key)
|
||||
return cacheobj.obj
|
||||
|
||||
def add(key, obj):
|
||||
print(str(datetime.now()) + ' adding cacheobject ' + key)
|
||||
log('adding cacheobject ' + key)
|
||||
cacheobj = CacheObj(key, obj)
|
||||
cache[key] = cacheobj
|
||||
|
||||
@@ -35,7 +36,7 @@ class CacheObj:
|
||||
self.time=datetime.now()
|
||||
|
||||
def isOlderThanHours(self, hours):
|
||||
print('checking time cacheobject ' + self.key + ': ' + str(self.time) + " < " + str(datetime.now() - timedelta(hours=hours)))
|
||||
log('checking time cacheobject ' + self.key + ': ' + str(self.time) + " < " + str(datetime.now() - timedelta(hours=hours)))
|
||||
return self.time < datetime.now() - timedelta(hours=hours)
|
||||
|
||||
|
||||
@@ -47,21 +48,21 @@ class FileCache():
|
||||
if cachefile.is_file():
|
||||
ti_m = os.path.getmtime(filepath)
|
||||
#checks last modified age of file, and removes it if it is too old
|
||||
print('checking time cachefile ' + filepath + ': ' + str(ti_m) + " < " + str(time.time() - (3600 * notOlderThanHours)))
|
||||
log('checking time cachefile ' + filepath + ': ' + str(ti_m) + " < " + str(time.time() - (3600 * notOlderThanHours)))
|
||||
if(ti_m < time.time() - (3600 * notOlderThanHours)):
|
||||
print()
|
||||
log('removing old filecache')
|
||||
os.remove(filepath);
|
||||
return None;
|
||||
|
||||
with open(filepath) as json_file:
|
||||
json_data = json.load(json_file);
|
||||
print('returning json data from cachefile: ' + key)
|
||||
log('returning json data from cachefile: ' + key)
|
||||
return json_data;
|
||||
|
||||
return None
|
||||
|
||||
def add(key, obj):
|
||||
print(str(datetime.now()) + ' adding filecacheobject ' + key);
|
||||
log('adding filecacheobject ' + key);
|
||||
json_data = JsonEncoder().encode(obj)
|
||||
with open("./filecache/" + key + ".json", 'w') as f:
|
||||
f.write(json_data)
|
||||
Reference in New Issue
Block a user