Jun 20, 2011

Сached decorator for functions

from django.core.cache import cache
from hashlib import sha256

def cached(ctime=3600):
    def decr(func):
        def wrp(*args,**kargs):
            key = sha256(func.func_name+repr(args)+repr(kargs)).hexdigest()
            res = cache.get(key)
            if res is None:
                res = func(*args,**kargs)
                cache.set(key,res,ctime)
            return res
        return wrp
    return decr

No comments: