Ejemplo n.º 1
0
    def test_static_method_default_key(self):
        cache_callable = User.static_method_default_key
        cache_prefix = create_cache_key('ppp', 2)
        cache_key = create_cache_key(
            cache_prefix, __name__ + '.User.static_method_default_key', 1, 2,
            11)

        self._check_base(cache_callable, param_to_change='b')
        self._check_cache_key(cache_callable, cache_key, a=1, b=2)

        # check partial invalidation
        self.cache.reset_mock()
        cache_callable(1, 2, 3)
        self.assertTrue(self.cache.called)

        self.cache.reset_mock()
        cache_callable(1, 2, 3)
        self.assertFalse(self.cache.called)

        self.cache.reset_mock()
        cache_callable.invalidate_cache_by_tags(c=3)
        cache_callable(1, 2, 3)
        self.assertTrue(self.cache.called)

        self.cache.reset_mock()
        cache_callable.invalidate_cache_by_prefix(b=2)
        cache_callable(1, 2, 3)
        self.assertTrue(self.cache.called)

        self.cache.reset_mock()
        cache_callable.invalidate_cache_by_key(1, b=2, c=3)
        cache_callable(1, 2, 3)
        self.assertTrue(self.cache.called)
Ejemplo n.º 2
0
    def test_default_cache_key(self):
        cache_callable = self.user.instance_default_cache_key
        cache_key = create_cache_key(
            __name__ + '.User.instance_default_cache_key', 1, 2, 8
        )
        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 1, 2)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)

        cache_callable = User.class_method_default_cache_key
        cache_key = create_cache_key(
            __name__ + '.User.class_method_default_cache_key', 2, 3, 9, 'HAHA'
        )

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 2, 3)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)

        cache_callable = computation
        cache_key = create_cache_key(
            __name__ + '.computation', 'a', 'b', 'c'
        )

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 'a', 'b', 'c')
        self._check_timeout(cache_key, 100)
Ejemplo n.º 3
0
    def test_static_method_default_key(self):
        cache_callable = User.static_method_default_key
        cache_prefix = create_cache_key('ppp', 2)
        cache_key = create_cache_key(
            cache_prefix, __name__ + '.User.static_method_default_key', 1, 2, 11
        )

        self._check_base(cache_callable, param_to_change='b')
        self._check_cache_key(cache_callable, cache_key, a=1, b=2)

        # check partial invalidation
        self.cache.reset_mock()
        cache_callable(1, 2, 3)
        self.assertTrue(self.cache.called)

        self.cache.reset_mock()
        cache_callable(1, 2, 3)
        self.assertFalse(self.cache.called)

        self.cache.reset_mock()
        cache_callable.invalidate_cache_by_tags(c=3)
        cache_callable(1, 2, 3)
        self.assertTrue(self.cache.called)

        self.cache.reset_mock()
        cache_callable.invalidate_cache_by_prefix(b=2)
        cache_callable(1, 2, 3)
        self.assertTrue(self.cache.called)

        self.cache.reset_mock()
        cache_callable.invalidate_cache_by_key(1, b=2, c=3)
        cache_callable(1, 2, 3)
        self.assertTrue(self.cache.called)
Ejemplo n.º 4
0
    def test_method_prefixed(self):
        cache_callable = self.user.instance_method_prefixed
        cache_prefix = create_cache_key('USER', self.user.id)

        # prefix should ba attached
        cache_key = create_cache_key(cache_prefix, 'p1', 1, 2, 3)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 1, 2, 3)
        self._check_timeout(cache_key, 3600)

        # prefix is a tag actually
        self._check_cache_prefix(cache_callable, cache_prefix, 1, 2, 3)
        self._check_tags(cache_callable, [create_cache_key(self.user.id, 'tag1')], 1, 2, 3)
