Created
November 11, 2014 22:19
-
-
Save GitMoIO/d606015a28ad52addd25 to your computer and use it in GitHub Desktop.
create a pickle file for caching data in python
From http://pynewb.tumblr.com/post/16870464613/python-pickle-file-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
import os | |
import cPickle as pickle | |
def cached_data(): | |
cache_path = "/tmp/cached-data.pickle" | |
if not os.path.exists(cache_path): | |
# The cache doesn't exist, create it and populate it | |
result = generate_data() | |
cache_file = open(cache_path,'wb') | |
# Write it to the result to the file as a pickled object | |
# Use the binary protocol for better performance | |
pickle.dump(result, cache_file, protocol=1) | |
cache_file.close() | |
return pickle.load(open(cache_path,'rb')) | |
def generate_data(): | |
""" | |
This function actually generates the data when it isn't cached. | |
The data generated is often expensive to compute so caching helps with performance. | |
""" | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment