def time_cache(time_add_setting):
 def _temp(key_func):
  dct = {}
  _time_caches[time_add_setting] = dct
  def wrapper(*args, **kwargs):
   generator = key_func(*args, **kwargs)
   key = next(generator)
   try:
    (expiry, value) = dct[key]
    if (expiry > time.time()):
     return value
   except KeyError:
    pass
   value = next(generator)
   time_add = getattr(settings, time_add_setting)
   if (key is not None):
    dct[key] = ((time.time() + time_add), value)
   return value
  return wrapper
 return _temp

def time_cache ( time_add_setting ) : def _temp ( key_func ) : dct = { } _time_caches [ time_add_setting ] = dct def wrapper ( * args , ** kwargs ) : generator = key_func ( * args , ** kwargs ) key = next ( generator ) try : ( expiry , value ) = dct [ key ] if ( expiry > time . time ( ) ) : return value except KeyError : pass value = next ( generator ) time_add = getattr ( settings , time_add_setting ) if ( key is not None ) : dct [ key ] = ( ( time . time ( ) + time_add ) , value ) return value return wrapper return _temp
this decorator works as follows: call it with a setting and after that use the function with a callable that returns the key .