def test_alias(): default_cache() alt_cache() cache = RedisCache() cache.delete("key") cache.close() caches.get('default').close()
def test_set_config_updates_existing_values(self): assert not isinstance(caches.get("default").serializer, JsonSerializer) caches.set_config({ "default": { "cache": "pycached.SimpleMemoryCache", "serializer": { "class": "pycached.serializers.JsonSerializer" }, } }) assert isinstance(caches.get("default").serializer, JsonSerializer)
def test_retrieve_cache(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "endpoint": "127.0.0.10", "port": 6378, "ttl": 10, "serializer": { "class": "pycached.serializers.PickleSerializer", "encoding": "encoding", }, "plugins": [ { "class": "pycached.plugins.HitMissRatioPlugin" }, { "class": "pycached.plugins.TimingPlugin" }, ], } }) cache = caches.get("default") assert isinstance(cache, RedisCache) assert cache.endpoint == "127.0.0.10" assert cache.port == 6378 assert cache.ttl == 10 assert isinstance(cache.serializer, PickleSerializer) assert cache.serializer.encoding == "encoding" assert len(cache.plugins) == 2
def default_cache(): cache = caches.get('default') # This always returns the same instance cache.set("key", "value") assert cache.get("key") == "value" assert isinstance(cache, SimpleMemoryCache) assert isinstance(cache.serializer, StringSerializer)
def test_set_config_removes_existing_caches(self): caches.set_config({ "default": { "cache": "pycached.SimpleMemoryCache" }, "alt": { "cache": "pycached.SimpleMemoryCache" }, }) caches.get("default") caches.get("alt") assert len(caches._caches) == 2 caches.set_config({ "default": { "cache": "pycached.SimpleMemoryCache" }, "alt": { "cache": "pycached.SimpleMemoryCache" }, }) assert caches._caches == {}
def __call__(self, f): if self.alias: self.cache = caches.get(self.alias) else: self.cache = _get_cache(cache=self._cache, serializer=self._serializer, plugins=self._plugins, **self._kwargs) @functools.wraps(f) def wrapper(*args, **kwargs): return self.decorator(f, *args, **kwargs) wrapper.cache = self.cache return wrapper
def test_multiple_caches(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "endpoint": "127.0.0.10", "port": 6378, "serializer": { "class": "pycached.serializers.PickleSerializer" }, "plugins": [ { "class": "pycached.plugins.HitMissRatioPlugin" }, { "class": "pycached.plugins.TimingPlugin" }, ], }, "alt": { "cache": "pycached.SimpleMemoryCache" }, }) default = caches.get("default") alt = caches.get("alt") assert isinstance(default, RedisCache) assert default.endpoint == "127.0.0.10" assert default.port == 6378 assert isinstance(default.serializer, PickleSerializer) assert len(default.plugins) == 2 assert isinstance(alt, SimpleMemoryCache)
def test_ensure_plugins_order(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "plugins": [ { "class": "pycached.plugins.HitMissRatioPlugin" }, { "class": "pycached.plugins.TimingPlugin" }, ], } }) cache = caches.get("default") assert isinstance(cache.plugins[0], HitMissRatioPlugin) cache = caches.create("default") assert isinstance(cache.plugins[0], HitMissRatioPlugin)
def test_reuse_instance(self): assert caches.get("default") is caches.get("default")
def test_get_wrong_alias(self): with pytest.raises(KeyError): caches.get("wrong_cache") with pytest.raises(KeyError): caches.create("wrong_cache")