Skip to main content

memoshelve

A persistent memoization decorator using Python's shelve with two-tier caching (memory + disk).

Features

  • Two-tier caching: in-memory + persistent disk storage
  • Async and sync function support
  • Cache inspection and management
  • Optional enhanced serialization with dill and stablehash

Installation

pip install memoshelve

# For enhanced serialization
pip install memoshelve[robust]

Usage

Basic Decorator

from memoshelve import cache

@cache(filename="cache.db")
def expensive_function(x, y):
    return x * y + 42

result = expensive_function(10, 20)  # Computed and cached
result = expensive_function(10, 20)  # Retrieved from cache

Context Manager

from memoshelve import memoshelve

with memoshelve(expensive_function, "cache.db") as cached_fn:
    result = cached_fn(10, 20)

Async Functions

@cache(filename="async_cache.db")
async def async_function(data):
    return len(data) * 42

API

Cache Methods

@cache(filename="example.db")
def compute(x, y):
    return x ** y

# Check if cached
compute.__contains__(2, 3)

# Get without computing
compute.get(2, 3)  # Raises KeyError if not cached

# Get with status
result, status = compute.__call_with_status__(2, 3)
# status: "cached (mem)", "cached (disk)", or "miss"

# Manual operations
compute.put(2, 3, 8)      # Store value
compute.uncache(2, 3)     # Remove from cache

Configuration

@cache(
    filename="cache.db",
    ignore=["debug"],         # Ignore parameters in cache key
    get_hash=custom_hash,     # Custom hash function
    disable=False,            # Toggle caching
    print_cache_miss=True,    # Log cache misses
)
def my_function(data, debug=False):
    return process(data)

Cache Management

from memoshelve import compact

# Compact cache file
compact("cache.db", backup=True)

# Access metadata
metadata = my_function.memoshelve
metadata.disk_keys()     # Keys in disk cache
metadata.mem_keys()      # Keys in memory cache
metadata.compact()       # Compact this cache

Storage

Default cache location: ~/.cache/memoshelve/ (configurable via XDG_CACHE_HOME)

Cache files use Python's shelve module and may create multiple files (.db, .dir, .dat).

License

MIT License

Metadata

Release files for memoshelve 1.0.9

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

Source distribution (sdist)

Source distribution for memoshelve 1.0.9
File Size Uploaded Provenance Actions
memoshelve-1.0.9.tar.gz 25.9 kB Without provenance Details for memoshelve-1.0.9.tar.gz

Built distribution (wheel)

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

Total release size: 41.1 kB

Release files / memoshelve-1.0.9.tar.gz

Download URL memoshelve-1.0.9.tar.gz
Size 25.9 kB
Tags Source
SHA-256 checksum
How to use checksums
81a7a41c2e9e8efaaedbfe6c49609080f476cccf6d83d42ae0f47031a584557d
BLAKE2b-256 checksum
How to use checksums
ea1dc5c8e59583373b9b72268f690f188dbafc58626362066c32e602b4adf54b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / memoshelve-1.0.9-py3-none-any.whl

Download URL memoshelve-1.0.9-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
062ddc08db4f13d9e9077bd2ec0342a5727abad50d5a1b7146fd98ed61382315
BLAKE2b-256 checksum
How to use checksums
34c5bcfe5902181486780b202e3f2c12493bfa96393004182d9a4f3f9496b860
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for memoshelve 1.0.9

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.

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