Hi Michele. Thanks for your great job!
Wrote a small decorator(django by default caches everything in 'default' cache):
def filesystem_cache(key_prefix, cache_time=None):
"""
Caches function based on key_prefix and function args/kwargs.
Stores function result in filesystem cache for a certain cache_time.
"""
if cache_time:
FILESYSTEM_CACHE_TIME = cache_time
else:
FILESYSTEM_CACHE_TIME = settings.CACHES['filesystem'].get('TIMEOUT')
@decorator
def wrapfunc(func, *args, **kwargs):
ignore_cache = kwargs.pop('_ignore_cache', False)
full_args = list()
full_args.extend(args)
for k, v in kwargs.items():
full_args.append('%s:%s' % (str(k), str(v)))
md5_args = md5_constructor(u':'.join([urlquote(var) for var in full_args]))
cache_key = 'template.cache.%s.%s' % (key_prefix, md5_args.hexdigest())
filesystem_cache = get_cache(FILESYSTEM_CACHE_NAME)
cached_value = filesystem_cache.get(cache_key)
if cached_value and not ignore_cache:
# if cached value exists - return it
return cached_value
result = func(*args, **kwargs)
filesystem_cache.set(cache_key, result, FILESYSTEM_CACHE_TIME)
return result
return wrapfunc
Everything works ok except that all args and kwargs for function fall into args
section :
In [2]: @filesystem_cache('bla')
...: def hello(a,b,c, d=10,e=12):
...: print a
...: print b
...: print c
...: print d
...: return e
In [3]: hello(5,6,7, d=11,e=18)
> /home/dev/imax/mws/src/imax_main/utils.py(29)wrapfunc()
-> md5_args = md5_constructor(u':'.join([urlquote(var) for var in full_args]))
(Pdb) l
24 full_args = list()
25 full_args.extend(args)
26 for k, v in kwargs.items():
27 full_args.append('%s:%s' % (str(k), str(v)))
28 import pdb; pdb.set_trace()
29 -> md5_args = md5_constructor(u':'.join([urlquote(var) for var in full_args]))
30 cache_key = 'template.cache.%s.%s' % (key_prefix, md5_args.hexdigest())
31 filesystem_cache = get_cache(FILESYSTEM_CACHE_NAME)
32 cached_value = filesystem_cache.get(cache_key)
33 if cached_value and not ignore_cache:
34 # if cached value exists - return it
(Pdb) args
func = <function hello at 0xa63f454>
args = (5, 6, 7, 11, 18)
kwargs = {}
(Pdb) kwargs
{}
Why d and e kwargs are in args array ? Thanks !
Original issue reported on code.google.com by alecs....@gmail.com on 13 Feb 2012 at 10:20
Original issue reported on code.google.com by
alecs....@gmail.com
on 13 Feb 2012 at 10:20