Skip to content

Instantly share code, notes, and snippets.

@shantanuo
Created June 20, 2016 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shantanuo/c6a376309d6bac6bd55bf77e3961b5fb to your computer and use it in GitHub Desktop.
Save shantanuo/c6a376309d6bac6bd55bf77e3961b5fb to your computer and use it in GitHub Desktop.
The code can persist cache to disk, but this modified version will work in notebook as well. http://tohyongcheng.github.io/python/2016/06/07/persisting-a-cache-in-python-to-disk.html
import atexit
import pickle
# or import cPickle as pickle
def persist_cache_to_disk(filename):
def decorator(original_func):
try:
cache = pickle.load(open(filename, 'r'))
except (IOError, ValueError):
cache = {}
# Your pythin script has to exit in order to run this!
# atexit.register(lambda: pickle.dump(cache, open(filename, "w")))
# Let's make a function:
def save_data():
pickle.dump(cache, open(filename, "w"))
def new_func(*args):
if tuple(args) not in cache:
cache[tuple(args)] = original_func(*args)
# Instead, dump your pickled data after
# every call where the cache is changed.
# This can be expensive!
save_data()
return cache[args]
return new_func
return decorator
@persist_cache_to_disk('users.p')
def get_all_users():
x = 'some user'
return x
get_all_users()
@shantanuo
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment