Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

Shelved Cache

Tests codecov PyPI version Downloads

Persistent cache implementation for Python cachetools.

Behaves like any Cache implementation, but entries are persisted to disk.

Original repository: https://github.com/mariushelf/shelved_cache

Usage example

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 the persistent_cached decorator:

from shelved_cache.decorators import persistent_cached
from shelved_cache import PersistentCache
from cachetools import LRUCache

filename = 'mycache'
pc = PersistentCache(LRUCache, filename, maxsize=2)

@persistent_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

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:

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:

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

Changelog

0.3.1

  • fix for Windows users
  • add Windows and MacOS to test suite

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 documentation

0.2.1

  • improved error handling

Acknowledgements

License

Author: Marius Helf (helfsmarius@gmail.com)

License: MIT -- see LICENSE

Metadata

Release files for shelved_cache_fixed 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shelved_cache_fixed 0.3.1
File Size Uploaded Provenance Actions
shelved_cache_fixed-0.3.1.tar.gz 7.0 kB Without provenance Details for shelved_cache_fixed-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for shelved_cache_fixed 0.3.1
File Interpreter ABI Platform Actions
shelved_cache_fixed-0.3.1-py3-none-any.whl Python 3 none any Details for shelved_cache_fixed-0.3.1-py3-none-any.whl

Total release size: 15.4 kB

Release files / shelved_cache_fixed-0.3.1.tar.gz

Download URL shelved_cache_fixed-0.3.1.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4eaf078a9a3588d47720f463ed819028a2861de50a67bb0b86b2f5465248fe26
BLAKE2b-256 checksum
How to use checksums
78f5d3f4cddfaa3ca9b74d9a4dd48f9d13e8e7075b1692087ef4261c6c4b57df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/5.15.0-122-generic

Release files / shelved_cache_fixed-0.3.1-py3-none-any.whl

Download URL shelved_cache_fixed-0.3.1-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66b749df6ced86f8e7fd84aaf4e73388e8975cd936a2dec69ade5ccbe951a31b
BLAKE2b-256 checksum
How to use checksums
b6546d9e5869ea805a02f9ce8c5a6697196f0b9f903fae191401de9549cdcd28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/5.15.0-122-generic

Security for shelved_cache_fixed 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page