Cache.getOrCompute

T getOrCompute(string key, T delegate() @safe compute, Duration ttl = Duration.zero) @safe

Gets a value from the cache, or computes and stores it if not present.

This is the most common pattern for using the cache. If the value is present and not expired, it is returned immediately. Otherwise, the provided delegate is called to compute the value, which is then cached.

Parameters

keyCache key
computeDelegate to compute the value if not cached
ttlOptional custom TTL (uses default if not specified)

Returns

The cached or computed value

Example:

auto stats = cache.getOrCompute("cpu", &getCpuStats);