Class CaffeineSpec

java.lang.Object
com.github.benmanes.caffeine.cache.CaffeineSpec

public final class CaffeineSpec extends Object
A specification of a Caffeine builder configuration.

CaffeineSpec supports parsing configuration off of a string, which makes it especially useful for command-line configuration of a Caffeine builder.

The string syntax is a series of comma-separated keys or key-value pairs, each corresponding to a Caffeine builder method.

Durations are represented by an integer, followed by one of "d", "h", "m", or "s", representing days, hours, minutes, or seconds respectively. There is currently no syntax to request expiration in milliseconds, microseconds, or nanoseconds.

Whitespace before and after commas and equal signs is ignored. Keys may not be repeated; it is also illegal to use the following pairs of keys in a single value:

  • maximumSize and maximumWeight
  • weakValues and softValues

CaffeineSpec does not support configuring Caffeine methods with non-value parameters. These must be configured in code.

A new Caffeine builder can be instantiated from a CaffeineSpec using Caffeine.from(CaffeineSpec) or Caffeine.from(String).

  • Field Details

    • SPLIT_OPTIONS

      static final String SPLIT_OPTIONS
      See Also:
    • SPLIT_KEY_VALUE

      static final String SPLIT_KEY_VALUE
      See Also:
    • specification

      final String specification
    • initialCapacity

      int initialCapacity
    • maximumWeight

      long maximumWeight
    • maximumSize

      long maximumSize
    • recordStats

      boolean recordStats
    • keyStrength

      Caffeine.Strength keyStrength
    • valueStrength

      Caffeine.Strength valueStrength
    • expireAfterAccessDuration

      long expireAfterAccessDuration
    • expireAfterAccessTimeUnit

      TimeUnit expireAfterAccessTimeUnit
    • expireAfterWriteDuration

      long expireAfterWriteDuration
    • expireAfterWriteTimeUnit

      TimeUnit expireAfterWriteTimeUnit
    • refreshAfterWriteDuration

      long refreshAfterWriteDuration
    • refreshAfterWriteTimeUnit

      TimeUnit refreshAfterWriteTimeUnit
  • Constructor Details

    • CaffeineSpec

      private CaffeineSpec(String specification)
  • Method Details

    • toBuilder

      Caffeine<Object,Object> toBuilder()
      Returns a Caffeine builder configured according to this specification.
      Returns:
      a builder configured to the specification
    • parse

      public static CaffeineSpec parse(String specification)
      Creates a CaffeineSpec from a string.
      Parameters:
      specification - the string form
      Returns:
      the parsed specification
    • parseOption

      void parseOption(String option)
      Parses and applies the configuration option.
    • configure

      void configure(String key, @Nullable String value)
      Configures the setting.
    • initialCapacity

      void initialCapacity(String key, String value)
      Configures the initial capacity.
    • maximumSize

      void maximumSize(String key, String value)
      Configures the maximum size.
    • maximumWeight

      void maximumWeight(String key, String value)
      Configures the maximum size.
    • weakKeys

      void weakKeys(@Nullable String value)
      Configures the keys as weak references.
    • valueStrength

      void valueStrength(String key, @Nullable String value, Caffeine.Strength strength)
      Configures the value as weak or soft references.
    • expireAfterAccess

      void expireAfterAccess(String key, String value)
      Configures expire after access.
    • expireAfterWrite

      void expireAfterWrite(String key, String value)
      Configures expire after write.
    • refreshAfterWrite

      void refreshAfterWrite(String key, String value)
      Configures refresh after write.
    • recordStats

      void recordStats(@Nullable String value)
      Configures the value as weak or soft references.
    • parseInt

      static int parseInt(String key, String value)
      Returns a parsed int value.
    • parseLong

      static long parseLong(String key, String value)
      Returns a parsed long value.
    • parseDuration

      static long parseDuration(String key, String value)
      Returns a parsed duration value.
    • parseTimeUnit

      static TimeUnit parseTimeUnit(String key, String value)
      Returns a parsed TimeUnit value.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • durationInNanos

      static long durationInNanos(long duration, @Nullable TimeUnit unit)
      Converts an expiration duration/unit pair into a single long for hashing and equality.
    • toParsableString

      public String toParsableString()
      Returns a string that can be used to parse an equivalent CaffeineSpec. The order and form of this representation is not guaranteed, except that parsing its output will produce a CaffeineSpec equal to this instance.
      Returns:
      a string representation of this specification
    • toString

      public String toString()
      Returns a string representation for this CaffeineSpec instance. The form of this representation is not guaranteed.
      Overrides:
      toString in class Object