本文整理汇总了Python中aiocache.cached方法的典型用法代码示例。如果您正苦于以下问题:Python aiocache.cached方法的具体用法?Python aiocache.cached怎么用?Python aiocache.cached使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiocache
的用法示例。
在下文中一共展示了aiocache.cached方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_init
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def test_init(self):
c = cached(
ttl=1,
key="key",
key_builder="fn",
cache=SimpleMemoryCache,
plugins=None,
alias=None,
noself=False,
namespace="test",
)
assert c.ttl == 1
assert c.key == "key"
assert c.key_builder == "fn"
assert c.cache is None
assert c._cache == SimpleMemoryCache
assert c._serializer is None
assert c._kwargs == {"namespace": "test"}
示例2: reuse_data
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def reuse_data():
cache = Cache(serializer=JsonSerializer()) # Not ideal to define here
data = await cache.get("my_custom_key") # Note the key is defined in `cached` decorator
return data
示例3: decorator
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def decorator(self, mocker, mock_cache):
with patch("aiocache.decorators._get_cache", return_value=mock_cache):
yield cached()
示例4: test_fails_at_instantiation
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def test_fails_at_instantiation(self):
with pytest.raises(TypeError):
@cached(wrong_param=1)
async def fn(n):
return n
示例5: test_alias_takes_precedence
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def test_alias_takes_precedence(self, mock_cache):
with patch(
"aiocache.decorators.caches.get", MagicMock(return_value=mock_cache)
) as mock_get:
c = cached(alias="default", cache=SimpleMemoryCache, namespace="test")
c(stub)
mock_get.assert_called_with("default")
assert c.cache is mock_cache
示例6: test_keeps_signature
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def test_keeps_signature(self, mock_cache):
with patch("aiocache.decorators._get_cache", return_value=mock_cache):
@cached()
async def what(self, a, b):
return "1"
assert what.__name__ == "what"
assert str(inspect.signature(what)) == "(self, a, b)"
assert inspect.getfullargspec(what.__wrapped__).args == ["self", "a", "b"]
示例7: test_reuses_cache_instance
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def test_reuses_cache_instance(self):
with patch("aiocache.decorators._get_cache") as get_c:
cache = MagicMock(spec=BaseCache)
get_c.side_effect = [cache, None]
@cached()
async def what():
pass
await what()
await what()
assert get_c.call_count == 1
assert cache.get.call_count == 2
示例8: test_cache_per_function
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def test_cache_per_function(self):
@cached()
async def foo():
pass
@cached()
async def bar():
pass
assert foo.cache != bar.cache
示例9: test_inheritance
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def test_inheritance(self):
assert isinstance(cached_stampede(), cached)
示例10: test_cached_ttl
# 需要导入模块: import aiocache [as 别名]
# 或者: from aiocache import cached [as 别名]
def test_cached_ttl(self, cache):
@cached(ttl=1, key=pytest.KEY)
async def fn():
return str(random.randint(1, 50))
resp1 = await fn()
resp2 = await fn()
assert await cache.get(pytest.KEY) == resp1 == resp2
await asyncio.sleep(1)
assert await cache.get(pytest.KEY) is None