Skip to main content

larzcache

Caching done right, in pure Python. Zero dependencies.

An LRU + TTL in-memory cache, a durable file cache, a tiered cache that stacks fast over slow, and a @memoize decorator with TTL and stats — all thread-safe, all with hit/miss accounting, none needing Redis or any install.

from larzcache import LRUCache, memoize

cache = LRUCache(maxsize=1000, ttl=300)      # 1000 entries, 5-min default TTL
cache.set("user:42", {...})
cache.get("user:42")
cache.get_or_set("report", build_report, ttl=60)   # compute only on a miss

@memoize(ttl=30)
def price(symbol):
    ...                                       # cached 30s per symbol

Why

  • Zero dependencies, thread-safe. Pure standard library, safe to share across threads, nothing to run or install.
  • LRU and TTL. Bound memory by entry count (least-recently-used eviction) and/or expire entries by age — per entry or per cache.
  • Durable when you want it. FileCache persists entries as JSON so they survive restarts; Tiered puts a fast memory cache in front of it and promotes hits upward.
  • A better lru_cache. @memoize adds TTL, live stats, and selective invalidation (fn.invalidate(args)) on top of the familiar decorator.
  • Observable. Every cache reports hits, misses, evictions, and hit rate.

Install

pip install larzcache

The caches

from larzcache import LRUCache, FileCache, Tiered

mem = LRUCache(maxsize=500, ttl=60)          # in-memory
disk = FileCache("cache/", ttl=3600)         # durable JSON files
both = Tiered(mem, disk)                      # memory in front of disk

both.set("k", {"any": "json-able value"})
both.get("k")            # checks memory, then disk (promoting on hit)
both.get_or_set("k", expensive)
"k" in both
both.delete("k")
both.clear()
mem.stats()              # {'hits':.., 'misses':.., 'evictions':.., 'hit_rate':..}

The decorator

from larzcache import memoize

@memoize(maxsize=256, ttl=60)
def fetch(user_id):
    ...

fetch.cache_info()      # hit/miss stats
fetch.invalidate(42)    # drop just fetch(42)
fetch.cache_clear()     # drop everything

Arguments must be hashable (same rule as functools.lru_cache).

Tests

python -m unittest discover -s tests -v      # 19 tests, zero deps

The Larz stack

Pure-Python, zero-dependency building blocks:

  • larz — money-native web framework
  • larzchain — from-scratch PoW blockchain
  • larzmoney — exact, penny-perfect money
  • larzcrypt — pure-Python cryptography toolkit
  • larzdb — crash-safe embedded database
  • larzagent — zero-dep AI agent framework
  • larzchart — data to inline SVG charts
  • larzmark — Markdown + SEO static sites
  • larztask — durable background job queue
  • larzvault — encrypted secrets manager
  • larzvm — deterministic gas-metered VM
  • larzcache — this library

License

MIT © larz-scripter

Metadata

Release files for larzcache 0.1.0

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

Source distribution (sdist)

Source distribution for larzcache 0.1.0
File Size Uploaded Provenance
larzcache-0.1.0.tar.gz 7.5 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 14.8 kB

Release files / larzcache-0.1.0.tar.gz

Download URL larzcache-0.1.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
bd457be72354aaa4628d6fe36bd0affda4bc38fd059c8d6316f1bffc10d18110
BLAKE2b-256 checksum
How to use checksums
a68c02ee00ece20c3cae2535dc237dd2885f924f89627ffa33f53233ea118888
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / larzcache-0.1.0-py3-none-any.whl

Download URL larzcache-0.1.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c225f1a11bb1fe4112a8bf6632daad76b9db2724e7dbc7cae25bb7614974f79c
BLAKE2b-256 checksum
How to use checksums
c1d69ae265728c574db95e7f56532827d5e0399320184c5e5a607742fafa54f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for larzcache 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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