def cached(key = None, extradata = {}):
def decorator(f):<EOL><INDENT>@wraps(f)<EOL>def wrapper(*args, **kwargs):<EOL><INDENT>uid = key<EOL>if not uid:<EOL><INDENT>from hashlib import md5<EOL>arguments = list(args) + [(a, kwargs[a]) for a in sorted(kwargs.keys())]<EOL>uid = md5(str(arguments)).hexdigest()<EOL><DEDENT>if exists(uid):<EOL><INDENT>debug('<STR_LIT>' % (uid, cache))<EOL>return get(uid)<EOL><DEDENT>else:<EOL><INDENT>debug('<STR_LIT>' % (uid, cache))<EOL>result = f(*args, **kwargs)<EOL>debug('<STR_LIT>' % (result, uid, cache))<EOL>debug('<STR_LIT>' + (str(extradata) or '<STR_LIT:None>'))<EOL>put(uid, result, extradata)<EOL>return result<EOL><DEDENT><DEDENT>return wrapper<EOL><DEDENT>return decorator<EOL>
Decorator used for caching.