Skip to content

Commit

Permalink
CacheService.retrieve() not long accepts a max_age param, but returns…
Browse files Browse the repository at this point in the history
… last_modified
  • Loading branch information
bigwheels16 committed Apr 27, 2019
1 parent 6638158 commit 2e73351
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 36 deletions.
9 changes: 3 additions & 6 deletions core/cache_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from core.decorators import instance
from core.dict_object import DictObject
from core.logger import Logger
from pathlib import Path
import os
Expand All @@ -20,18 +21,14 @@ def store(self, group, filename, contents):
with open(base_path + os.sep + filename, "w") as f:
f.write(contents)

def retrieve(self, group, filename, max_age=None):
def retrieve(self, group, filename):
base_path = os.getcwd() + self.CACHE_DIR + os.sep + group

full_path = base_path + os.sep + filename

try:
with open(full_path, "r") as f:
last_modified = int(os.path.getmtime(full_path))
if not max_age or int(time.time()) < last_modified + max_age:
# TODO return obj with last_modified, ex: DictObj({"data": f.read(), "last_modified": last_modified})
return f.read()
else:
return None
return DictObject({"data": f.read(), "last_modified": last_modified})
except FileNotFoundError:
return None
16 changes: 9 additions & 7 deletions core/lookup/character_history_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from requests import ReadTimeout

from core.decorators import instance
Expand All @@ -22,9 +24,11 @@ def inject(self, registry):
def get_character_history(self, name, server_num):
cache_key = "%s.%d.json" % (name, server_num)

t = int(time.time())

# check cache for fresh value
cache_result = self.cache_service.retrieve(self.CACHE_GROUP, cache_key, self.CACHE_MAX_AGE)
if cache_result:
cache_result = self.cache_service.retrieve(self.CACHE_GROUP, cache_key)
if cache_result and cache_result.last_modified > (t - self.CACHE_MAX_AGE):
# TODO set cache age
result = json.loads(cache_result)
else:
Expand All @@ -43,14 +47,12 @@ def get_character_history(self, name, server_num):
if result:
# store result in cache
self.cache_service.store(self.CACHE_GROUP, cache_key, json.dumps(result))
else:
elif cache_result:
# check cache for any value, even expired
# TODO set cache age
cache_obj = self.cache_service.retrieve(self.CACHE_GROUP, cache_key)
if cache_obj:
result = json.loads(cache_obj)
result = json.loads(cache_result)

if result:
# TODO set cache age
return map(lambda x: DictObject(x), result)
else:
return None
18 changes: 10 additions & 8 deletions core/lookup/org_pork_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from requests import ReadTimeout

from core.decorators import instance
Expand Down Expand Up @@ -27,11 +29,13 @@ def inject(self, registry):
def get_org_info(self, org_id):
cache_key = "%d.%d.json" % (org_id, self.bot.dimension)

t = int(time.time())

# check cache for fresh value
cache_result = self.cache_service.retrieve(self.CACHE_GROUP, cache_key, self.CACHE_MAX_AGE)
cache_result = self.cache_service.retrieve(self.CACHE_GROUP, cache_key)

is_cache = False
if cache_result:
if cache_result and cache_result.last_modified > (t - self.CACHE_MAX_AGE):
result = json.loads(cache_result)
is_cache = True
else:
Expand All @@ -42,7 +46,7 @@ def get_org_info(self, org_id):
result = r.json()

# if data is invalid
if not result[0] or not result[0]["ORG_INSTANCE"] or result[0]["ORG_INSTANCE"] != org_id:
if result[0]["ORG_INSTANCE"] != org_id:
result = None
except ReadTimeout:
self.logger.warning("Timeout while requesting '%s'" % url)
Expand All @@ -54,12 +58,10 @@ def get_org_info(self, org_id):
if result:
# store result in cache
self.cache_service.store(self.CACHE_GROUP, cache_key, json.dumps(result))
else:
elif cache_result:
# check cache for any value, even expired
cache_result = self.cache_service.retrieve(self.CACHE_GROUP, cache_key)
if cache_result:
result = json.loads(cache_result)
is_cache = True
result = json.loads(cache_result)
is_cache = True

if not result:
return None
Expand Down
16 changes: 9 additions & 7 deletions modules/standard/aou/aou_controller.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

from core.decorators import instance, command
from core.chat_blob import ChatBlob
from core.command_param_types import Any, Const, Int
Expand Down Expand Up @@ -104,11 +106,13 @@ def aou_search_cmd(self, request, include_all_matches, search):
def retrieve_guide(self, guide_id):
cache_key = "%d.xml" % guide_id

t = int(time.time())

# check cache for fresh value
cache_result = self.cache_service.retrieve(self.CACHE_GROUP, cache_key, self.CACHE_MAX_AGE)
cache_result = self.cache_service.retrieve(self.CACHE_GROUP, cache_key)

if cache_result:
result = ElementTree.fromstring(cache_result)
if cache_result and cache_result.last_modified > (t - self.CACHE_MAX_AGE):
result = ElementTree.fromstring(cache_result.data)
else:
response = requests.get(self.AOU_URL + "&mode=view&id=" + str(guide_id), timeout=5)
result = ElementTree.fromstring(response.content)
Expand All @@ -119,11 +123,9 @@ def retrieve_guide(self, guide_id):
if result:
# store result in cache
self.cache_service.store(self.CACHE_GROUP, cache_key, ElementTree.tostring(result, encoding="unicode"))
else:
elif cache_result:
# check cache for any value, even expired
cache_result = self.cache_service.retrieve(self.CACHE_GROUP, cache_key)
if cache_result:
result = ElementTree.fromstring(cache_result)
result = ElementTree.fromstring(cache_result)

if result:
return self.get_guide_info(result)
Expand Down
12 changes: 4 additions & 8 deletions test/core/cache_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def test_store_overwrite(self):
cache.store("test", "test.txt", "this is a test")
contents = cache.retrieve("test", "test.txt")

self.assertEqual("this is a test", contents)
self.assertEqual("this is a test", contents.data)
cache.store("test", "test.txt", "this is a test2")

contents = cache.retrieve("test", "test.txt")
self.assertEqual("this is a test2", contents)
self.assertEqual("this is a test2", contents.data)

# cleanup files
shutil.rmtree("./data")
Expand All @@ -31,12 +31,8 @@ def test_retrieve_expired(self):
cache = CacheService()
cache.store("test", "test.txt", "this is a test")

contents = cache.retrieve("test", "test.txt", 1)
self.assertEqual("this is a test", contents)

time.sleep(2)
contents = cache.retrieve("test", "test.txt", 1)
self.assertIsNone(contents)
contents = cache.retrieve("test", "test.txt")
self.assertEqual("this is a test", contents.data)

# cleanup files
shutil.rmtree("./data")

0 comments on commit 2e73351

Please sign in to comment.