Registry / data / flexcache

flexcache

JSON →
library0.3pypypi✓ verified 25d ago

flexcache is a Python library (current version 0.3) designed to cache transformed versions of source objects to disk. It provides a robust and extensible framework for managing expensive calculations by storing their results persistently. The library allows for flexible cache invalidation strategies based on file modification time or content hash. It is currently in a pre-1.0 release, suggesting an active development cadence with potential for future changes.

pip install flexcache
INSTALL
IMPORT
SIG · FLEXCACHE
F
flexcache
datapythonv0.3
Install
1.8s avg
Import
164ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.170s · 18.2MB
glibc
py 3.103.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()
Debug
Known issues
gotchaThe library uses Python's `pickle` protocol for serialization of cached objects. Deserializing data from an untrusted source using `pickle` can lead to arbitrary code execution. Ensure that cached files are only loaded from trusted sources or implement custom, secure serialization.
fix
Only use `flexcache` with trusted input sources for cached data. For sensitive applications, consider implementing a custom cache (subclassing `DiskCache`) with a safer serialization method like JSON or a custom binary format for trusted, known data types.
affects: 0.1.0 - 0.3.0
gotchaChoosing the correct cache invalidation strategy (`DiskCacheByMTime` vs. `DiskCacheByHash`) is crucial. `DiskCacheByMTime` relies on file modification timestamps, which might not detect changes if a file's content is altered without updating its timestamp (e.g., atomic writes that replace the file).
fix
If the integrity of cached data is paramount and source files might change without MTime updates, use `DiskCacheByHash`. Otherwise, `DiskCacheByMTime` offers better performance for frequently updated files.
affects: 0.1.0 - 0.3.0
breakingAs `flexcache` is in a pre-1.0 version (0.3), its API and internal behavior may not be fully stable. Future minor releases (e.g., 0.4, 0.5) might introduce breaking changes without strict adherence to semantic versioning until a 1.0 release.
fix
Pin the `flexcache` version in your `requirements.txt` (e.g., `flexcache==0.3.0`) and review release notes thoroughly before upgrading to any new minor version.
affects: <1.0.0
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.
fix
Install 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.
fix
The 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.
fix
Ensure 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.
fix
Ensure 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.
Agent activity
8 hits · last 30 days
node
6
Resources
flexcache — pip install flexcache · libregistry