Write JSON before updating cache, removes redundant loading

This commit is contained in:
Noah 2014-12-06 22:34:40 +00:00
parent 14971857c7
commit 83d878a6b6

View File

@ -74,14 +74,14 @@ def commit(document, data, cache=True):
logger.debug("JsonDB: mkdir {}".format(segment)) logger.debug("JsonDB: mkdir {}".format(segment))
os.mkdir(segment, 0o755) os.mkdir(segment, 0o755)
# Write the JSON.
write_json(path, data)
# Update the cached document. # Update the cached document.
if cache: if cache:
set_cache(document, data, expires=cache_lifetime) set_cache(document, data, expires=cache_lifetime)
set_cache(document+"_mtime", time.time(), expires=cache_lifetime) set_cache(document+"_mtime", time.time(), expires=cache_lifetime)
# Write the JSON.
write_json(path, data)
# Release the lock. # Release the lock.
unlock_cache(document) unlock_cache(document)
@ -257,9 +257,11 @@ def lock_cache(key, timeout=5, expire=20):
# Take the lock. # Take the lock.
set_cache(lock_key, time.time(), expire) set_cache(lock_key, time.time(), expire)
logger.debug("Cache lock acquired: {}, expires in {}s".format(key, expire))
return True return True
def unlock_cache(key): def unlock_cache(key):
"""Release the lock on a cache key.""" """Release the lock on a cache key."""
del_cache(key + "_lock") del_cache(key + "_lock")
logger.debug("Cache lock released: {}".format(key))