python-function-caching
Provide a reusable pattern for memoizing deterministic function calls to reduce latency and repeated computation. Backed by the `python` diskcache package. Use skill when updating or editing everything.
When & Why to Use This Skill
The Python Function Caching skill provides a robust, disk-backed memoization pattern using the diskcache library. It is designed to significantly reduce latency and computational overhead by storing results of deterministic functions, making it an essential tool for optimizing expensive operations like LLM API calls and complex data processing.
Use Cases
- Cost and Latency Optimization: Cache expensive LLM API responses during development and testing to avoid redundant costs and speed up iteration cycles.
- Data Processing Efficiency: Store results of heavy computational tasks or data transformations to prevent re-calculating the same data across multiple runs.
- External Service Integration: Memoize calls to external REST APIs or third-party services where the data is deterministic, reducing network overhead and improving application responsiveness.
| name | python-function-caching |
|---|---|
| description | Provide a reusable pattern for memoizing deterministic function calls to reduce latency and repeated computation. Backed by the `python` diskcache package. Use skill when updating or editing everything. |
Python Function Caching Skill
Purpose
- Caching the function result. Works for pure functions that depend only on their inputs and have no side effects.
- Perfect for expensive calls (calculations, or calls to external services like LLM's).
Workflow
- Copy the contents of the file
assets/disk_cache.pyinto the project. - In the file update the
project_namein the linecache = Cache("~/.cache/project_name")to be the appropriate name. Also replaceproject_namefurther down in the docstring. - Use
@diskcache_decoratoror@diskcache_decorator(seconds-till-expired)for any function the user wants cached.- By default, the cache should not expire.
- If it makes sense that it should expire, 1 hour (3600 seconds seconds), 1 day (86400 seconds), or 1 week (604800 seconds) are common settings.
- Tricky part is invoking tests to make sure that the cache isn't used. Tests should use the
ignore_cache=Trueto get around this unless otherwise requested by the user.
Behavior
- Uses the function arguments to compute the hash
- Unless
ignore_cacheisTrue, the result is returned right away. - If the function is called, the result is always stored in the cache.
- Current code does not have an eviction policy.