Ejemplo n.º 5
0
    def test_instance_method_dynamic_timeout(self):
        cache_callable = self.user.instance_dynamic_timeout

        self._check_base(cache_callable)

        cache_key = create_cache_key('dyn_timeout', 2)
        self._check_cache_key(cache_callable, cache_key, 2, 3, 4)
        self._check_timeout(cache_key, 2 * 100)

        self.cache.reset_mock()

        cache_key = create_cache_key('dyn_timeout', 4)
        self._check_cache_key(cache_callable, cache_key, 4, 5, 6)
        self._check_timeout(cache_key, 4 * 100)
Ejemplo n.º 6
0
    def test_instance_method_dynamic_timeout(self):
        cache_callable = self.user.instance_dynamic_timeout

        self._check_base(cache_callable)

        cache_key = create_cache_key('dyn_timeout', 2)
        self._check_cache_key(cache_callable, cache_key, 2, 3, 4)
        self._check_timeout(cache_key, 2 * 100)

        self.cache.reset_mock()

        cache_key = create_cache_key('dyn_timeout', 4)
        self._check_cache_key(cache_callable, cache_key, 4, 5, 6)
        self._check_timeout(cache_key, 4 * 100)
Ejemplo n.º 7
0
    def test_method_prefixed(self):
        cache_callable = self.user.instance_method_prefixed
        cache_prefix = create_cache_key('USER', self.user.id)

        # prefix should ba attached
        cache_key = create_cache_key(cache_prefix, 'p1', 1, 2, 3)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 1, 2, 3)
        self._check_timeout(cache_key, 3600)

        # prefix is a tag actually
        self._check_cache_prefix(cache_callable, cache_prefix, 1, 2, 3)
        self._check_tags(cache_callable,
                         [create_cache_key(self.user.id, 'tag1')], 1, 2, 3)
Ejemplo n.º 8
0
    def test_cache_custom_tags(self):
        cache_callable = self.user.instance_method_custom_tags
        cache_key = create_cache_key(10, 11)
        cache_tags = self.user.generate_custom_tags(MetaCallable(args=(self.user, 10)))

        self._check_cache_key(cache_callable, cache_key, 10, 11)
        self._check_tags(cache_callable, cache_tags, 10, 11)
Ejemplo n.º 9
0
    def test_not_default_timeout(self):
        cache_callable = self.user.instance_method_timeout
        cache_key = create_cache_key(self.user.id, 5, 5)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 5, 5)
        self._check_timeout(cache_key, 400)
Ejemplo n.º 10
0
    def test_cache_key_as_list(self):
        cache_callable = self.user.instance_method_list
        cache_key = create_cache_key(self.user.id, 2, 3)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 2, 3)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)
Ejemplo n.º 11
0
    def generate_key_based_on_meta(m, a=1):
        assert isinstance(m, MetaCallable)
        assert m.function is getattr(m['self'], 'instance_method_meta_test').function
        assert m.scope is m['self']
        assert a == 1

        return create_cache_key(m['a'], m['b'], m['c'])
Ejemplo n.º 12
0
    def test_property_friends(self):
        friends_list = ['Ivan', 'Sergey', 'S***n']

        self.assertListEqual(self.user.friends, friends_list)

        self.cache.assert_called_once_with()
        self.cache.reset_mock()

        self.assertListEqual(self.user.friends, friends_list)
        self.cache.assert_not_called()
        self.cache.reset_mock()

        User.friends.invalidate_cache_by_key(self.user)
        self.assertListEqual(self.user.friends, friends_list)
        self.cache.assert_called_once_with()
        self.cache.reset_mock()

        cache_callable = lambda: getattr(self.user, 'friends')
        cache_callable.property = True
        cache_key = create_cache_key(self.user.id, 'friends')

        self._check_cache_key(cache_callable,
                              cache_key,
                              invalidator=partial(
                                  User.friends.invalidate_cache_by_key,
                                  self.user))
        self._check_timeout(cache_key, 666)
