Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1ay1 Agentty Generation Guarded Cache

From Leeroopedia
Knowledge Sources
Domains Caching, Concurrency
Last Updated 2026-10-28 12:00 GMT

Overview

Cached value where a slow refill can't overwrite after it was cleared, using a generation counter to invalidate in-flight fetches.

Description

The agentty::util::GenCache<T> template is a cached value that a slow refill can't overwrite after it was cleared. A fetch reads `gen()` before going to the network and stores with `store_if(value, gen)`. If `clear()` ran in between, the store is dropped, so a list fetched before an invalidation never lands after it.

The cell holds a value, a generation counter, and a "landed" flag. `clear()` increments the generation, invalidating every fetch already in flight. `store_if()` checks the generation matches before writing — if it changed, the stale fetch is silently dropped.

Usage

Import this when you need to: cache a value that may be invalidated while a slow refill is in flight, preventing stale data from overwriting a cleared cache.

Code Reference

Source Location

Signature

namespace agentty::util {

template <class T>
class GenCache {
public:
    [[nodiscard]] T get() const;
    [[nodiscard]] std::uint64_t gen() const;

    // Store `v` only if nothing cleared the cache since `seen` was read.
    // Returns whether it landed.
    bool store_if(T v, std::uint64_t seen);

    // Empty the cache and invalidate every fetch already in flight.
    void clear();
};
}

Import

#include "agentty/util/gen_cache.hpp"

I/O Contract

Inputs

Name Type Required Description
v T Yes (store_if) The value to cache
seen std::uint64_t Yes (store_if) Generation observed before the fetch

Outputs

Name Type Description
get() T The cached value (T{} when cleared)
gen() std::uint64_t Current generation (bumps on clear)
store_if() bool Whether the value landed (false = cleared during fetch)

Usage Examples

Cache with In-Flight Invalidation

#include "agentty/util/gen_cache.hpp"

agentty::util::GenCache<std::vector<ModelInfo>> catalog_cache;

// Fetch path:
auto seen = catalog_cache.gen();
auto models = fetch_from_network();  // slow

if (!catalog_cache.store_if(models, seen)) {
    // Cache was cleared while we were fetching — our data is stale, drop it
    // A concurrent re-fetch will populate it with fresh data
    return;
}
// Our value landed in the cache

// Invalidate:
catalog_cache.clear();  // bumps gen, drops stale in-flight stores

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment