Class DistributedCaffeine<K,​V>

  • Type Parameters:
    K - the key type of the cache
    V - the value type of the cache

    public final class DistributedCaffeine<K,​V>
    extends Object
    Distributed Caffeine is a Caffeine-based distributed cache using MongoDB change streams for near real-time synchronization between multiple cache instances, especially across different machines.

    Cache instances can be configured and constructed using a builder returned by newBuilder(MongoCollection). A cache instance can be of type DistributedCache (extends Cache) or of type DistributedLoadingCache (extends LoadingCache).

    Attention: To ensure the integrity of distributed synchronization between cache instances, the following minor restrictions apply:

    • Reference-based eviction using Caffeine's weak or soft references for keys or values is not supported. Even for the use of Caffeine (stand-alone), it is advised to use the more predictable size- or time-based eviction instead.
    Author:
    Andreas Oberhoff
    • Method Detail

      • newBuilder

        public static <K,​V> DistributedCaffeine.Builder<K,​V> newBuilder​(com.mongodb.client.MongoCollection<org.bson.Document> mongoCollection)
        Returns a new builder for configuring and constructing cache instances. For example, the builder can be finalized with DistributedCaffeine.Builder.build() to construct a cache instance of type DistributedCache (extends Cache) or with DistributedCaffeine.Builder.build(CacheLoader) to construct a loading cache instance of type DistributedLoadingCache (extends LoadingCache).

        Note: A call to this method must use additional generic type parameters for key and value directly before the method name like in the following example:

         DistributedCache<Key, Value> distributedCache = DistributedCaffeine.<Key, Value>newBuilder(mongoCollection)
             ...
             .build();
         
        Type Parameters:
        K - the key type of the cache
        V - the value type of the cache
        Parameters:
        mongoCollection - the MongoDB collection used for distributed synchronization between cache instances
        Returns:
        builder for configuring and constructing cache instances