Ejemplo n.º 13
0
    def test_ordinal_func(self):
        cache_callable = ordinal_func
        cache_prefix = ordinal_func.prefix
        cache_key = create_cache_key(cache_prefix, 10, 20)

        self.cache.reset_mock()

        result = process_args(a=10, b=10)

        self.assertEqual(cache_callable(a=10, b=10), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        # cached version
        self.assertEqual(cache_callable(a=10, b=10), result)
        self.assertFalse(self.cache.called)
        self.cache.reset_mock()

        result = process_args(a=10, b=22)

        # different params, no cache
        self.assertEqual(cache_callable(a=10, b=22), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        self._check_cache_key(cache_callable, cache_key, a=10, b=20)
        self._check_cache_prefix(cache_callable, cache_prefix, a=10, b=20)
Ejemplo n.º 14
0
    def test_default_cache_key_for_property(self):
        self.assertEqual(self.user.test_property, 'property')

        cache_callable = lambda: getattr(self.user, 'test_property')
        cache_key = create_cache_key(__name__ + '.User.test_property')

        self._check_cache_key(
            cache_callable,
            cache_key,
            invalidator=User.test_property.invalidate_cache_by_key)

        self.local_cache.clear()
        self.cache.reset_mock()

        self.assertEqual(self.user.test_property, 'property')
        self.cache.assert_called_once_with('property')
        self.cache.reset_mock()

        self.assertEqual(self.user.test_property, 'property')
        self.assertFalse(self.cache.called)

        # invalidate cache
        User.test_property.invalidate_cache_by_key()
        self.assertEqual(self.user.test_property, 'property')
        self.cache.assert_called_once_with('property')
Ejemplo n.º 15
0
    def test_cache_key_as_list(self):
        cache_callable = self.user.instance_method_list
        cache_key = create_cache_key(self.user.id, 2, 3)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 2, 3)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)
Ejemplo n.º 16
0
    def test_not_default_timeout(self):
        cache_callable = self.user.instance_method_timeout
        cache_key = create_cache_key(self.user.id, 5, 5)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 5, 5)
        self._check_timeout(cache_key, 400)
Ejemplo n.º 17
0
    def test_class_method_full_spec(self):
        cache_callable = User.class_method_full_spec
        a = u'a'
        b = u'b'
        c = 10

        cache_prefix = create_cache_key('USER', a, b)
        cache_key = create_cache_key(cache_prefix, User.name, a)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, a, b, c)
        self._check_timeout(cache_key, 500)
        self._check_tags(cache_callable,
                         ['tag4', create_cache_key(u'tag5', User.name)], a, b,
                         c)
        self._check_cache_prefix(cache_callable, cache_prefix, a, b, c)
Ejemplo n.º 18
0
    def test_ordinal_func(self):
        cache_callable = ordinal_func
        cache_prefix = ordinal_func.prefix
        cache_key = create_cache_key(cache_prefix, 10, 20)

        self.cache.reset_mock()

        result = process_args(a=10, b=10)

        self.assertEqual(cache_callable(a=10, b=10), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        # cached version
        self.assertEqual(cache_callable(a=10, b=10), result)
        self.assertFalse(self.cache.called)
        self.cache.reset_mock()

        result = process_args(a=10, b=22)

        # different params, no cache
        self.assertEqual(cache_callable(a=10, b=22), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        self._check_cache_key(cache_callable, cache_key, a=10, b=20)
        self._check_cache_prefix(cache_callable, cache_prefix, a=10, b=20)
Ejemplo n.º 19
0
    def test_second_func(self):
        cache_callable = second_func
        cache_key = create_cache_key('second', 100)

        self._check_base(cache_callable, param_to_change='c')
        self._check_cache_key(cache_callable, cache_key, 1, 2, c=100)
        self._check_timeout(cache_key, 450)
        self._check_tags(cache_callable, ['yyy'], 'yyy', 111)
Ejemplo n.º 20
0
    def test_second_func(self):
        cache_callable = second_func
        cache_key = create_cache_key('second', 100)

        self._check_base(cache_callable, param_to_change='c')
        self._check_cache_key(cache_callable, cache_key, 1, 2, c=100)
        self._check_timeout(cache_key, 450)
        self._check_tags(cache_callable, ['yyy'], 'yyy', 111)
Ejemplo n.º 21
0
    def test_cache_tags(self):
        cache_callable = self.user.instance_method_tags
        cache_key = create_cache_key(self.user.id, 5, 5)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 5, 5)
        self._check_timeout(cache_key, 500)
        self._check_tags(cache_callable, ['tag1', 'tag2'], 6, 7)
Ejemplo n.º 22
0
    def test_property_no_tags(self):
        self.assertEqual(self.user.property_no_tags, '42')

        cache_callable = lambda: getattr(self.user, 'property_no_tags')
        cache_callable.property = True
        cache_key = create_cache_key('static_key')

        self._check_cache_key(cache_callable, cache_key)
Ejemplo n.º 23
0
    def test_property_no_tags(self):
        self.assertEqual(self.user.property_no_tags, '42')

        cache_callable = lambda: getattr(self.user, 'property_no_tags')
        cache_callable.property = True
        cache_key = create_cache_key('static_key')

        self._check_cache_key(cache_callable, cache_key)
Ejemplo n.º 24
0
    def test_cache_key_as_string(self):
        cache_callable = self.user.instance_method_string
        cache_key = create_cache_key(self.user.id, 1, 2, 3)

        self._check_base(self.user.instance_method_string)
        self._check_cache_key(cache_callable, cache_key, 1, 2, c=3)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)
        self.assertEqual(len(self.local_cache), 1)
