Cache Values with an LRU Cache in Larzscript
Cache expensive results with a bounded, least-recently-used eviction policy, plus one-line memoization for a whole function.
cache.set()/get() take an optional per-entry TTL; cache.memoize() wraps a single-argument function so repeat calls skip recomputation automatically, and cache.stats() reports real hit/miss counts.
larzscript pkg install cache
Code
import "cache" as cache
let c = cache.new(100)
cache.set(c, "a", 1, 60)
print(cache.get(c, "a", nil))
print(cache.get(c, "missing", "default"))
let memo = cache.memoize(fn(n) { return n * n })
print(memo["call"](5))
print(memo["call"](5))
print(cache.stats(memo["cache"]))Output
1
default
25
25
{hits: 1, misses: 0, size: 1, capacity: 1000}Related
larzcacheAn LRU cache with optional per-entry TTL and a one-line memoize() wrapper→Retry with Exponential Backoff in LarzscriptRetry a flaky call a few times, waiting longer between each attempt, instead of failing on the first hiccup or hammering a struggling service.→Memoize a Function in LarzscriptCache a function's results so calling it twice with the same input only does the real work once.→Implement a State Machine in LarzscriptDeclare states and transitions, fire events, and let the machine reject anything that isn't a legal move - a document workflow (draft -> review -> published) in a few lines.→