diff --git a/diskcache/fanout.py b/diskcache/fanout.py index 9822ee4..f806c76 100644 --- a/diskcache/fanout.py +++ b/diskcache/fanout.py @@ -12,6 +12,14 @@ from .core import DEFAULT_SETTINGS, ENOVAL, Cache, Disk, Timeout from .persistent import Deque, Index +def _default_with_meta(default, expire_time, tag): + """Match Cache.get/pop: wrap default when expire_time or tag is requested.""" + if expire_time and tag: + return (default, None, None) + if expire_time or tag: + return (default, None) + return default + class FanoutCache: """Cache that shards keys and values.""" @@ -284,7 +292,7 @@ def get( try: return shard.get(key, default, read, expire_time, tag, retry) except (Timeout, sqlite3.OperationalError): - return default + return _default_with_meta(default, expire_time, tag) def __getitem__(self, key): """Return corresponding value for `key` from cache. @@ -350,7 +358,7 @@ def pop( try: return shard.pop(key, default, expire_time, tag, retry) except Timeout: - return default + return _default_with_meta(default, expire_time, tag) def delete(self, key, retry=False): """Delete corresponding item for `key` from cache. diff --git a/tests/test_fanout.py b/tests/test_fanout.py index af221b6..16ebb22 100644 --- a/tests/test_fanout.py +++ b/tests/test_fanout.py @@ -316,6 +316,34 @@ def test_pop_timeout(cache): assert cache.pop(0) is None +def test_get_timeout_expire_time(cache): + shards = mock.Mock() + shard = mock.Mock() + get_func = mock.Mock() + + shards.__getitem__ = mock.Mock(side_effect=lambda key: shard) + shard.get = get_func + get_func.side_effect = dc.Timeout + + with mock.patch.object(cache, '_shards', shards): + assert cache.get(0, expire_time=True) == (None, None) + assert cache.get(0, default=1, expire_time=True, tag=True) == (1, None, None) + + +def test_pop_timeout_expire_time(cache): + shards = mock.Mock() + shard = mock.Mock() + pop_func = mock.Mock() + + shards.__getitem__ = mock.Mock(side_effect=lambda key: shard) + shard.pop = pop_func + pop_func.side_effect = dc.Timeout + + with mock.patch.object(cache, '_shards', shards): + assert cache.pop(0, expire_time=True) == (None, None) + assert cache.pop(0, default=1, expire_time=True, tag=True) == (1, None, None) + + def test_delete_timeout(cache): shards = mock.Mock() shard = mock.Mock()