Ejemplo n.º 25
0
    def test_cache_tags(self):
        cache_callable = self.user.instance_method_tags
        cache_key = create_cache_key(self.user.id, 5, 5)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 5, 5)
        self._check_timeout(cache_key, 500)
        self._check_tags(cache_callable, ['tag1', 'tag2'], 6, 7)
Ejemplo n.º 26
0
    def test_cache_custom_tags(self):
        cache_callable = self.user.instance_method_custom_tags
        cache_key = create_cache_key(10, 11)
        cache_tags = self.user.generate_custom_tags(
            MetaCallable(args=(self.user, 10)))

        self._check_cache_key(cache_callable, cache_key, 10, 11)
        self._check_tags(cache_callable, cache_tags, 10, 11)
Ejemplo n.º 27
0
    def generate_custom_tags(meta):
        """ :type meta: MetaCallable """
        if meta.has_returned_value:
            cache_mock.assert_called_with(meta.returned_value)

        self = meta.args[0]
        a = meta.args[1]
        return [create_cache_key(self.name, self.id, a), 'simple_tag']
Ejemplo n.º 28
0
    def test_cache_key_as_string(self):
        cache_callable = self.user.instance_method_string
        cache_key = create_cache_key(self.user.id, 1, 2, 3)

        self._check_base(self.user.instance_method_string)
        self._check_cache_key(cache_callable, cache_key, 1, 2, c=3)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)
        self.assertEqual(len(self.local_cache), 1)
Ejemplo n.º 29
0
    def generate_key_based_on_meta(m, a=1):
        assert isinstance(m, MetaCallable)
        assert m.function is getattr(m['self'],
                                     'instance_method_meta_test').function
        assert m.scope is m['self']
        assert a == 1

        return create_cache_key(m['a'], m['b'], m['c'])
Ejemplo n.º 30
0
    def generate_custom_tags(meta):
        """ :type meta: MetaCallable """
        if meta.has_returned_value:
            cache_mock.assert_called_with(meta.returned_value)

        self = meta.args[0]
        a = meta.args[1]
        return [create_cache_key(self.name, self.id, a), 'simple_tag']
Ejemplo n.º 31
0
    def test_class_method_full_spec(self):
        cache_callable = User.class_method_full_spec
        a = u'a'
        b = u'b'
        c = 10

        cache_prefix = create_cache_key('USER', a, b)
        cache_key = create_cache_key(cache_prefix, User.name, a)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, a, b, c)
        self._check_timeout(cache_key, 500)
        self._check_tags(
            cache_callable,
            ['tag4', create_cache_key(u'tag5', User.name)],
            a, b, c
        )
        self._check_cache_prefix(cache_callable, cache_prefix, a, b, c)
