diff --git a/aiocache/decorators.py b/aiocache/decorators.py index 9b87e6c7..b0174074 100644 --- a/aiocache/decorators.py +++ b/aiocache/decorators.py @@ -328,7 +328,7 @@ async def decorator( result = await f(*new_args, **kwargs) result.update(partial) - if cache_write: + if result and cache_write: if aiocache_wait_for_write: await self.set_in_cache(result, f, args, kwargs) else: diff --git a/tests/acceptance/test_decorators.py b/tests/acceptance/test_decorators.py index 188ab12b..c9d96954 100644 --- a/tests/acceptance/test_decorators.py +++ b/tests/acceptance/test_decorators.py @@ -1,7 +1,6 @@ import asyncio import pytest import random - from unittest import mock from aiocache import cached, cached_stampede, multi_cached @@ -110,6 +109,18 @@ async def test_multi_cached(self, cache): for key in default_keys: assert await cache.get(key) is not None + @pytest.mark.asyncio + async def test_multi_cached_with_empty_dict(self, mocker, cache): + mocker.spy(cache, "set") + + @multi_cached("keys") + async def fn(keys): + return {} + + await fn([pytest.KEY]) + assert await cache.exists(pytest.KEY) is False + assert cache.set.call_count == 0 + @pytest.mark.asyncio async def test_keys_without_kwarg(self, cache): @multi_cached("keys")