Exemple #1
0
    def invalidate(self, func, *args, **kwargs):
        """Invalidate a cache decorated function.  You must call this with
        the same positional and keyword arguments as what you did when you
        call the decorated function, otherwise the cache will not be deleted.
        The usage is simple::

            @cache.cache()
            def load(name, limit):
                return load_from_database(name, limit)

            rv = load('foo', limit=5)

            cache.invalidate(load, 'foo', limit=5)

        :param func: decorated function to invalidate
        :param args: same positional arguments as you call the function
        :param kwargs: same keyword arguments as you call the function
        :return: whether it is invalidated or not
        """
        try:
            cache_params = func.__rc_cache_params__
        except AttributeError:
            raise TypeError('Attempted to invalidate a function that is'
                            'not cache decorated')
        key_prefix = cache_params['key_prefix']
        cache_args = args
        include_self = cache_params.get('include_self', False)
        if include_self:
            instance_self = getattr(func, '__self__', None)
            if instance_self:
                cache_args = tuple([instance_self] + list(args))
        cache_key = generate_key_for_cached_func(
            key_prefix, func, *cache_args, **kwargs)
        return self.delete(cache_key)
Exemple #2
0
    def invalidate(self, func, *args, **kwargs):
        """Invalidate a cache decorated function.  You must call this with
        the same positional and keyword arguments as what you did when you
        call the decorated function, otherwise the cache will not be deleted.
        The usage is simple::

            @cache.cache()
            def load(name, limit):
                return load_from_database(name, limit)

            rv = load('foo', limit=5)

            cache.invalidate(load, 'foo', limit=5)

        :param func: decorated function to invalidate
        :param args: same positional arguments as you call the function
        :param kwargs: same keyword arguments as you call the function
        :return: whether it is invalidated or not
        """
        try:
            cache_params = func.__rc_cache_params__
        except AttributeError:
            raise TypeError('Attempted to invalidate a function that is'
                            'not cache decorated')
        key_prefix = cache_params['key_prefix']
        cache_args = args
        include_self = cache_params.get('include_self', False)
        if include_self:
            instance_self = getattr(func, '__self__', None)
            if instance_self:
                cache_args = tuple([instance_self] + list(args))
        cache_key = generate_key_for_cached_func(key_prefix, func, *cache_args,
                                                 **kwargs)
        return self.delete(cache_key)
Exemple #3
0
def test_generate_key():
    def func():
        pass

    cache_key = generate_key_for_cached_func(None, func, 'foo')
    assert cache_key == u'test_utils func foo'
    cache_key = generate_key_for_cached_func('prefix', func, 'foo')
    assert cache_key == u'prefix test_utils func foo'
    cache_key = generate_key_for_cached_func(None, func, 'foo', k='v')
    assert cache_key == u'test_utils func foo k=v'
    cache_key = generate_key_for_cached_func(None, func, 'foo', k='v', k2='v2')
    assert cache_key == u'test_utils func foo k=v k2=v2'

    def method(self):
        pass

    cache_key = generate_key_for_cached_func(None, method, 'foo')
    assert cache_key == u'test_utils method foo'
    cache_key = generate_key_for_cached_func(None, method, None, 'foo')
    assert cache_key == u'test_utils method None foo'

    def method(cls):
        pass

    cache_key = generate_key_for_cached_func(None, method, 'foo')
    assert cache_key == u'test_utils method foo'
Exemple #4
0
 def wrapper(*args, **kwargs):
     cache_key = generate_key_for_cached_func(
         key_prefix, f, *args, **kwargs)
     if self._running_mode == BATCH_MODE:
         promise = Promise()
         self._pending_operations.append(
             (f, args, kwargs, promise, cache_key, expire))
         return promise
     rv = self._raw_get(cache_key)
     if rv is not None:
         return self.serializer.loads(rv)
     rv = f(*args, **kwargs)
     self.set(cache_key, rv, expire)
     return rv
Exemple #5
0
 def wrapper(*args, **kwargs):
     cache_args = args
     # handle self and cls
     if has_self:
         if not include_self:
             cache_args = args[1:]
     cache_key = generate_key_for_cached_func(
         key_prefix, f, *cache_args, **kwargs)
     if self._running_mode == BATCH_MODE:
         promise = Promise()
         self._pending_operations.append(
             (f, args, kwargs, promise, cache_key, expire))
         return promise
     rv = self._raw_get(cache_key)
     if rv is None:
         value = f(*args, **kwargs)
         self.set(cache_key, value, expire)
         rv = self.serializer.dumps(value)
     return self.serializer.loads(rv)
Exemple #6
0
def test_generate_key():
    def func():
        pass
    cache_key = generate_key_for_cached_func(None, func, 'foo')
    assert cache_key == u'test_utils func foo'
    cache_key = generate_key_for_cached_func('prefix', func, 'foo')
    assert cache_key == u'prefix test_utils func foo'
    cache_key = generate_key_for_cached_func(None, func, 'foo', k='v')
    assert cache_key == u'test_utils func foo k=v'
    cache_key = generate_key_for_cached_func(None, func,
                                             'foo', k='v', k2='v2')
    assert cache_key == u'test_utils func foo k=v k2=v2'

    def method(self):
        pass
    cache_key = generate_key_for_cached_func(None, method, 'foo')
    assert cache_key == u'test_utils method foo'
    cache_key = generate_key_for_cached_func(None, method, None, 'foo')
    assert cache_key == u'test_utils method None foo'

    def method(cls):
        pass
    cache_key = generate_key_for_cached_func(None, method, 'foo')
    assert cache_key == u'test_utils method foo'