Created
June 20, 2016 05:28
-
-
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
This file contains hidden or 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
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for stack overflow http://stackoverflow.com/questions/37883015/using-decorators-to-persist-python-objects