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)
    .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

A duration can be represented by an integer followed by one of "d", "h", "m", or "s", representing days, hours, minutes, or seconds respectively. Alternatively, as of v2.8.7, an ISO-8601 format string may be provided and is parsed by Duration.parse. For the purposes of a cache, all fractional values are rounded and negative durations throw an exception. Some examples of the two formats are below.

Short ISO-8601 Description
50s PT50S 50 seconds
11m PT11M 11 minutes
6h PT6H 6 hours
3d P3D 3 days
P3DT3H4M 3 days, 3 hours and 4 minutes
Clone this wiki locally