Install & Compatibility
Where this runs
tested against v0.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.170s · 18.2MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.8s · import 0.158s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
DiskCacheByMTime
✓ from flexcache import DiskCacheByMTime
Commonly used for caching where invalidation is based on file modification time.
DiskCacheByHash
✓ from flexcache import DiskCacheByHash
Used when cache invalidation requires detecting changes via file content hash, offering stronger integrity checks than modification time.
DiskCache
✓ from flexcache import DiskCache
Base class for implementing custom disk caching logic.
This quickstart demonstrates how to initialize a `DiskCacheByMTime` instance, define an expensive 'reader' function, and use the `load` method to automatically cache and retrieve transformed data. It also shows how modifications to the source object invalidate the cache, triggering a re-execution of the reader. Ensure `os` is imported for the quickstart to correctly handle environment variables for cache directory paths.
import pathlib
from flexcache import DiskCacheByMTime
# Create a cache directory (ensure it exists)
cache_dir_path = pathlib.Path(os.environ.get('FLEXCACHE_CACHE_DIR', './my_flexcache_cache'))
cache_dir_path.mkdir(parents=True, exist_ok=True)
dc = DiskCacheByMTime(cache_folder=cache_dir_path)
def expensive_parser(source_path: pathlib.Path) -> str:
"""Simulates an expensive operation that reads and transforms a file."""
print(f"[Parser] Reading and processing: {source_path}")
return source_path.read_text().upper()
# Create a dummy source file
source_file_path = pathlib.Path("source.txt")
source_file_path.write_text("Hello, Flexcache World!")
# First call: `expensive_parser` will be executed and result cached
print("\n--- First call ---")
parsed_content_1 = dc.load(source_file_path, reader=expensive_parser)
print(f"Result: {parsed_content_1}")
# Second call: result will be loaded from cache without executing `expensive_parser`
print("\n--- Second call (from cache) ---")
parsed_content_2 = dc.load(source_file_path, reader=expensive_parser)
print(f"Result: {parsed_content_2}")
# Modify the source file to invalidate the cache (for DiskCacheByMTime)
source_file_path.write_text("UPDATED content for Flexcache!")
# Third call: `expensive_parser` will be re-executed due to source modification
print("\n--- Third call (cache invalidated) ---")
parsed_content_3 = dc.load(source_file_path, reader=expensive_parser)
print(f"Result: {parsed_content_3}")
# Clean up generated files and directory
source_file_path.unlink()
for item in cache_dir_path.iterdir():
if item.is_file():
item.unlink()
cache_dir_path.rmdir()
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'flexcache'
The 'flexcache' library has not been installed in your Python environment or the environment where your script is being run.
fixInstall the library using pip: `pip install flexcache`
from flexcache import DiskCache
This is a common search query when developers are looking for the correct way to import core components, or encountering an issue where `DiskCache` might not be directly available or named differently in their installed version, leading to an ImportError or AttributeError if they guess incorrectly.
fixThe correct import for the main disk caching class is `from flexcache import DiskCache` or `from flexcache import DiskCacheByMTime` / `DiskCacheByHash` for specific invalidation strategies. Ensure `flexcache` is installed.
TypeError: Missing 1 required positional argument: 'func'
This error typically occurs when a caching decorator or function expects a callable (the function to be cached) but does not receive it, often due to incorrect application of the decorator or direct function call.
fixEnsure the `@cache_decorator` (or equivalent) is correctly applied directly above a function definition, or that you are passing a callable function as the required argument when calling a caching utility directly.
FileNotFoundError: [Errno 2] No such file or directory: '/path/to/cache/directory'
The specified cache directory either does not exist or the Python process lacks the necessary permissions to create or access it.
fixEnsure the `cache_dir` argument (or equivalent configuration) points to an existing and writeable directory, or that the Python script has permissions to create the directory if it doesn't exist.
Upgrade
Version history
0.3latest on PyPI · released Mar 9, 2024
Audit
Dependencies
typing_extensionsrequiredType hinting utilities, likely for compatibility across Python versions or advanced type features.