Skip to main content

Persistent cache for Python cachetools.

Project description

from shelved_cache import PersistentCache

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 cachetools' cached decorator:

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.

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:

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

Known issues

shelved-cache seems to run into permission errors on Windows machines with Python versions 3.13 and above.

Acknowledgements

License

Author: Marius Helf (helfsmarius@gmail.com)

License: MIT -- see LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shelved_cache-0.5.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shelved_cache-0.5.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file shelved_cache-0.5.0.tar.gz.

File metadata

  • Download URL: shelved_cache-0.5.0.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shelved_cache-0.5.0.tar.gz
Algorithm Hash digest
SHA256 6424ad2b016081f4c979871c90e83d2982ac9c43a10e98102fa77aeea797538c
MD5 3041c7e85b8f33a375d2083987b89c26
BLAKE2b-256 36c8379fd1cfaa0d567917c5e90faf8df208cd5f7fe08e395237b34efe418f6d

See more details on using hashes here.

File details

Details for the file shelved_cache-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: shelved_cache-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shelved_cache-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa474cacc28f5891d4bdafeb95facb23c08c191a8df7daf6db5d2401bc604448
MD5 cc65c4017f6b3e9a9dfcf81199ee9aa6
BLAKE2b-256 acc678d4c216f8e97a0b96c745ad11a084596a15547fe8035a00e3479c4b7349

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page