freshcrate
Skin:/
Home > Frameworks > shelved-cache

shelved-cache

Persistent cache for Python cachetools.

Why this rank:Strong adoptionRelease freshnessHealthy release cadence

Description

from shelved_cache import PersistentCache # Shelved Cache [![Tests](https://github.com/mariushelf/shelved_cache/actions/workflows/cicd.yaml/badge.svg)](https://github.com/mariushelf/shelved_cache/actions/workflows/cicd.yaml) [![codecov](https://codecov.io/gh/mariushelf/shelved_cache/branch/master/graph/badge.svg)](https://codecov.io/gh/mariushelf/shelved_cache) [![PyPI version](https://badge.fury.io/py/shelved_cache.svg)](https://pypi.org/project/shelved_cache/) [![Downloads](https://static.pepy.tech/badge/shelved-cache/month)](https://pepy.tech/project/shelved-cache) Persistent cache implementation for Python [cachetools](https://github.com/tkem/cachetools/). Behaves like any `Cache` implementation, but entries are persisted to disk. Original repository: [https://github.com/mariushelf/shelved_cache](https://github.com/mariushelf/shelved_cache) # Usage example ```python from shelved_cache import PersistentCache from cachetools import LRUCache filename = 'mycache' # create persistency around an LRUCache pc = PersistentCache(LRUCache, filename=filename, maxsize=2) # we can now use the cache like a normal LRUCache. # But: the cache is persisted to disk. pc["a"] = 42 pc["b"] = 43 assert pc["a"] == 42 assert pc["b"] == 43 # close the file pc.close() # Now in the same script or in another script, we can re-load the cache: pc2 = PersistentCache(LRUCache, filename=filename, maxsize=2) assert pc2["a"] == 42 assert pc2["b"] == 43 ``` ## Use as a decorator Just like a regular `cachetools.Cache`, the `PersistentCache` can be used with `cachetools`' `cached` decorator: ```python import cachetools from shelved_cache import PersistentCache from cachetools import LRUCache filename = 'mycache' pc = PersistentCache(LRUCache, filename, maxsize=2) @cachetools.cached(pc) def square(x): print("called") return x * x assert square(3) == 9 # outputs "called" assert square(3) == 9 # no output because the cache is used ``` ## Note: decorating multiple functions If you want to decorate multiple functions, you need to use a new instance of `PersistentCache` for each function. Make sure that each cache uses a different file name. ```python import cachetools from shelved_cache import PersistentCache from cachetools import LRUCache @cachetools.cached(PersistentCache(LRUCache, "square.cache", maxsize=100)) def square(x): return x * x @cachetools.cached(PersistentCache(LRUCache, "cube.cache", maxsize=100)) def cube(x): return x * x * x assert square(2) == 4 assert cube(2) == 8 ``` # Features ## persistent cache See usage examples above. ## Async decorators The package contains equivalents for `cachetools`' `cached` and `cachedmethod` decorators which support wrapping async methods. You can find them in the `decorators` submodule. They support both synchronous *and* asynchronous functions and methods. Examples: ```python from shelved_cache import cachedasyncmethod from cachetools import LRUCache class A: # decorate an async method: @cachedasyncmethod(lambda self: LRUCache(2)) async def asum(self, a, b): return a + b a = A() assert await a.asum(1, 2) == 3 class S: @cachedasyncmethod(lambda self: LRUCache(2)) def sum(self, a, b): return a + b s = S() assert s.sum(1, 2) == 3 ``` ## Support for lists as function arguments Using the `autotuple_hashkey` function, list arguments are automatically converted to tuples, so that they support hashing. Example: ```python from cachetools import cached, LRUCache from shelved_cache.keys import autotuple_hashkey @cached(LRUCache(2), key=autotuple_hashkey) def sum(values): return values[0] + values[1] # fill cache assert sum([1, 2]) == 3 # access cache assert sum([1, 2]) == 3 ``` # Known issues shelved-cache seems to run into permission errors on Windows machines with Python versions 3.13 and above. # Acknowledgements * [cachetools](https://github.com/tkem/cachetools/) by Thomas Kemmer * [asyncache](https://github.com/hephex/asyncache) by hephex # License Author: Marius Helf ([helfsmarius@gmail.com](mailto:helfsmarius@gmail.com)) License: MIT -- see [LICENSE](LICENSE)

Release History

VersionChangesUrgencyDate
0.5.0Imported from PyPI (0.5.0)Low4/21/2026
0.4.0## What's Changed * drop support for Python 3.7 and 3.8 * add support for Python 3.12 and 3.13 * note: shelved_cache does not seem to work with Python 3.13 on Windows * note about decorating multiple functions in the README * improvement in async decorators * Use ruff by @mariushelf in https://github.com/mariushelf/shelved_cache/pull/5 * Fix cachedasyncmethod to match cachedmethod and use async with by @alex-linx in https://github.com/mariushelf/shelved_cache/pull/6 ## New ContributoLow10/19/2024
0.3.1* fix for Windows users * add Windows and MacOS to test suiteLow2/24/2023
0.3.0* add support for Python 3.10 and 3.11 * better error message when trying to use the same file for multiple caches * CI/CD pipeline * fixes for documentationLow2/18/2023

Dependencies & License Audit

Loading dependencies...

Similar Packages

schemathesisProperty-based testing framework for Open API and GraphQL based appsv4.21.1
ctranslate2Fast inference engine for Transformer modelsv4.8.0
cadwynProduction-ready community-driven modern Stripe-like API versioning in FastAPI7.0.0
tqdmFast, Extensible Progress Meterv4.68.1
inspect-aiFramework for large language model evaluationsmain@2026-06-05

More in Frameworks

spec_driven_developSpec-Driven Develop is a platform-agnostic AI agent skill that automates the pre-development workflow for large-scale complex tasks. It is not a framework, not a runtime, not a package manager — it is
deer-flowAn open-source long-horizon SuperAgent harness that researches, codes, and creates. With the help of sandboxes, memories, tools, skill, subagents and message gateway, it handles different levels of ta
tqdmFast, Extensible Progress Meter
simBuild, deploy, and orchestrate AI agents. Sim is the central intelligence layer for your AI workforce.