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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

larzcache-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

larzcache-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file larzcache-0.1.0.tar.gz.

File metadata

  • Download URL: larzcache-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for larzcache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bd457be72354aaa4628d6fe36bd0affda4bc38fd059c8d6316f1bffc10d18110
MD5 3ff784f78c3b44af7518d099e7e6dc01
BLAKE2b-256 a68c02ee00ece20c3cae2535dc237dd2885f924f89627ffa33f53233ea118888

See more details on using hashes here.

File details

Details for the file larzcache-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzcache-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for larzcache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c225f1a11bb1fe4112a8bf6632daad76b9db2724e7dbc7cae25bb7614974f79c
MD5 13d136d6a68332a1d06cd5a5594bb0dd
BLAKE2b-256 c1d69ae265728c574db95e7f56532827d5e0399320184c5e5a607742fafa54f8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

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