Ejemplo n.º 32
0
    def test_instance_method_and_meta_accepted_decorator(self):
        cache_callable = self.user.instance_method_meta_test

        cache_key = create_cache_key(1, 2, 5)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 1, 2, c=5)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)
        self.assertEqual(len(self.local_cache), 1)
Ejemplo n.º 33
0
    def test_instance_method_and_meta_accepted_decorator(self):
        cache_callable = self.user.instance_method_meta_test

        cache_key = create_cache_key(1, 2, 5)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 1, 2, c=5)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)
        self.assertEqual(len(self.local_cache), 1)
Ejemplo n.º 34
0
    def test_property_no_tags(self):
        self.assertEqual(self.user.property_no_tags, '42')

        cache_callable = lambda: getattr(self.user, 'property_no_tags')
        cache_key = create_cache_key('static_key')

        self._check_cache_key(
            cache_callable,
            cache_key,
            invalidator=partial(User.property_no_tags.invalidate_cache_by_key))
Ejemplo n.º 35
0
    def test_class_method_key_string(self):
        cache_callable = User.class_method_cache_key_string
        cache_key = create_cache_key(User.name, 17)

        self._check_base(cache_callable, param_to_change='c')
        self._check_cache_key(cache_callable, cache_key, 1, 2)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)

        cache_callable = self.user.class_method_cache_key_string
        self._check_base(cache_callable, param_to_change='c')
        self._check_cache_key(cache_callable, cache_key, 4, 5)
Ejemplo n.º 36
0
    def test_class_method_key_string(self):
        cache_callable = User.class_method_cache_key_string
        cache_key = create_cache_key(User.name, 17)

        self._check_base(cache_callable, param_to_change='c')
        self._check_cache_key(cache_callable, cache_key, 1, 2)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)

        cache_callable = self.user.class_method_cache_key_string
        self._check_base(cache_callable, param_to_change='c')
        self._check_cache_key(cache_callable, cache_key, 4, 5)
Ejemplo n.º 37
0
    def test_static_method(self):
        cache_callable = User.static_method
        hg = 123
        test = u'ЫЫЫЫ'

        cache_prefix = cache_callable.prefix
        cache_key = create_cache_key(cache_prefix, hg, hg, test)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, hg, test)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)
        self._check_cache_prefix(cache_callable, cache_prefix, hg, test)
Ejemplo n.º 38
0
    def test_static_method(self):
        cache_callable = User.static_method
        hg = 123
        test = u'ЫЫЫЫ'

        cache_prefix = cache_callable.prefix
        cache_key = create_cache_key(cache_prefix, hg, hg, test)

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, hg, test)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)
        self._check_cache_prefix(cache_callable, cache_prefix, hg, test)
Ejemplo n.º 39
0
    def test_default_cache_key(self):
        cache_callable = self.user.instance_default_cache_key
        cache_key = create_cache_key(
            __name__ + '.User.instance_default_cache_key', 1, 2, 8)
        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 1, 2)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)

        cache_callable = User.class_method_default_cache_key
        cache_key = create_cache_key(
            __name__ + '.User.class_method_default_cache_key', 2, 3, 9, 'HAHA')

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 2, 3)
        self._check_timeout(cache_key, DEFAULT_TIMEOUT)

        cache_callable = computation
        cache_key = create_cache_key(__name__ + '.computation', 'a', 'b', 'c')

        self._check_base(cache_callable)
        self._check_cache_key(cache_callable, cache_key, 'a', 'b', 'c')
        self._check_timeout(cache_key, 100)
