Skip to content

Instantly share code, notes, and snippets.

@twexler
Created May 24, 2011 21:25
Show Gist options
  • Save twexler/989734 to your computer and use it in GitHub Desktop.
Save twexler/989734 to your computer and use it in GitHub Desktop.
PyCache
#!/usr/bin/python
from __future__ import with_statement
import os,stat, pickle
class PyCache:
file = "/tmp/pycache.tmp"
def __init__(self): pass
def retrieve(self):
try:
st = os.stat(self.file)
curtime = time.time()
if curtime - st[stat.ST_MTIME] > 300:
os.unlink(self.file)
return None
else:
with open(self.file) as f: return pickle.load(f)
except:
return None
def cache(self, object):
with open(self.file, 'w') as f: pickle.dump(object,f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment