#NEWLINE# def cache( seconds = 300 , cache_name = DEFAULT_CACHE_ALIAS , skip_first = False ) : #NEWLINE# #INDENT# '\n    Cache the result of a function call with particular parameters for specified\n    number of seconds.\n\n    Args:\n        * skip_first - set to True if first argument should not be considered\n          when calculating hash of arguments (useful when first argument\n          is instance of a class (self)).\n    ' #NEWLINE# def _cache( func ) : #NEWLINE# #INDENT# @ wraps ( func ) #NEWLINE# def wrapper( * args , **kwargs ) : #NEWLINE# #INDENT# cache_proxy = caches [ cache_name ] #NEWLINE# key = _cache_key_hash ( func , * ( args [ 1 : ] if skip_first else args ) , ** kwargs ) #NEWLINE# result = cache_proxy . get ( key , default = CACHE_DEFAULT ) #NEWLINE# if ( result is CACHE_DEFAULT ) : #NEWLINE# #INDENT# logger . debug ( 'Recalculating result of {}' . format ( func . __name__ ) ) #NEWLINE# result = func ( * args , ** kwargs ) #NEWLINE# cache_proxy . set ( key , result , seconds ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logger . debug ( 'Taking result of {} from cache' . format ( func . __name__ ) ) #NEWLINE# #UNINDENT# return result #NEWLINE# #UNINDENT# if settings . USE_CACHE : #NEWLINE# #INDENT# return wrapper #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return func #NEWLINE# #UNINDENT# #UNINDENT# return _cache