Ejemplo n.º 40
0
    def test_property_friends_count(self):
        self.assertEqual(self.user.friends_count, 15)

        cache_callable = lambda: getattr(self.user, 'friends_count')
        cache_callable.property = True
        cache_callable.invalidate_cache_by_prefix = User.friends_count.invalidate_cache_by_prefix

        cache_prefix = 'USER_PROPERTY'
        cache_key = create_cache_key(cache_prefix, self.user.id, 'friends_count')

        self._check_cache_key(cache_callable, cache_key)
        self._check_timeout(cache_key, 100)
        # noinspection PyTypeChecker
        self._check_cache_prefix(cache_callable, cache_prefix)
Ejemplo n.º 41
0
    def test_property_friends_count(self):
        self.assertEqual(self.user.friends_count, 15)

        cache_callable = lambda: getattr(self.user, 'friends_count')
        cache_callable.property = True
        cache_callable.invalidate_cache_by_prefix = User.friends_count.invalidate_cache_by_prefix

        cache_prefix = 'USER_PROPERTY'
        cache_key = create_cache_key(cache_prefix, self.user.id,
                                     'friends_count')

        self._check_cache_key(cache_callable, cache_key)
        self._check_timeout(cache_key, 100)
        # noinspection PyTypeChecker
        self._check_cache_prefix(cache_callable, cache_prefix)
