def test_cache_decorator_basic_apis(redis_unix_socket_path): cache = Cache(redis_options={'unix_socket_path': redis_unix_socket_path}) @cache.cache() def load(name, offset): return ' '.join(('load', name, offset)) assert load('name', 'offset') == 'load name offset' assert load('name', offset='offset') == 'load name offset' @cache.cache() def load(name, offset): return ' '.join(('load02', name, offset)) assert load('name', 'offset') == 'load name offset' assert load('name', offset='offset') == 'load name offset' assert cache.invalidate(load, 'name', 'offset') assert load('name', 'offset') == 'load02 name offset' assert load('name', offset='offset') == 'load name offset' assert cache.invalidate(load, 'name', offset='offset') assert load('name', offset='offset') == 'load02 name offset' class Foo(object): @cache.cache() def load_method(self, name, offset): return ' '.join(('load', name, str(offset))) foo = Foo() assert foo.load_method('name', 10) == 'load name 10' assert foo.load_method('name', offset=10) == 'load name 10' assert cache.invalidate(foo.load_method, 'name', 10)
def test_cache_namespace(redis_unix_socket_path): cache01 = Cache(redis_options={'unix_socket_path': redis_unix_socket_path}) cache02 = Cache( namespace='test:', redis_options={'unix_socket_path': redis_unix_socket_path}) assert cache01.set('key', 'value') assert cache01.get('key') == 'value' assert cache02.get('key') is None
def test_cache_namespace(redis_unix_socket_path): cache01 = Cache(redis_options={'unix_socket_path': redis_unix_socket_path}) cache02 = Cache(namespace='test:', redis_options={'unix_socket_path': redis_unix_socket_path}) assert cache01.set('key', 'value') assert cache01.get('key') == 'value' assert cache02.get('key') is None
def test_cache_decorator_basic_apis(redis_unix_socket_path): cache = Cache(redis_options={'unix_socket_path': redis_unix_socket_path}) @cache.cache() def load(name, offset): return ' '.join(('load', name, offset)) rv = load('name', 'offset') assert isinstance(rv, unicode) assert rv == 'load name offset' assert load('name', offset='offset') == 'load name offset' @cache.cache() def load(name, offset): return ' '.join(('load02', name, offset)) assert load('name', 'offset') == 'load name offset' assert load('name', offset='offset') == 'load name offset' assert cache.invalidate(load, 'name', 'offset') assert load('name', 'offset') == 'load02 name offset' assert load('name', offset='offset') == 'load name offset' assert cache.invalidate(load, 'name', offset='offset') assert load('name', offset='offset') == 'load02 name offset' class Foo(object): @cache.cache() def load_method(self, name, offset): return ' '.join(('load', name, str(offset))) foo = Foo() assert foo.load_method('name', 10) == 'load name 10' assert foo.load_method('name', offset=10) == 'load name 10' class Foo(object): @cache.cache() def load_method(self, name, offset): return ' '.join(('load02', name, str(offset))) foo = Foo() assert foo.load_method('name', 10) == 'load name 10' assert cache.invalidate(foo.load_method, 'name', 10) assert foo.load_method('name', 10) == 'load02 name 10'
def test_cache_decorator_include_self(redis_unix_socket_path): cache = Cache(redis_options={'unix_socket_path': redis_unix_socket_path}) class User(object): def __init__(self, user_id): self.user_id = user_id def __str__(self): return '<User %s>' % self.user_id @cache.cache(include_self=True) def load(self, name, offset): return ' '.join(('load', name, str(offset))) user01 = User(1) user02 = User(2) assert user01.load('name', 'offset') == 'load name offset' assert user02.load('name', 'offset') == 'load name offset' class User(object): def __init__(self, user_id): self.user_id = user_id def __str__(self): return '<User %s>' % self.user_id @cache.cache(include_self=True) def load(self, name, offset): return ' '.join(('load02', name, str(offset))) user01 = User(1) user02 = User(2) assert user01.load('name', 'offset') == 'load name offset' assert user02.load('name', 'offset') == 'load name offset' assert cache.invalidate(user01.load, 'name', 'offset') assert user01.load('name', 'offset') == 'load02 name offset' assert user02.load('name', 'offset') == 'load name offset'
def test_cache_batch_mode(redis_unix_socket_path): cache = Cache(redis_options={'unix_socket_path': redis_unix_socket_path}) @cache.cache() def cache_batch_test_func(value): return value with cache.batch_mode(): pass results = [] with cache.batch_mode(): for i in range(10): rv = cache_batch_test_func(i) assert rv.is_pending assert rv.value is None results.append(rv) for i, rv in enumerate(results): assert rv.is_resolved assert rv.value == i for i in range(20): assert cache_batch_test_func(i) == i
def test_cache_bypass_values(redis_unix_socket_path): cache = Cache(redis_options={'unix_socket_path': redis_unix_socket_path}, bypass_values=[None]) @cache.cache() def bypass_values_test_func(): return None assert bypass_values_test_func() is None @cache.cache() def bypass_values_test_func(): return 1 assert bypass_values_test_func() is not None
def test_cache_basic_apis(redis_unix_socket_path): cache = Cache(redis_options={'unix_socket_path': redis_unix_socket_path}) assert cache.get('key') is None assert cache.set('key', 'value') assert cache.get('key') == 'value' assert cache.delete('key') assert cache.get('key') is None assert cache.get_many('key1', 'key2') == [None, None] assert cache.set_many({'key1': 'value1', 'key2': 'value2'}) assert cache.get_many('key1', 'key2') == ['value1', 'value2'] assert cache.delete_many('key1', 'key2') assert cache.get_many('key1', 'key2') == [None, None] assert cache.get_many() == [] assert cache.set_many({}) assert cache.delete_many() assert cache.get('key') is None assert cache.set('key', ['value']) assert cache.get('key') == ['value'] assert cache.get_many('key') == [['value']] assert cache.delete('key') assert cache.get('key') is None