Skip to content

Commit

Permalink
Fix cache policy bug (#4789)
Browse files Browse the repository at this point in the history
* Fix cache policy bug

* Fix test
  • Loading branch information
philippjfr authored May 5, 2023
1 parent f351189 commit 956de9b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion panel/io/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def _cleanup_cache(cache, policy, max_items, time):
their TTL (time-to-live) has expired.
"""
while len(cache) >= max_items:
if policy.lower() == 'lifo':
if policy.lower() == 'fifo':
key = list(cache.keys())[0]
elif policy.lower() == 'lru':
key = sorted(((k, time-t) for k, (_, _, _, t) in cache.items()),
Expand Down Expand Up @@ -329,6 +329,10 @@ def cache(
cache_dir: str
Directory to cache to on disk.
"""
if policy.lower() not in ('fifo', 'lru', 'lfu'):
raise ValueError(
f"Cache policy must be one of 'FIFO', 'LRU' or 'LFU', not {policy}."
)

hash_funcs = hash_funcs or {}
if func is None:
Expand Down
4 changes: 2 additions & 2 deletions panel/tests/io/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ def test_disk_cache():

@pytest.mark.xdist_group("cache")
@pytest.mark.parametrize('to_disk', (True, False))
def test_cache_lifo(to_disk):
def test_cache_fifo(to_disk):
if to_disk and diskcache is None:
pytest.skip('requires diskcache')
global OFFSET
OFFSET.clear()
fn = cache(function_with_args, max_items=2, policy='lifo', to_disk=to_disk)
fn = cache(function_with_args, max_items=2, policy='fifo', to_disk=to_disk)
assert fn(0, 0) == 0
assert fn(0, 1) == 1
assert fn(0, 0) == 0
Expand Down

0 comments on commit 956de9b

Please sign in to comment.