Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions diskcache/fanout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions tests/test_fanout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down