Caching for Speed
The fastest work is the work you don't repeat. Caching stores expensive results so you can reuse them - and larzcache makes it easy, safely, from memory to disk to tiers.
A huge amount of performance work comes down to one idea: don't compute the same expensive thing twice. Caching stores the result of costly work (a slow query, an API call, a heavy computation) so subsequent requests reuse it instead of redoing it. Done well it's transformative; done carelessly it causes stale-data bugs. larzcache makes caching easy and gives you control over freshness.
Cache the expensive stuff
The candidates for caching are things that are costly to produce and don't change every second - and larzcache covers the common shapes.
| Cache option | For |
|---|---|
| In-memory LRU + TTL | Fast reuse of hot data, auto-expiring |
| Durable file cache | Cache that survives restarts |
| Tiered (fast over slow) | Memory in front of disk, with promotion |
| @memoize | Cache a function's results by its arguments |
The @memoize decorator is the easiest win: wrap an expensive, pure-ish function and repeated calls with the same arguments return instantly from cache.
Freshness is the hard part
Caching's famous difficulty is invalidation - knowing when a cached value is stale. TTLs (time-to-live) and explicit invalidation are your tools.
from larzcache import LRUCache, memoize
cache = LRUCache(maxsize=1000, ttl=60) # auto-expire after 60s
@memoize(ttl=300) # cache results 5 min, by args
def expensive_report(user_id):
return crunch(user_id)Speed up safely
- Cache expensive, slow-changing work - queries, API calls, heavy computation.
- @memoize is the easy win for pure-ish functions.
- Use TTLs so stale entries auto-expire; invalidate on change when needed.
- Tiers + durability - memory over disk, surviving restarts.
๐ฏ Key takeaways
- Caching reuses expensive results instead of recomputing them - a huge, common performance win.
- larzcache offers in-memory LRU/TTL, a durable file cache, tiers, and a @memoize decorator.
- The hard part is freshness - use TTLs to auto-expire and invalidate on change when data must be current.
- Cache read-heavy, slow-changing work aggressively; be careful with data that must always be current.
Frequently asked
What's the risk of caching - why not cache everything?
The classic risk is stale data: a cache serves a saved copy, so if the underlying data changes and the cache doesn't update, users see outdated (sometimes wrong or confusing) information. Caching everything indiscriminately - especially data that must be current, like a user's balance or a live status - causes exactly these bugs. The discipline is to cache things that are expensive to produce and change slowly (reports, config, API results), use TTLs so entries can't be stale forever, and explicitly invalidate cache entries when the data they represent changes. Caching is a trade-off between speed and freshness; you manage it deliberately rather than blanketing everything.
What's the easiest way to start caching?
The @memoize decorator, for expensive functions whose output depends only on their arguments. You wrap the function, optionally with a TTL, and repeated calls with the same arguments return the stored result instantly instead of recomputing - a big speedup for one line of code. Start there for pure-ish, costly functions (calculations, lookups, formatting of stable data). From there you can add an LRUCache with a TTL for hot data, or tiered/durable caching as needs grow. Beginning with @memoize on your slowest repeated functions usually gives the largest, safest win for the least effort.
You've finished the Data & Storage with the Larz Stack sessions
Now lock it in with the coursework.
Do the assignments โRelated lessons
Evolving Your Schema with Migrations
Data outlives code, and its shape changes over time. Migrations let you evolve your databaโฆ
Data & Storage with the Larz StackStorage Without a Database Server
Most projects reach for Postgres on day one - and take on a server to run, secure and backโฆ
Building with the Larz StackWeb, AI & Dev Tooling โ the Everyday Kit
The libraries you reach for constantly โ talking to the web, building AI agents, making CLโฆ