def test_retrieve_cache_new_instance(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "endpoint": "127.0.0.10", "port": 6378, "serializer": { "class": "pycached.serializers.PickleSerializer", "encoding": "encoding", }, "plugins": [ { "class": "pycached.plugins.HitMissRatioPlugin" }, { "class": "pycached.plugins.TimingPlugin" }, ], } }) cache = caches.create("default") assert isinstance(cache, RedisCache) assert cache.endpoint == "127.0.0.10" assert cache.port == 6378 assert isinstance(cache.serializer, PickleSerializer) assert cache.serializer.encoding == "encoding" assert len(cache.plugins) == 2
def reset_caches(): caches.set_config({ "default": { "cache": "pycached.SimpleMemoryCache", "serializer": { "class": "pycached.serializers.NullSerializer" }, } })
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_create_extra_args(self): caches.set_config({ "default": { "cache": "pycached.RedisCache", "endpoint": "127.0.0.9", "db": 10, "port": 6378, } }) cache = caches.create("default", namespace="whatever", endpoint="127.0.0.10", db=10) assert cache.namespace == "whatever" assert cache.endpoint == "127.0.0.10" assert cache.db == 10
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_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 test_set_config_no_default(self): with pytest.raises(ValueError): caches.set_config({ "no_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" }, ], } })
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_set_empty_config(self): with pytest.raises(ValueError): caches.set_config({})
from pycached import caches, SimpleMemoryCache, RedisCache from pycached.serializers import StringSerializer, PickleSerializer caches.set_config({ 'default': { 'cache': "pycached.SimpleMemoryCache", 'serializer': { 'class': "pycached.serializers.StringSerializer" } }, 'redis_alt': { 'cache': "pycached.RedisCache", 'endpoint': "127.0.0.1", 'port': 6379, 'timeout': 1, 'serializer': { 'class': "pycached.serializers.PickleSerializer" }, 'plugins': [ {'class': "pycached.plugins.HitMissRatioPlugin"}, {'class': "pycached.plugins.TimingPlugin"} ] } }) def default_cache(): cache = caches.get('default') # This always returns the same instance cache.set("key", "value") assert cache.get("key") == "value"