def test_unusual_character_key_creation(self): return_string('āęìøü') expected_key_unusual_chars = get_function_cache_key('function', 'tests.return_string', ('āęìøü',), {}) self.assertExpectedKeyInCache(expected_key_unusual_chars) return_string('aeiou') expected_key = get_function_cache_key('function', 'tests.return_string', ('aeiou',), {}) self.assertExpectedKeyInCache(expected_key) self.assertNotEqual(expected_key_unusual_chars, expected_key)
def test_same_function_name_from_module_level(self): """Two different functions with same name should have different cache keys""" Vegetable.foo(1, 2) vegetable_static_method_key = get_function_cache_key('function', 'tests.Vegetable.foo', (1, 2), {}) self.assertExpectedKeyInCache(vegetable_static_method_key) foo(1, 2) module_function_key = get_function_cache_key('function', 'tests.foo', (1, 2), {}) self.assertExpectedKeyInCache(module_function_key) self.assertNotEqual(vegetable_static_method_key, module_function_key)
def test_same_function_name_from_module_level(self): """Two different functions with same name should have different cache keys""" Vegetable.foo(1, 2) vegetable_static_method_key = get_function_cache_key( 'function', 'tests.Vegetable.foo', (1, 2), {}) self.assertExpectedKeyInCache(vegetable_static_method_key) foo(1, 2) module_function_key = get_function_cache_key('function', 'tests.foo', (1, 2), {}) self.assertExpectedKeyInCache(module_function_key) self.assertNotEqual(vegetable_static_method_key, module_function_key)
def test_unusual_character_key_creation(self): return_string('āęìøü') expected_key_unusual_chars = get_function_cache_key( 'function', 'tests.return_string', ('āęìøü', ), {}) self.assertExpectedKeyInCache(expected_key_unusual_chars) return_string('aeiou') expected_key = get_function_cache_key('function', 'tests.return_string', ('aeiou', ), {}) self.assertExpectedKeyInCache(expected_key) self.assertNotEqual(expected_key_unusual_chars, expected_key)
def test_same_static_method_name_different_class_class_reference(self): """ Two different classes with the same static method name should have different cache keys """ Fruit.static_method(self.cherry) fruit_static_method_key = get_function_cache_key('function', 'tests.Fruit.static_method', (self.cherry,), {}) self.assertExpectedKeyInCache(fruit_static_method_key) Vegetable.static_method(self.cherry) vegetable_static_method_key = get_function_cache_key('function', 'tests.Vegetable.static_method', (self.cherry,), {}) self.assertExpectedKeyInCache(vegetable_static_method_key) self.assertNotEqual(fruit_static_method_key, vegetable_static_method_key)
def test_same_static_method_name_different_class_instance_reference(self): """ Two different classes with the same static method name should have different cache keys """ self.apple.static_method(self.cherry) apple_static_method_key = get_function_cache_key('function', 'tests.Fruit.static_method', (self.cherry,), {}) self.assertExpectedKeyInCache(apple_static_method_key) self.celery.static_method(self.cherry) celery_static_method_key = get_function_cache_key('function', 'tests.Vegetable.static_method', (self.cherry,), {}) self.assertExpectedKeyInCache(celery_static_method_key) self.assertNotEqual(apple_static_method_key, celery_static_method_key)
def test_same_class_method_name_different_class(self): """ Two different classes with the same class method name should have different cache keys """ self.apple.add_sweet_letter(self.cherry) apple_add_sweet_cherry_key = get_function_cache_key('class_method', 'tests.Fruit.add_sweet_letter', (self.apple, self.cherry), {}) self.assertExpectedKeyInCache(apple_add_sweet_cherry_key) self.celery.add_sweet_letter(self.cherry) celery_add_sweet_cherry_key = get_function_cache_key('class_method', 'tests.Vegetable.add_sweet_letter', (self.celery, self.cherry), {}) self.assertExpectedKeyInCache(celery_add_sweet_cherry_key) self.assertNotEqual(apple_add_sweet_cherry_key, celery_add_sweet_cherry_key)
def test_same_method_name_different_class(self): """ Two different classes with the same method name should have different cache keys """ self.apple.take_then_give_back(self.cherry) apple_take_give_back_cherry_key = get_function_cache_key('method', 'tests.Fruit.take_then_give_back', (self.apple, self.cherry), {}) self.assertExpectedKeyInCache(apple_take_give_back_cherry_key) self.celery.take_then_give_back(self.cherry) celery_take_give_back_cherry_key = get_function_cache_key('method', 'tests.Vegetable.take_then_give_back', (self.celery, self.cherry), {}) self.assertExpectedKeyInCache(celery_take_give_back_cherry_key) self.assertNotEqual(apple_take_give_back_cherry_key, celery_take_give_back_cherry_key)
def test_same_static_method_name_different_class_instance_reference(self): """ Two different classes with the same static method name should have different cache keys """ self.apple.static_method(self.cherry) apple_static_method_key = get_function_cache_key( 'function', 'tests.Fruit.static_method', (self.cherry, ), {}) self.assertExpectedKeyInCache(apple_static_method_key) self.celery.static_method(self.cherry) celery_static_method_key = get_function_cache_key( 'function', 'tests.Vegetable.static_method', (self.cherry, ), {}) self.assertExpectedKeyInCache(celery_static_method_key) self.assertNotEqual(apple_static_method_key, celery_static_method_key)
def test_same_static_method_name_different_class_class_reference(self): """ Two different classes with the same static method name should have different cache keys """ Fruit.static_method(self.cherry) fruit_static_method_key = get_function_cache_key( 'function', 'tests.Fruit.static_method', (self.cherry, ), {}) self.assertExpectedKeyInCache(fruit_static_method_key) Vegetable.static_method(self.cherry) vegetable_static_method_key = get_function_cache_key( 'function', 'tests.Vegetable.static_method', (self.cherry, ), {}) self.assertExpectedKeyInCache(vegetable_static_method_key) self.assertNotEqual(fruit_static_method_key, vegetable_static_method_key)
def test_same_class_method_name_different_class(self): """ Two different classes with the same class method name should have different cache keys """ self.apple.add_sweet_letter(self.cherry) apple_add_sweet_cherry_key = get_function_cache_key( 'class_method', 'tests.Fruit.add_sweet_letter', (self.apple, self.cherry), {}) self.assertExpectedKeyInCache(apple_add_sweet_cherry_key) self.celery.add_sweet_letter(self.cherry) celery_add_sweet_cherry_key = get_function_cache_key( 'class_method', 'tests.Vegetable.add_sweet_letter', (self.celery, self.cherry), {}) self.assertExpectedKeyInCache(celery_add_sweet_cherry_key) self.assertNotEqual(apple_add_sweet_cherry_key, celery_add_sweet_cherry_key)
def test_same_method_name_different_class(self): """ Two different classes with the same method name should have different cache keys """ self.apple.take_then_give_back(self.cherry) apple_take_give_back_cherry_key = get_function_cache_key( 'method', 'tests.Fruit.take_then_give_back', (self.apple, self.cherry), {}) self.assertExpectedKeyInCache(apple_take_give_back_cherry_key) self.celery.take_then_give_back(self.cherry) celery_take_give_back_cherry_key = get_function_cache_key( 'method', 'tests.Vegetable.take_then_give_back', (self.celery, self.cherry), {}) self.assertExpectedKeyInCache(celery_take_give_back_cherry_key) self.assertNotEqual(apple_take_give_back_cherry_key, celery_take_give_back_cherry_key)
def wrapper(*args, **kwargs): function_cache_key = utils.get_function_cache_key(func_type, func_name, args, kwargs) cache_key = utils.get_hashed_cache_key(function_cache_key) try: value = cache.get(cache_key) except Exception: value = None if value is None: value = func(*args, **kwargs) # Try and set the key, value pair in the cache. # But if it fails on an error from the underlying # cache system, handle it. try: cache.set(cache_key, value, timeout) except CacheSetError: pass return value
def wrapper(*args, **kwargs): function_cache_key = utils.get_function_cache_key( func_type, func_name, args, kwargs) cache_key = utils.get_hashed_cache_key(function_cache_key) try: value = cache.get(cache_key) except Exception: value = None if value is None: value = func(*args, **kwargs) # Try and set the key, value pair in the cache. # But if it fails on an error from the underlying # cache system, handle it. try: cache.set(cache_key, value, timeout) except CacheSetError: pass return value