Skip to content

RF: add a helper add_cache to avoid duplication (.name) and allow for formalized addition of caches #1227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 4, 2023
Merged
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
43 changes: 34 additions & 9 deletions fsspec/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,14 +767,39 @@ def _read_cache(self, start, end, start_block_number, end_block_number):


caches = {
"none": BaseCache,
# one custom case
None: BaseCache,
"mmap": MMapCache,
"bytes": BytesCache,
"readahead": ReadAheadCache,
"block": BlockCache,
"first": FirstChunkCache,
"all": AllBytes,
"parts": KnownPartsOfAFile,
"background": BackgroundBlockCache,
}


def register_cache(cls, clobber=False):
"""'Register' cache implementation.

Parameters
----------
clobber: bool, optional
If set to True (default is False) - allow to overwrite existing
entry.

Raises
------
ValueError
"""
name = cls.name
if not clobber and name in caches:
raise ValueError(f"Cache with name {name!r} is already known: {caches[name]}")
caches[name] = cls


for c in (
BaseCache,
MMapCache,
BytesCache,
ReadAheadCache,
BlockCache,
FirstChunkCache,
AllBytes,
KnownPartsOfAFile,
BackgroundBlockCache,
):
register_cache(c)
9 changes: 8 additions & 1 deletion fsspec/tests/test_caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from fsspec.caching import BlockCache, FirstChunkCache, caches
from fsspec.caching import BlockCache, FirstChunkCache, caches, register_cache


def test_cache_getitem(Cache_imp):
Expand Down Expand Up @@ -138,3 +138,10 @@ def wrapped(*a, **kw):
f.read(1)
time.sleep(0.1) # second block is loading
assert len(thread_ids) == 2


def test_register_cache():
# just test that we have them populated and fail to re-add again unless overload
with pytest.raises(ValueError):
register_cache(BlockCache)
register_cache(BlockCache, clobber=True)