Skip to main content

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

Project description

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

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

memoshelve-1.0.6.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

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

memoshelve-1.0.6-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file memoshelve-1.0.6.tar.gz.

File metadata

  • Download URL: memoshelve-1.0.6.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"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}

File hashes

Hashes for memoshelve-1.0.6.tar.gz
Algorithm Hash digest
SHA256 8443b7919c15a6bb0ecb5dea8ffffb1d99d8be2bfcc24d7960613870b744b332
MD5 48444584ac7a1a23b42f6cbd792385e1
BLAKE2b-256 f04a5158f82d9e3dc9d3664cd94e8a98a25259fc823dd68af48bc271fbfd31e7

See more details on using hashes here.

File details

Details for the file memoshelve-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: memoshelve-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"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}

File hashes

Hashes for memoshelve-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 413856ed67a32e14ab4632a273cc5405cd6aff558b0b22ec58302da30e87d248
MD5 76dd5dc70fde08b6a37bac8166340726
BLAKE2b-256 d727b476202632f1f9f5cd0125b744d89797dbaacf4ef095df4f6f7f42cc462f

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