Skip to content

Commit

Permalink
Write cache data in user space learningequality#4109
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaoming committed Jul 20, 2015
1 parent 8abd359 commit c69812c
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions kalite/topic_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@
logging.warning("Channel {channel} does not exist.".format(channel=django_settings.CHANNEL))


def cache_file_path(basename):
"""Consistently return path for a cache filename. This path has to be
writable for the user running kalite."""
assert "/" not in basename, "Please use a valid filename"
cache_dir = os.path.join(django_settings.USER_DATA_ROOT, 'cache')
os.makedirs(cache_dir)
return os.path.join(cache_dir, basename + '.json')


# Globals that can be filled
TOPICS = None
CACHE_VARS.append("TOPICS")
Expand Down Expand Up @@ -75,7 +84,11 @@ def get_topic_tree(force=False, annotate=False, channel=None, language=None, par

if annotate:
if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force:
topics = softload_json(settings.TOPICS_FILEPATHS.get(channel) + "_" + language + ".cache", logger=logging.debug, raises=False)
topics = softload_json(
cache_file_path("topic_{0}_{1}.json".format(channel, language)),
logger=logging.debug,
raises=False
)
if topics:
TOPICS[channel][language] = topics
return TOPICS[channel][language]
Expand Down Expand Up @@ -130,7 +143,7 @@ def recurse_nodes(node, parent=""):

if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP:
try:
with open(settings.TOPICS_FILEPATHS.get(channel) + "_" + language + ".cache", "w") as f:
with open(cache_file_path("topic_{0}_{1}.json".format(channel, language)), "w") as f:
json.dump(TOPICS[channel][language], f)
except IOError as e:
logging.warn("Annotated topic cache file failed in saving with error {e}".format(e=e))
Expand Down Expand Up @@ -168,12 +181,18 @@ def get_exercise_cache(force=False, language=None):
EXERCISES = {}
if EXERCISES.get(language) is None:
if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force:
exercises = softload_json(settings.EXERCISES_FILEPATH + "_" + language + ".cache", logger=logging.debug, raises=False)
exercises = softload_json(
cache_file_path("exercises_{1}.json".format(language)),
logger=logging.debug,
raises=False
)
if exercises:
EXERCISES[language] = exercises
return EXERCISES[language]
EXERCISES[language] = softload_json(settings.EXERCISES_FILEPATH, logger=logging.debug, raises=False)
if language == "en": # English-language exercises live in application space, translations in user space

# English-language exercises live in application space, translations in user space
if language == "en":
exercise_root = os.path.join(settings.KHAN_EXERCISES_DIRPATH, "exercises")
else:
exercise_root = os.path.join(django_settings.USER_DATA_ROOT, "exercises")
Expand All @@ -197,7 +216,7 @@ def get_exercise_cache(force=False, language=None):
elif exercise.get("uses_assessment_items", False):
available = False
items = []
for item in exercise.get("all_assessment_items","[]"):
for item in exercise.get("all_assessment_items", "[]"):
item = json.loads(item)
if get_assessment_item_data(request=None, assessment_item_id=item.get("id")):
items.append(item)
Expand Down Expand Up @@ -227,7 +246,7 @@ def get_exercise_cache(force=False, language=None):

if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP:
try:
with open(settings.EXERCISES_FILEPATH + "_" + language + ".cache", "w") as f:
with open(cache_file_path("exercises_{1}.json".format(language)), "w") as f:
json.dump(EXERCISES[language], f)
except IOError as e:
logging.warn("Annotated exercise cache file failed in saving with error {e}".format(e=e))
Expand Down Expand Up @@ -272,7 +291,11 @@ def get_content_cache(force=False, annotate=False, language=None):

if annotate:
if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force:
content = softload_json(settings.CONTENT_FILEPATH + "_" + language + ".cache", logger=logging.debug, raises=False)
content = softload_json(
cache_file_path("content_{1}.json".format(language)),
logger=logging.debug,
raises=False
)
if content:
CONTENT[language] = content
return CONTENT[language]
Expand Down Expand Up @@ -348,7 +371,7 @@ def get_content_cache(force=False, annotate=False, language=None):

if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP:
try:
with open(settings.CONTENT_FILEPATH + "_" + language + ".cache", "w") as f:
with open(cache_file_path("content_{1}.json".format(language)), "w") as f:
json.dump(CONTENT[language], f)
except IOError as e:
logging.warn("Annotated content cache file failed in saving with error {e}".format(e=e))
Expand Down

0 comments on commit c69812c

Please sign in to comment.