namespaces

cache

Store short-lived values through the cache provider, with TTLs, counters, and bulk reads and writes.

The cache namespace stores short-lived key-value data in a fast store, through the configured cache provider. It is for data you can afford to lose and rebuild, not for your source of truth.

When to use

Reach for the cache to avoid repeating expensive work: a computed result, a slow upstream response, or a counter. Set a TTL so entries clean themselves up. Because a value can expire or be evicted at any time, never keep anything here that you cannot recompute. For durable data, use db or doc.

The Cache expensive work guide walks through the cache-aside pattern from end to end.

Operations

cache.set stores a value and cache.get reads it back, returning null on a miss:

marreta
cache.set("greeting", "hello", ttl: 60)
value = cache.get("greeting")
NameSignatureSummary
cache.getcache.get(key)Returns the cached value, or null when missing.
cache.setcache.set(key, value, ttl: N, only_if_absent: true)Stores a value, optionally with a TTL or only when absent.
cache.deletecache.delete(key)Deletes a key and returns whether it existed.
cache.existscache.exists(key)Returns whether a key exists.
cache.ttlcache.ttl(key)Returns the remaining TTL in seconds, or null.
cache.expirecache.expire(key, ttl: N)Updates a key’s TTL.
cache.incrcache.incr(key, by: N)Increments an integer counter atomically.
cache.decrcache.decr(key, by: N)Decrements an integer counter atomically.
cache.get_manycache.get_many(keys)Reads multiple keys at once.
cache.set_manycache.set_many(values)Writes multiple entries at once.

Notes

  • The cache provider must be configured and reachable before marreta serve. Run marreta doctor to check the connection.
  • A value set without ttl: does not expire on its own. Give it a TTL, or cache.delete it when the underlying data changes.
  • incr and decr are atomic, so they are safe for counters under concurrent requests.
  • only_if_absent: true stores only when the key is unset, and returns whether it stored. It is the building block for a simple lock or a write-once flag.