Skip to content

Specification

Ben Manes edited this page Oct 21, 2020 · 11 revisions
CaffeineSpec spec = CaffeineSpec.parse(
    "maximumWeight=1000, expireAfterWrite=10m, recordStats");
LoadingCache<Key, Graph> graphs = Caffeine.from(spec)
    .maximumWeight(10_000)
    .weigher((Key key, Graph graph) -> graph.vertices().size())
    .build(key -> createExpensiveGraph(key));

CaffeineSpec supports parsing a simple configuration format into a Caffeine builder. The string syntax is a series of comma-separated keys or key-value pairs, each corresponding to a builder method. This format does not support configuring builder methods with non-value parameters, such as removalListener, which must be configured in code.

The format supports the following builder methods. It is illegal to combine maximumSize with maximumWeight or weakValues with softValues.

  • initialCapacity=[integer]: sets Caffeine.initialCapacity
  • maximumSize=[long]: sets Caffeine.maximumSize
  • maximumWeight=[long]: sets Caffeine.maximumWeight
  • expireAfterAccess=[duration]: sets Caffeine.expireAfterAccess
  • expireAfterWrite=[duration]: sets Caffeine.expireAfterWrite
  • refreshAfterWrite=[duration]: sets Caffeine.refreshAfterWrite
  • weakKeys: sets Caffeine.weakKeys
  • weakValues: sets Caffeine.weakValues
  • softValues: sets Caffeine.softValues
  • recordStats: sets Caffeine.recordStats

Durations are represented as either ISO-8601 seconds using Duration or by an integer followed by one of "d", "h", "m", or "s", representing days, hours, minutes, or seconds respectively.

Clone this wiki locally