Skip to content

Instantly share code, notes, and snippets.

@selwyth
Created February 6, 2018 22:15
Show Gist options
  • Save selwyth/167e6b7a6f07b13cb7d6bba7853bb9d9 to your computer and use it in GitHub Desktop.
Save selwyth/167e6b7a6f07b13cb7d6bba7853bb9d9 to your computer and use it in GitHub Desktop.
caching
# Utility function to help with local prototyping to minimize trips to fetch data.
# Import the function and decorate any data-retrieval function to use cache if
# available, make one if not. Remove the decorator before submitting to production.
import cPickle as pickle
import logging
import os
from functools import wraps
THIS_DIR = os.getcwd()
def persist_cache_to_disk(filename):
def decorator(original_func):
@wraps(original_func)
def new_func(*args, **kwargs):
# Use the cache if available
if os.path.exists(filename):
logging.info("Cache found at {}, loading it now".format(
os.path.join(THIS_DIR, filename)
))
with open(filename, 'r') as f:
cache = pickle.load(f)
logging.info('Cache loaded')
else:
cache = None
if cache is None:
logging.info("No cache found at {}, getting data".format(
os.path.join(THIS_DIR, filename)
))
cache = original_func(*args, **kwargs)
logging.info("Persisting data to cache at {}".format(
os.path.join(THIS_DIR, filename)
))
pickle.dump(cache, open(filename, "w"))
logging.info("Finished persisting data")
return cache
return new_func
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment