Ejemplo n.º 1
0
def use_cache(func, *args, **kwargs):
    """
    Simplecache takes func with args and kwargs
    Returns the cached item if it exists otherwise does the function
    """
    cache_days = kwargs.pop('cache_days', 14) or 14
    cache_name = kwargs.pop('cache_name', '') or ''
    cache_only = kwargs.pop('cache_only', False) or False
    cache_force = kwargs.pop('cache_force', False) or False
    cache_fallback = kwargs.pop('cache_fallback', False) or False
    cache_refresh = kwargs.pop('cache_refresh', False) or False
    cache_combine_name = kwargs.pop('cache_combine_name', False) or False
    headers = kwargs.pop('headers', None) or None
    if not cache_name or cache_combine_name:
        cache_name = format_name(cache_name, *args, **kwargs)
    my_cache = get_cache(cache_name) if not cache_refresh else None
    if my_cache:
        return my_cache
    if not cache_only:
        if headers:
            kwargs['headers'] = headers
        my_object = func(*args, **kwargs)
        return set_cache(my_object,
                         cache_name,
                         cache_days,
                         force=cache_force,
                         fallback=cache_fallback)
Ejemplo n.º 2
0
        def wrapper(self, *args, **kwargs):
            if not self.authorize():
                return

            # Setup getter/setter cache funcs
            func_get = get_pickle if pickle_object else cache.get_cache
            func_set = set_pickle if pickle_object else cache.set_cache

            # Set cache_name
            cache_name = u'{}.'.format(func.__name__)
            cache_name = u'{}.{}'.format(self.__class__.__name__, cache_name)
            cache_name = format_name(cache_name, *args, **kwargs)

            # Cached response last_activity timestamp matches last_activity from trakt so no need to refresh
            last_activity = self._get_last_activity(activity_type,
                                                    activity_key)
            cache_object = func_get(cache_name) if last_activity else None
            if cache_object and cache_object.get(
                    'last_activity') == last_activity:
                if cache_object.get('response') and cache_object.get(
                        'last_activity'):
                    return cache_object['response']

            # Either not cached or last_activity doesn't match so get a new request and cache it
            response = func(self, *args, **kwargs)
            if not response:
                return
            func_set({
                'response': response,
                'last_activity': last_activity
            },
                     cache_name=cache_name,
                     cache_days=cache_days)
            return response
 def wrapper(self, *args, **kwargs):
     """ Syntactic sugar to log output of function """
     response = func(self, *args, **kwargs)
     log_text = '{}.{}.'.format(self.__class__.__name__, func_name)
     log_text = format_name(log_text, *args, **kwargs)
     kodi_log(log_text, 1)
     kodi_log(response, 1)
     return response
 def wrapper(self, *args, **kwargs):
     """ Syntactic sugar to time a class function """
     timer_a = timer()
     response = func(self, *args, **kwargs)
     timer_z = timer()
     total_time = timer_z - timer_a
     if total_time > 0.001:
         timer_name = '{}.{}.'.format(self.__class__.__name__,
                                      func_name)
         timer_name = format_name(timer_name, *args, **kwargs)
         kodi_log('{}\n{:.3f} sec'.format(timer_name, total_time), 1)
     return response