Class CacheSpec

java.lang.Object
dev.relism.flash.ext.cache.CacheSpec

public final class CacheSpec extends Object
How one cache should behave. Every field is optional — a spec that sets nothing gives an unbounded cache that never expires, which is a memory leak wearing a hat, so set at least one of maxSize or ttl.

 CacheSpec.of().maxSize(10_000).ttl(Duration.ofMinutes(10))
 

Mutable builder rather than a record with withX copies: it is constructed once at boot inside a lambda and never shared.

  • Method Details

    • of

      public static CacheSpec of()
    • maxSize

      public CacheSpec maxSize(long maxSize)
      Maximum entries before the backend starts evicting. Negative means unbounded.
    • ttl

      public CacheSpec ttl(Duration ttl)
      Entries expire this long after they were written.
    • ttlAfterAccess

      public CacheSpec ttlAfterAccess(Duration ttlAfterAccess)
      Entries expire this long after they were last read or written.
    • recordStats

      public CacheSpec recordStats()
      Records hit/miss counters for Cache.stats().

      Off by default: counting costs a pair of atomic increments on every lookup, and a cache nobody is measuring should not pay for numbers nobody reads.

    • maxSize

      public long maxSize()
    • ttl

      public Duration ttl()
    • ttlAfterAccess

      public Duration ttlAfterAccess()
    • statsRecorded

      public boolean statsRecorded()
    • bounded

      public boolean bounded()