Skip to main content

persistent decorators mirroring functools.lru_cache

Project description

PicoCache: Persistent Memoization

A Persistent, datastore‑backed lru_cache for Python.
PicoCache gives you the ergonomics of functools.lru_cache while keeping your cached values safe across process restarts and even across machines.
PicoCache ships with a zero‑dependency SQLiteCache that relies only on the standard‑library sqlite3 module. Additional back‑ends can be enabled via extras:

  • SQLAlchemyCache – persists to any SQL database supported by SQLAlchemy.
  • RedisCache – stores values in Redis, ideal for distributed deployments.
  • DjangoCache – plugs straight into Django’s configured cache backend (Memcached, Redis, database, etc.).

Why PicoCache?

  • Familiar API – decorators feel identical to functools.lru_cache.
  • Durable – survive restarts, scale horizontally.
  • Introspectablecache_info() and cache_clear() just like the standard library.
  • Zero boilerplate – pass a connection URL and start decorating.

Installation

# core (built‑in SQLiteCache, no external deps)
pip install picocache

# optional extras
pip install picocache[redis]        # RedisCache
pip install picocache[sqlalchemy]   # SQLAlchemyCache
pip install picocache[django]       # DjangoCache
# or any combination, e.g.
pip install "picocache[redis,sqlalchemy]"

Quick‑start

1. Built‑in SQLiteCache (no external deps)

from picocache import SQLiteCache

cache = SQLiteCache()     # defaults to ./picocache.db

@cache
def fib(n: int) -> int:
    return n if n < 2 else fib(n - 1) + fib(n - 2)

2. SQLAlchemy back‑end

from picocache import SQLAlchemyCache

# Create the decorator bound to an SQLite file
sql_cache = SQLAlchemyCache("sqlite:///cache.db")

@sql_cache(maxsize=256)        # feels just like functools.lru_cache
def fib(n: int) -> int:
    return n if n < 2 else fib(n - 1) + fib(n - 2)

3. Redis

from picocache import RedisCache

redis_cache = RedisCache("redis://localhost:6379/0")

@redis_cache(maxsize=128, typed=True)
def slow_add(a: int, b: int) -> int:
    print("Executing body…")
    return a + b

On the second call with the same arguments, slow_add() returns instantly and “Executing body…” is not printed – the result came from Redis.

4. Django

from picocache import DjangoCache

django_cache = DjangoCache()          # uses settings.CACHES["default"]

@django_cache(maxsize=None)           # unlimited size, rely on Django’s TTL
def expensive_fn(x):
    ...

API

Each decorator is constructed with connection details (if any) and called with the same signature as functools.lru_cache:

SQLAlchemyCache(url_or_engine, *, key_serializer=None, value_serializer=None, ...)
RedisCache(url_or_params, *, key_serializer=None, value_serializer=None, ...)
DjangoCache(*, key_serializer=None, value_serializer=None, ...)

__call__(maxsize=None, typed=False)

Returns a decorator that memoises the target function.

Param Type Default Meaning
maxsize int/None None Per‑function entry limit (None → no limit).
typed bool False Treat arguments with different types as distinct (same as stdlib).

The wrapped function gains:

  • .cache_info()namedtuple(hits, misses, currsize, maxsize)
  • .cache_clear() or .clear() → empties the persistent store for that function.

Running the tests

uv sync --all-extras
just test
  • SQL tests run against an in‑memory SQLite DB (no external services).
  • Redis tests are skipped automatically unless a Redis server is available on localhost:6379.

License

MIT – see LICENSE for details.

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

picocache-1.2.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

picocache-1.2.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file picocache-1.2.0.tar.gz.

File metadata

  • Download URL: picocache-1.2.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.14

File hashes

Hashes for picocache-1.2.0.tar.gz
Algorithm Hash digest
SHA256 47e2b8a3176ca98734b85aef95c33d5db84e84f1361fab7088c0575254694228
MD5 8d9d0479be15a88e4d60daad9704808e
BLAKE2b-256 b499e3fb48a94c9897ce45c7670507b698c1719eacfe9ef91e5e381c88975e11

See more details on using hashes here.

File details

Details for the file picocache-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: picocache-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.14

File hashes

Hashes for picocache-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad8964c6702f6f0afcda228414ecb6e0a2a732e753c61e6143e3b8f98c3c1677
MD5 1813b9992d783c37ee110ef01962e6f2
BLAKE2b-256 40d1253bc4805f9bbe8317f6673f7094404629fb7f6e7bde94d6862d04c7a2e2

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