org.feijoas.mango.common

cache

package cache

This package contains caching utilities.

The core interface used to represent caches is Cache. In-memory caches can be configured and created using CacheBuilder, with cache entries being loaded by CacheLoader. Statistics about cache performance are exposed using CacheStats.

See the Guava User Guide article on caches.

Since

0.7 (copied from Guava-libraries)

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. cache
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. trait Cache[K, V] extends AnyRef

    A semi-persistent mapping from keys to values.

    A semi-persistent mapping from keys to values. Cache entries are manually added using getOrElseUpdate(key, loader) or put(key, value), and are stored in the cache until either evicted or manually invalidated.

    Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.

    Annotations
    @Beta()
    Since

    0.7 (copied from Guava-libraries)

  2. final case class CacheBuilder[-K, -V] extends Product with Serializable

    A builder of LoadingCache and Cache instances having any combination of the following features:

    A builder of LoadingCache and Cache instances having any combination of the following features:

    • automatic loading of entries into the cache
    • least-recently-used eviction when a maximum size is exceeded
    • time-based expiration of entries, measured since last access or last write
    • keys automatically wrapped in weak references
    • values automatically wrapped in weak or soft references
    • notification of evicted (or otherwise removed) entries
    • accumulation of cache access statistics

    These features are all optional; caches can be created using all or none of them. By default cache instances created by CacheBuilder will not perform any type of eviction.

    Usage example:

    val createExpensiveGraph: Key => Graph = ...
    
    val graphs: LoadingCache[Key, Graph] = CacheBuilder.newBuilder()
        .maximumSize(10000)
        .expireAfterWrite(10, TimeUnit.MINUTES)
        .removalListener(MY_LISTENER)
        .build(createExpensiveGraph)

    This class just delegates to the CacheBuilder implementation in Guava

    Note: by default, the returned cache uses equality comparisons to determine equality for keys or values. However, if weakKeys was specified, the cache uses identity (eq) comparisons instead for keys. Likewise, if weakValues or softValues was specified, the cache uses identity comparisons for values.

    Entries are automatically evicted from the cache when any of

    • maximumSize(Long)
    • maximumWeight(Long)
    • expireAfterWrite
    • expireAfterAccess
    • weakKeys
    • weakValues
    • softValues

    are requested.

    If maximumSize or maximumWeight is requested entries may be evicted on each cache modification.

    If expireAfterWrite or #expireAfterAccess is requested entries may be evicted on each cache modification, on occasional cache accesses, or on calls to Cache.cleanUp(). Expired entries may be counted in Cache.size()}, but will never be visible to read or write operations.

    If weakKeys, weakValues, or softValues are requested, it is possible for a key or value present in the cache to be reclaimed by the garbage collector. Entries with reclaimed keys or values may be removed from the cache on each cache modification, on occasional cache accesses, or on calls to Cache.cleanUp(); such entries may be counted in Cache.size(), but will never be visible to read or write operations.

    Certain cache configurations will result in the accrual of periodic maintenance tasks which will be performed during write operations, or during occasional read operations in the absense of writes. The Cache.cleanUp() method of the returned cache will also perform maintenance, but calling it should not be necessary with a high throughput cache. Only caches built with removalListener, expireAfterWrite, expireAfterAccess, weakKeys, weakValues, or softValues perform periodic maintenance.

    The caches produced by CacheBuilder are serializable, and the deserialized caches retain all the configuration properties of the original cache. Note that the serialized form does not include cache contents, but only configuration.

    Warning: This implementation differs from Guava. It is truly immutable and returns a new instance on each method call. Use the standard method-chaining idiom, as illustrated in the documentation above. This has the advantage that it is not possible to create a cache whose key or value type is incompatible with e.g. the weigher. This CacheBuilder is also contravariant in both type K and V.

    See the Guava User Guide for more information.

    K

    the key of the cache

    V

    the value of the cache

    Since

    0.7 (copied from Guava-libraries)

  3. trait CacheLoader[K, V] extends (K) ⇒ V

    Computes or retrieves values, based on a key, for use in populating a LoadingCache.

    Computes or retrieves values, based on a key, for use in populating a LoadingCache.

    This class should be when bulk retrieval is significantly more efficient than many individual lookups. Otherwise the CacheLoader companion object can be used to create a CacheLoader[K,V] from a function K => V. For example

    val createExpensiveGraph: Key => Graph = ...
    val loader: CacheLoader[Key,Graph] = CacheLoader.from(createExpensiveGraph)

    Alternatively the function K => V can be passed to CacheBuilder directly.

    Since

    0.7 (copied from Guava-libraries)

  4. case class CacheStats(hitCount: Long, missCount: Long, loadSuccessCount: Long, loadExceptionCount: Long, totalLoadTime: Long, evictionCount: Long) extends Product with Serializable

    Statistics about the performance of a Cache.

    Statistics about the performance of a Cache. Instances of this class are immutable.

    Cache statistics are incremented according to the following rules:

    • When a cache lookup encounters an existing cache entry hitCount is incremented.
    • When a cache lookup first encounters a missing cache entry, a new entry is loaded.
    • After successfully loading an entry missCount and loadSuccessCount are incremented, and the total loading time, in nanoseconds, is added to totalLoadTime.
    • When an exception is thrown while loading an entry, missCount and loadExceptionCount are incremented, and the total loading time, in nanoseconds, is added to totalLoadTime.
    • Cache lookups that encounter a missing cache entry that is still loading will wait for loading to complete (whether successful or not) and then increment missCount.
    • When an entry is evicted from the cache, evictionCount is incremented.
    • No stats are modified when a cache entry is invalidated or manually removed.
    • No stats are modified on a query to Cache#getIfPresent.

    A lookup is specifically defined as an invocation of one of the methods LoadingCache#get(key), Cache#get(key, loader), or LoadingCache#getAll(keys).

    Since

    0.7 (copied from Guava-libraries)

  5. trait CacheWrapper[K, V] extends Cache[K, V]

    An adapter that wraps a Guava-Cache in a Mango-Cache and forwards all method calls to the underlying Guava-Cache.

    An adapter that wraps a Guava-Cache in a Mango-Cache and forwards all method calls to the underlying Guava-Cache.

    Attributes
    protected[org.feijoas.mango]
    Since

    0.7

  6. trait LoadingCache[K, V] extends Cache[K, V] with (K) ⇒ V

    A semi-persistent mapping from keys to values.

    A semi-persistent mapping from keys to values. Values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.

    Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.

    When evaluated as a scala.Function, a cache yields the same result as invoking #getUnchecked.

    Note that while this class is still annotated as Beta, the API is frozen from a consumer's standpoint. In other words existing methods are all considered non-Beta and won't be changed without going through an 18 month deprecation cycle; however new methods may be added at any time.

    Annotations
    @Beta()
    Since

    0.7 (copied from Guava-libraries)

  7. trait LoadingCacheWrapper[K, V] extends CacheWrapper[K, V] with LoadingCache[K, V]

    An adapter that wraps a Guava-LoadingCache in a LoadingCache and forwards all method calls to the underlying Guava-LoadingCache.

    An adapter that wraps a Guava-LoadingCache in a LoadingCache and forwards all method calls to the underlying Guava-LoadingCache.

    Attributes
    protected[org.feijoas.mango]
    Since

    0.7

  8. sealed trait RemovalCause extends Serializable

    The reason why a cached entry was removed.

    The reason why a cached entry was removed. See the companion object for the individual RemovalCause cases.

    Annotations
    @Beta()
    Since

    0.7 (copied from guava-libraries)

  9. case class RemovalNotification[+K, +V](key: Option[K], value: Option[V], cause: RemovalCause) extends Product with Serializable

    A notification of the removal of a single entry.

    A notification of the removal of a single entry. The key and/or value may be None if the corresponding key/value was already garbage collected.

    A key/value pair associated with CacheBuilder, this class holds strong references to the key and value, regardless of the type of references the cache may be using.

    key

    the key of the cache entry

    value

    the value of the cache entry

    cause

    the cause for which the entry was removed

    Annotations
    @Beta()
    Since

    0.7 (copied from Guava-libraries)

