tilecache
darts_utils.tilecache
¶
Caching functionality for xarray datasets.
XarrayCacheManager
¶
Manager for caching xarray datasets.
Example
def process_tile(tile_id: str):
# Initialize cache manager
preprocess_cache = Path("preprocess_cache")
cache_manager = XarrayCacheManager(preprocess_cache)
def create_tile():
# Your existing tile creation logic goes here
return create_tile(...) # Replace with actual implementation
# Get cached tile or create and cache it
tile = cache_manager.get_or_create(
identifier=tile_id,
creation_func=create_tile
)
return tile
Initialize the cache manager.
Parameters:
Source code in darts-utils/src/darts_utils/tilecache.py
cache_dir
instance-attribute
¶
cache_dir = (
pathlib.Path(
darts_utils.tilecache.XarrayCacheManager(cache_dir)
)
if isinstance(
darts_utils.tilecache.XarrayCacheManager(cache_dir),
str,
)
else darts_utils.tilecache.XarrayCacheManager(cache_dir)
)
exists
¶
Check if a cached Dataset exists.
Parameters:
-
identifier
(str
) –Unique identifier for the cached file
Returns:
-
bool
(bool
) –True if the Dataset exists in cache, False otherwise
Source code in darts-utils/src/darts_utils/tilecache.py
get_or_create
¶
get_or_create(
identifier: str,
creation_func: callable,
force: bool,
*args: tuple[typing.Any, ...],
**kwargs: dict[str, typing.Any],
) -> xarray.Dataset
Get cached Dataset or create and cache it if it doesn't exist.
Parameters:
-
identifier
(str
) –Unique identifier for the cached file
-
creation_func
(callable
) –Function to create the Dataset if not cached
-
force
(bool
) –If True, forces reprocessing even if cached
-
*args
(tuple[typing.Any, ...]
, default:()
) –Arguments to pass to creation_func
-
**kwargs
(dict[str, typing.Any]
, default:{}
) –Keyword arguments to pass to creation_func
Returns:
Source code in darts-utils/src/darts_utils/tilecache.py
load_from_cache
¶
Load a Dataset from cache if it exists.
Parameters:
-
identifier
(str
) –Unique identifier for the cached file
Returns:
Source code in darts-utils/src/darts_utils/tilecache.py
save_to_cache
¶
Save a Dataset to cache.
Parameters:
-
dataset
(xarray.Dataset
) –Dataset to cache
-
identifier
(str
) –Unique identifier for the cached file
Returns:
-
bool
(bool
) –Success of operation