Skip to content

UXCacheEntry

UXCacheEntry is one row of a UXCache.

#use <UXKit> // or #import "UXCache.xc"
class UXCacheEntry : Object {
u8* key;
Object* value;
i32 touched; // the cache's clock when this was last used
}

set creates one, and eviction discards it. You reach one only through find, which exists for inspection.

The cache keeps a monotonic integer that increments on every get and set, and stamps the entry with it. Eviction picks the entry with the smallest stamp.

A counter makes the structure testable and deterministic: the same sequence of calls evicts the same entry on every run and every platform, regardless of timing. “Recent” means an order of use, not a number of seconds.

touched tells you relative age and nothing else. Comparing stamps between two caches is meaningless, because each has its own clock.

u8* key

The entry stores the pointer set was given. It is not copied and not managed.

The string must outlive the entry: use a literal, or copy it first with UXStr.dup. A key from a scratch buffer leaves the cache comparing against bytes that have since changed, and the symptom is a lookup that misses for no visible reason.

Comparison is by content, so a key built at run time matches a literal with the same characters.

Object* value

The cache owns what it holds, so the value survives until evicted. A cache that never fills in practice keeps everything alive, so choose a setCapacity value instead of leaving the default 16.

u8* key // borrowed; compared by content
Object* value // strong
i32 touched // the cache's clock at last use; lowest is evicted first
  • UXCache: the cache these live in
  • UXKVEntry: the persistent equivalent, with no clock because nothing is evicted
  • UXBagEntry: the counted equivalent