コード例 #1
0
    def test_alias_config_is_reusable(self):
        caches.set_config({
            'default': {
                'cache': "aiocache.RedisCache",
                'endpoint': "127.0.0.10",
                'port': 6378,
                'serializer': {
                    'class': "aiocache.serializers.PickleSerializer"
                },
                'plugins': [
                    {'class': "aiocache.plugins.HitMissRatioPlugin"},
                    {'class': "aiocache.plugins.TimingPlugin"}
                ]
            },
            'alt': {
                'cache': "aiocache.SimpleMemoryCache",
            }
        })

        default = caches.create(**caches.get_alias_config('default'))
        alt = caches.create(**caches.get_alias_config('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)
コード例 #2
0
    def test_alias_config_is_reusable(self):
        caches.set_config(
            {
                "default": {
                    "cache": "aiocache.RedisCache",
                    "endpoint": "127.0.0.10",
                    "port": 6378,
                    "serializer": {"class": "aiocache.serializers.PickleSerializer"},
                    "plugins": [
                        {"class": "aiocache.plugins.HitMissRatioPlugin"},
                        {"class": "aiocache.plugins.TimingPlugin"},
                    ],
                },
                "alt": {"cache": "aiocache.SimpleMemoryCache"},
            }
        )

        default = caches.create(**caches.get_alias_config("default"))
        alt = caches.create(**caches.get_alias_config("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)
コード例 #3
0
    def test_create_deprecated(self):
        with patch("aiocache.factory.warnings.warn") as mock:
            caches.create(cache="aiocache.SimpleMemoryCache")

        mock.assert_called_once_with(
            "Creating a cache with an explicit config is deprecated, use 'aiocache.Cache'",
            DeprecationWarning,
        )
コード例 #4
0
    def test_retrieve_cache_new_instance(self):
        caches.set_config({
            'default': {
                'cache':
                "aiocache.RedisCache",
                'endpoint':
                "127.0.0.10",
                'port':
                6378,
                'serializer': {
                    'class': "aiocache.serializers.PickleSerializer",
                    'encoding': 'encoding'
                },
                'plugins': [{
                    'class': "aiocache.plugins.HitMissRatioPlugin"
                }, {
                    'class': "aiocache.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
コード例 #5
0
    def test_retrieve_cache_new_instance(self):
        caches.set_config(
            {
                "default": {
                    "cache": "aiocache.RedisCache",
                    "endpoint": "127.0.0.10",
                    "port": 6378,
                    "serializer": {
                        "class": "aiocache.serializers.PickleSerializer",
                        "encoding": "encoding",
                    },
                    "plugins": [
                        {"class": "aiocache.plugins.HitMissRatioPlugin"},
                        {"class": "aiocache.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
コード例 #6
0
def caches():

    aiocaches.set_config({
        'default': {
            'cache': "aiocache.SimpleMemoryCache",
            'serializer': {
                'class': 'aiocache.serializers.JsonSerializer'
            }
        },
        'redis': {
            'cache': "aiocache.SimpleMemoryCache",
            'serializer': {
                'class': 'aiocache.serializers.JsonSerializer'
            }
        }
    })
    active_caches = [
        aiocaches.create(**aiocaches.get_alias_config('default')),
        aiocaches.create(**aiocaches.get_alias_config('redis'))
    ]
    return active_caches
コード例 #7
0
def caches(loop):
    aiocaches.set_config({
        'default': {
            'cache': "aiocache.SimpleMemoryCache",
            'serializer': {
                'class': 'aiocache.serializers.NullSerializer'
            }
        },
        'redis': {
            'cache': "aiocache.SimpleMemoryCache",
            'serializer': {
                'class': 'aiocache.serializers.NullSerializer'
            }
        }
    })
    active_caches = [
        aiocaches.create(**aiocaches.get_alias_config('default')),
        aiocaches.create(**aiocaches.get_alias_config('redis'))
    ]
    yield active_caches
    loop.run_until_complete(aiocaches.get('default').clear())
    loop.run_until_complete(aiocaches.get('redis').clear())
コード例 #8
0
 def test_create_extra_args(self):
     caches.set_config({
         'default': {
             'cache': "aiocache.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
コード例 #9
0
async def alt_cache():
    # This generates a new instance every time! You can also use `caches.create('alt')`
    # or even `caches.create('alt', namespace="test", etc...)` to override extra args
    cache = caches.create(**caches.get_alias_config('redis_alt'))
    await cache.set("key", "value")

    assert await cache.get("key") == "value"
    assert isinstance(cache, Cache.REDIS)
    assert isinstance(cache.serializer, PickleSerializer)
    assert len(cache.plugins) == 2
    assert cache.endpoint == "127.0.0.1"
    assert cache.timeout == 1
    assert cache.port == 6379
    await cache.close()
コード例 #10
0
    def __call__(self, f):
        if self.alias:
            self.cache = caches.create(self.alias)
        else:
            self.cache = _get_cache(cache=self._cache,
                                    serializer=self._serializer,
                                    plugins=self._plugins,
                                    **self._kwargs)

        @functools.wraps(f)
        async def wrapper(*args, **kwargs):
            return await self.decorator(f, *args, **kwargs)

        return wrapper
コード例 #11
0
 def test_create_extra_args(self):
     caches.set_config(
         {
             "default": {
                 "cache": "aiocache.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
コード例 #12
0
    def test_ensure_plugins_order(self):
        caches.set_config({
            'default': {
                'cache': "aiocache.RedisCache",
                'plugins': [
                    {'class': "aiocache.plugins.HitMissRatioPlugin"},
                    {'class': "aiocache.plugins.TimingPlugin"}
                ]
            }
        })

        cache = caches.get('default')
        assert isinstance(cache.plugins[0], HitMissRatioPlugin)

        cache = caches.create('default')
        assert isinstance(cache.plugins[0], HitMissRatioPlugin)
コード例 #13
0
    def test_ensure_plugins_order(self):
        caches.set_config(
            {
                "default": {
                    "cache": "aiocache.RedisCache",
                    "plugins": [
                        {"class": "aiocache.plugins.HitMissRatioPlugin"},
                        {"class": "aiocache.plugins.TimingPlugin"},
                    ],
                }
            }
        )

        cache = caches.get("default")
        assert isinstance(cache.plugins[0], HitMissRatioPlugin)

        cache = caches.create("default")
        assert isinstance(cache.plugins[0], HitMissRatioPlugin)
コード例 #14
0
 def test_create_not_reuse(self):
     assert caches.create('default') is not caches.create('default')
コード例 #15
0
    def test_get_wrong_alias(self):
        with pytest.raises(KeyError):
            caches.get("wrong_cache")

        with pytest.raises(KeyError):
            caches.create("wrong_cache")
コード例 #16
0
 def test_create_cache_ensure_alias_or_cache(self):
     with pytest.raises(TypeError):
         caches.create()
コード例 #17
0
    def test_create_cache_class_no_alias(self):
        cache = caches.create(cache=RedisCache)

        assert isinstance(cache, RedisCache)
        assert cache.endpoint == '127.0.0.1'
        assert cache.port == 6379
コード例 #18
0
    def test_create_cache_str_no_alias(self):
        cache = caches.create(cache="aiocache.RedisCache")

        assert isinstance(cache, RedisCache)
        assert cache.endpoint == "127.0.0.1"
        assert cache.port == 6379