Class CacheBuilderSpec

java.lang.Object
com.google.common.cache.CacheBuilderSpec

public final class CacheBuilderSpec extends Object
A specification of a CacheBuilder configuration.

CacheBuilderSpec supports parsing configuration off of a string, which makes it especially useful for command-line configuration of a CacheBuilder.

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

The set of supported keys will grow as CacheBuilder evolves, but existing keys will never be removed.

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
  • softValues and weakValues

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

A new CacheBuilder can be instantiated from a CacheBuilderSpec using CacheBuilder.from(CacheBuilderSpec) or CacheBuilder.from(String).

Since:
12.0
  • Field Details

    • KEYS_SPLITTER

      private static final Splitter KEYS_SPLITTER
      Splits each key-value pair.
    • KEY_VALUE_SPLITTER

      private static final Splitter KEY_VALUE_SPLITTER
      Splits the key from the value.
    • VALUE_PARSERS

      private static final ImmutableMap<String,CacheBuilderSpec.ValueParser> VALUE_PARSERS
      Map of names to ValueParser.
    • initialCapacity

      @CheckForNull Integer initialCapacity
    • maximumSize

      @CheckForNull Long maximumSize
    • maximumWeight

      @CheckForNull Long maximumWeight
    • concurrencyLevel

      @CheckForNull Integer concurrencyLevel
    • keyStrength

      @CheckForNull LocalCache.Strength keyStrength
    • valueStrength

      @CheckForNull LocalCache.Strength valueStrength
    • recordStats

      @CheckForNull Boolean recordStats
    • writeExpirationDuration

      long writeExpirationDuration
    • writeExpirationTimeUnit

      @CheckForNull TimeUnit writeExpirationTimeUnit
    • accessExpirationDuration

      long accessExpirationDuration
    • accessExpirationTimeUnit

      @CheckForNull TimeUnit accessExpirationTimeUnit
    • refreshDuration

      long refreshDuration
    • refreshTimeUnit

      @CheckForNull TimeUnit refreshTimeUnit
    • specification

      private final String specification
      Specification; used for toParseableString().
  • Constructor Details

    • CacheBuilderSpec

      private CacheBuilderSpec(String specification)
  • Method Details

    • parse

      public static CacheBuilderSpec parse(String cacheBuilderSpecification)
      Creates a CacheBuilderSpec from a string.
      Parameters:
      cacheBuilderSpecification - the string form
    • disableCaching

      public static CacheBuilderSpec disableCaching()
      Returns a CacheBuilderSpec that will prevent caching.
    • toCacheBuilder

      CacheBuilder<Object,Object> toCacheBuilder()
      Returns a CacheBuilder configured according to this instance's specification.
    • toParsableString

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

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

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

      public boolean equals(@CheckForNull Object obj)
      Overrides:
      equals in class Object
    • durationInNanos

      @CheckForNull private static Long durationInNanos(long duration, @CheckForNull TimeUnit unit)
      Converts an expiration duration/unit pair into a single Long for hashing and equality. Uses nanos to match CacheBuilder implementation.
    • format

      private static String format(String format, Object... args)