Created
July 4, 2010 21:39
-
-
Save koenbollen/463786 to your computer and use it in GitHub Desktop.
Simple Persistence Caching: make a function cached to disk with a simple decorator.
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
#!/usr/bin/env python | |
# Koen Bollen <meneer koenbollen nl> | |
# 2010 GPL | |
# | |
# Simple Persistence Caching | |
# | |
import os | |
import shelve | |
import urllib2 | |
from functools import wraps | |
from time import time | |
import struct | |
__all__ = [ "cached", "download" ] | |
def cached( f=None, expire=None ): | |
if f is None: | |
cached.expire = expire | |
return cached | |
c = shelve.open( "/tmp/cache-%s-%s"%(os.getlogin(),f.__name__), 'c', -1 ) | |
@wraps(f) | |
def wrapper( *args, **kwargs ): | |
h = struct.pack( "l", hash( args+tuple(kwargs.items()) ) ) | |
try: | |
result, created = c[h] | |
if cached.expire and time()-created > cached.expire: | |
raise KeyError | |
except KeyError: | |
result = f( *args, **kwargs ) | |
c[h] = result, time() | |
except TypeError: | |
result = f( *args, **kwargs ) | |
return result | |
return wrapper | |
cached.expire = None | |
@cached( expire=300 ) | |
def download( url, fp=None ): | |
res = urllib2.urlopen( url ) | |
try: | |
if fp is not None: | |
while True: | |
chunk = res.read( 4096 ) | |
if not chunk: | |
break | |
fp.write( chunk ) | |
return | |
data = res.read() | |
return data | |
finally: | |
res.close() | |
@cached | |
def add5( x ): | |
from time import sleep | |
sleep(1) | |
return x+5 | |
def test(): | |
print add5( 5 ) | |
print add5( x=5 ) | |
if __name__ == "__main__": | |
test() | |
# vim: expandtab shiftwidth=4 softtabstop=4 textwidth=79: |
You need to set
os.environ['PYTHONHASHSEED'] = 0
orhash
will be different on each session.
Good to know! 👍 But this code snippet is from 13 years ago... 😁 But I learned something new about Python! Cheers!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to set
os.environ['PYTHONHASHSEED'] = 0
orhash
will be different on each session.