def cache_with_key(keyfunc, cache_name=None, timeout=None, with_statsd_key=None):
 def decorator(func):
  @wraps(func)
  def func_with_caching(*args, **kwargs):
   key = keyfunc(*args, **kwargs)
   val = cache_get(key, cache_name=cache_name)
   extra = ''
   if (cache_name == 'database'):
    extra = '.dbcache'
   if (with_statsd_key is not None):
    metric_key = with_statsd_key
   else:
    metric_key = statsd_key(key)
   status = ('hit' if (val is not None) else 'miss')
   statsd.incr(('cache%s.%s.%s' % (extra, metric_key, status)))
   if (val is not None):
    return val[0]
   val = func(*args, **kwargs)
   cache_set(key, val, cache_name=cache_name, timeout=timeout)
   return val
  return func_with_caching
 return decorator

def cache_with_key ( keyfunc , cache_name = None , timeout = None , with_statsd_key = None ) : def decorator ( func ) : @ wraps ( func ) def func_with_caching ( * args , ** kwargs ) : key = keyfunc ( * args , ** kwargs ) val = cache_get ( key , cache_name = cache_name ) extra = '' if ( cache_name == 'database' ) : extra = '.dbcache' if ( with_statsd_key is not None ) : metric_key = with_statsd_key else : metric_key = statsd_key ( key ) status = ( 'hit' if ( val is not None ) else 'miss' ) statsd . incr ( ( 'cache%s.%s.%s' % ( extra , metric_key , status ) ) ) if ( val is not None ) : return val [ 0 ] val = func ( * args , ** kwargs ) cache_set ( key , val , cache_name = cache_name , timeout = timeout ) return val return func_with_caching return decorator
decorator which applies django caching to a function .