Skip to main content

persistent decorators mirroring functools.lru_cache

Project description

PicoCache

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.
Two back‑ends are provided out of the box:

  • SQLAlchemyCache – persists to any RDBMS supported by SQLAlchemy (SQLite, Postgres, MySQL, …).
  • RedisCache – stores values in Redis, perfect for distributed deployments.

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

pip install picocache

Quick‑start

1. SQL (SQLite example)

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)

2. 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.


API

Each decorator object is initialised with connection details 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, ...)

__call__(maxsize=128, typed=False)

Returns a decorator that memoises the target function.

Param Type Default Meaning
maxsize int/None 128 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() → empties the persistent store for that function.

Running the tests

uv sync
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-0.2.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

picocache-0.2.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for picocache-0.2.0.tar.gz
Algorithm Hash digest
SHA256 46987997ba66fa1e7d64ab74e9be905d07fe7c2374774afd1d60c27aaec4ed80
MD5 fefc96794779dc7b8af232da16a00d78
BLAKE2b-256 03404dc3c429e3ab2cf2941e596243f3387dcb40afc63b9082a04108f4ce54e6

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for picocache-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41de32cb905e7b0d8eec711369a11c883c5ed01e81a04b90543cfafb7280d45f
MD5 61e8c5ab36105aeda2feaa0ef8cb9a62
BLAKE2b-256 9fd8d8843373632ee240b703a2f148aba0ac1b47b863b7de9a7a0891ebbc7d92

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