Ejemplo n.º 42
0
    def test_invalidators(self):
        a, b = u'a', u'b'
        cache_callable = ordinal_func
        cache_prefix = ordinal_func.prefix
        cache_key = create_cache_key(cache_prefix, a, b)

        self.cache.reset_mock()

        result = process_args(a=a, b=b)

        self.assertEqual(cache_callable(a=a, b=b), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        # cached version
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.assertFalse(self.cache.called)
        self.cache.reset_mock()

        # invalidate cache via cache key
        invalidate_cache_key(cache_key)
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        # cached version
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.assertFalse(self.cache.called)
        self.cache.reset_mock()

        # invalidate cache via prefix
        invalidate_cache_prefix(cache_prefix)
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        # cached version
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.assertFalse(self.cache.called)
        self.cache.reset_mock()

        # invalidate cache via attached invalidator
        cache_callable.invalidate_cache_by_key(a=a, b=b)
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()
Ejemplo n.º 43
0
    def test_invalidators(self):
        a, b = u'a', u'b'
        cache_callable = ordinal_func
        cache_prefix = ordinal_func.prefix
        cache_key = create_cache_key(cache_prefix, a, b)

        self.cache.reset_mock()

        result = process_args(a=a, b=b)

        self.assertEqual(cache_callable(a=a, b=b), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        # cached version
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.assertFalse(self.cache.called)
        self.cache.reset_mock()

        # invalidate cache via cache key
        invalidate_cache_key(cache_key)
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        # cached version
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.assertFalse(self.cache.called)
        self.cache.reset_mock()

        # invalidate cache via prefix
        invalidate_cache_prefix(cache_prefix)
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()

        # cached version
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.assertFalse(self.cache.called)
        self.cache.reset_mock()

        # invalidate cache via attached invalidator
        cache_callable.invalidate_cache_by_key(a=a, b=b)
        self.assertEqual(cache_callable(a=a, b=b), result)
        self.cache.assert_called_once_with(result)
        self.cache.reset_mock()
Ejemplo n.º 44
0
    def test_default_cache_key_for_property(self):
        self.assertEqual(self.user.test_property, 'property')

        cache_callable = lambda: getattr(self.user, 'test_property')
        cache_callable.property = True

        cache_key = create_cache_key(__name__ + '.User.test_property')

        self._check_cache_key(cache_callable, cache_key)

        self.local_cache.clear()
        self.cache.reset_mock()

        self.assertEqual(self.user.test_property, 'property')
        self.cache.assert_called_once_with('property')

        self.cache.reset_mock()
        self.assertEqual(self.user.test_property, 'property')
        self.assertFalse(self.cache.called)

        # invalidate cache
        User.test_property.invalidate_cache_by_key()
        self.assertEqual(self.user.test_property, 'property')
        self.cache.assert_called_once_with('property')
Ejemplo n.º 45
0
def custom_cache_key(*args, **kwargs):
    return create_cache_key('my_prefix', args[0].id, *args[1:])
Ejemplo n.º 46
0
def custom_cache_key(*args, **kwargs):
    return create_cache_key('my_prefix', args[0].id, *args[1:])
Ejemplo n.º 47
0
class User(object):
    name = 'user_name'
    prefixed_ecached = partial(ecached, prefix='USER:{self.id}', timeout=3600)

    def __init__(self, uid):
        self.id = uid

    @ecached('dyn_timeout:{a}', timeout=choose_timeout)
    def instance_dynamic_timeout(self, a, b, c):
        return get_test_result(a, b, c)

    @ecached()
    def instance_default_cache_key(self, a, b, c=8):
        return get_test_result(a, b, c)

    @ecached()
    @classmethod
    def class_method_default_cache_key(cls, a, b, c=9, d='HAHA'):
        return get_test_result(a, b, c)

    @ecached_property()
    def test_property(self):
        return get_test_result('property')

    @ecached('{self.id}:{a}:{b}:{c}')
    def instance_method_string(self, a, b, c=10):
        return get_test_result(a, b, c)

    @ecached(['self.id', 'a', 'b'])
    def instance_method_list(self, a, b, c=11):
        return get_test_result(a, b, c)

    @ecached(custom_cache_key)
    def instance_method_callable(self, a, b, c=12):
        return get_test_result(a, b, c)

    @ecached('{self.id}:{a}:{b}', 400)
    def instance_method_timeout(self, a, b, c=13):
        return get_test_result(a, b, c)

    @ecached('{self.id}:{a}:{b}', 500, ('tag1', 'tag2'))
    def instance_method_tags(self, a, b, c=14):
        return get_test_result(a, b, c)

    @staticmethod
    def generate_custom_tags(meta):
        """ :type meta: MetaCallable """
        if meta.has_returned_value:
            cache_mock.assert_called_with(meta.returned_value)

        self = meta.args[0]
        a = meta.args[1]
        return [create_cache_key(self.name, self.id, a), 'simple_tag']

    @meta_accepted
    @staticmethod
    def generate_key_based_on_meta(m, a=1):
        assert isinstance(m, MetaCallable)
        assert m.function is getattr(m['self'],
                                     'instance_method_meta_test').function
        assert m.scope is m['self']
        assert a == 1

        return create_cache_key(m['a'], m['b'], m['c'])

    @ecached(generate_key_based_on_meta)
    def instance_method_meta_test(self, a, b, c=666):
        return get_test_result(a, b, c)

    @ecached('{a}:{b}', tags=generate_custom_tags)
    def instance_method_custom_tags(self, a, b, c=14):
        return get_test_result(a, b, c)

    @prefixed_ecached('p1:{a}:{b}:{c}', tags=['{self.id}:tag1'])
    def instance_method_prefixed(self, a, b, c=15):
        return get_test_result(a, b, c)

    @ecached_property('{self.id}:friends_count',
                      timeout=100,
                      prefix='USER_PROPERTY')
    def friends_count(self):
        cache_mock()
        return 15

    @ecached_property('static_key')
    def property_no_tags(self):
        cache_mock()
        return '42'

    @ecached(cache_key='{cls.name}:{c}')
    @classmethod
    def class_method_cache_key_string(cls, a, b, c=17):
        return get_test_result(a, b, c)

    @ecached(('cls.name', 'a'),
             500, ['tag4', 'tag5:{cls.name}'],
             prefix=lambda cls, *args, **kwargs: create_cache_key(
                 'USER', args[0], args[1]))
    @classmethod
    def class_method_full_spec(cls, a, b, c=18):
        return get_test_result(a, b, c)

    @ecached('{hg}:{hg}:{test}', prefix=u'пользователь')
    @staticmethod
    def static_method(hg, test='abc', n=1.1):
        return get_test_result(hg, test, n)

    @ecached(tags=['ttt:{c}'], prefix='ppp:{b}')
    @staticmethod
    def static_method_default_key(a, b, c=11):
        return get_test_result(a, b, c)