Value Members

  1. object Cache

    Utility functions to convert between Guava Cache[K, V] and Cache[K, V] and vice versa.

  2. object CacheBuilder extends Serializable

    Factory for CacheBuilder instances.

  3. object CacheLoader

    Factory for CacheLoader instances.

  4. object CacheStats extends Serializable

    CacheStats helper functions

  5. object LoadingCache

    Utility functions to convert between Guava LoadingCache[K, V] and LoadingCache[K, V] and vice versa.

  6. object RemovalCause extends Serializable

    Available RemovalCauses

  7. object RemovalListener

    A function RemovalNotification[K, V] => Unit that can receive a notification when an entry is removed from a cache.

    A function RemovalNotification[K, V] => Unit that can receive a notification when an entry is removed from a cache. The removal resulting in notification could have occured to an entry being manually removed or replaced, or due to eviction resulting from timed expiration, exceeding a maximum size, or garbage collection.

    An instance may be called concurrently by multiple threads to process different entries. Implementations of RemovalListener should avoid performing blocking calls or synchronizing on shared resources.

    Since

    0.7

  8. object RemovalNotification extends Serializable

  9. object Weigher

    Calculates the weights of cache entries.

    Calculates the weights of cache entries.

    Since

    0.7

Inherited from AnyRef

Inherited from Any

Ungrouped