Data and Math

vcache

A least recently used cache, and memoizing for any function.

Released v1.0.0 · 2026-09-28
Import
use "stdlib/cache.ven"

A least recently used cache with a fixed size and real eviction, plus a memoize helper that wraps a one argument function and remembers every result it returns. The memoize cache has no limit; use the cache directly when the bound and the eviction matter. Neither structure locks itself across concurrent tasks.

vcache.ven
print LRUCache(2, new_map(), [])
Reference (7 functions)

API reference

lru_new(capacity)

Builds a new, empty LRUCache with room for capacity entries.

lru_new(2).capacity

lru_touch(cache, key)

Internal helper: move key to the most-recently-used end of cache.order (the end of the List), leaving cache.store untouched.

c = lru_new(2)
lru_put(c, "a", 1)
lru_put(c, "b", 2)
lru_touch(c, "a")
lru_put(c, "c", 3)
print lru_has(c, "a")
print lru_has(c, "b")
:: prints: yes
::         no

lru_has(cache, key)

Tests whether key is currently present in cache.

c = lru_new(2)
lru_put(c, "a", 1)
print lru_has(c, "a")
print lru_has(c, "z")
:: prints: yes
::         no

lru_get(cache, key)

Gives back the value stored for key in cache, or nothing when absent.

c = lru_new(2)
lru_put(c, "a", 1)
print lru_get(c, "a")
:: prints: 1

lru_put(cache, key, value)

Stores value under key in cache, evicting the least-recently-used entry if full.

c = lru_new(2)
lru_put(c, "a", 1)
lru_put(c, "b", 2)
lru_put(c, "c", 3)
print lru_has(c, "a")
print lru_has(c, "c")
:: prints: no
::         yes

lru_size(cache)

Gives back how many entries are currently stored in cache.

c = lru_new(2)
lru_put(c, "a", 1)
print lru_size(c)
:: prints: 1

memoized(f)

Wraps a one-argument function f so repeat calls with the same argument are cached.

make square(n)
    give n * n
cached = memoized(square)
print cached(5)
:: prints: 25

Get notified when vcache changes

Get notified about

New versions and security advisories.

New and updated packages, once a day.

News on the math model built in Venutian.