1
0
mirror of https://github.com/quay/quay.git synced 2026-01-26 06:21:37 +03:00
Files
quay/data/cache/test/test_cache.py
Syed Mushtaq Ahmed df4ad94527 modelcache: Make ModelCache TTL configurable (PROJQUAY-1878) (#765)
Adds a configuration option to modify the cache expiry timeout
for ModelCache objects

Co-authored-by: Syed <syed@apache.org>
2021-04-28 10:46:32 -04:00

99 lines
2.6 KiB
Python

import pytest
from mock import patch
from data.cache import (
InMemoryDataModelCache,
NoopDataModelCache,
MemcachedModelCache,
RedisDataModelCache,
)
from data.cache.cache_key import CacheKey
DATA = {}
TEST_CACHE_CONFIG = {
"repository_blob_cache_ttl": "240s",
"catalog_page_cache_ttl": "240s",
"namespace_geo_restrictions_cache_ttl": "240s",
"active_repo_tags_cache_ttl": "240s",
"appr_applications_list_cache_ttl": "3600s",
"appr_show_package_cache_ttl": "3600s",
}
class MockClient(object):
def __init__(self, **kwargs):
pass
def get(self, key, default=None):
return DATA.get(key, default)
def set(self, key, value, expire=None):
DATA[key] = value
def close(self):
pass
@pytest.mark.parametrize(
"cache_type",
[
(NoopDataModelCache),
(InMemoryDataModelCache),
],
)
def test_caching(cache_type):
key = CacheKey("foo", "60m")
cache = cache_type(TEST_CACHE_CONFIG)
# Perform two retrievals, and make sure both return.
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
def test_memcache():
global DATA
DATA = {}
key = CacheKey("foo", "60m")
with patch("data.cache.impl.PooledClient", MockClient):
cache = MemcachedModelCache(TEST_CACHE_CONFIG, ("127.0.0.1", "-1"))
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
def test_memcache_should_cache():
global DATA
DATA = {}
key = CacheKey("foo", None)
def sc(value):
return value["a"] != 1234
with patch("data.cache.impl.PooledClient", MockClient):
cache = MemcachedModelCache(TEST_CACHE_CONFIG, ("127.0.0.1", "-1"))
assert cache.retrieve(key, lambda: {"a": 1234}, should_cache=sc) == {"a": 1234}
# Ensure not cached since it was `1234`.
assert cache._get_client_pool().get(key.key) is None
# Ensure cached.
assert cache.retrieve(key, lambda: {"a": 2345}, should_cache=sc) == {"a": 2345}
assert cache._get_client_pool().get(key.key) is not None
assert cache.retrieve(key, lambda: {"a": 2345}, should_cache=sc) == {"a": 2345}
def test_redis_cache():
global DATA
DATA = {}
key = CacheKey("foo", "60m")
with patch("data.cache.impl.StrictRedis", MockClient):
cache = RedisDataModelCache(TEST_CACHE_CONFIG, "127.0.0.1")
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}
assert cache.retrieve(key, lambda: {"a": 1234}) == {"a": 1234}