Created
February 6, 2018 22:15
-
-
Save selwyth/167e6b7a6f07b13cb7d6bba7853bb9d9 to your computer and use it in GitHub Desktop.
caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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