Class DistributedCaffeine.Builder<K,​V>

    • Method Detail

      • withCaffeineBuilder

        public DistributedCaffeine.Builder<K,​V> withCaffeineBuilder​(com.github.benmanes.caffeine.cache.Caffeine<?,​?> caffeineBuilder)
        Specifies the Caffeine builder to be used for internal caching. It must be configured without a final build step. Instead of Caffeine.build() or Caffeine.build(CacheLoader), this builder's build() or build(CacheLoader) can be used later on like in the following example:
         DistributedCache<Key, Value> distributedCache = DistributedCaffeine.<Key, Value>newBuilder(mongoCollection)
             .withCaffeineBuilder(Caffeine.newBuilder()
                 .maximumSize(10_000)
                 .expireAfterWrite(Duration.ofMinutes(5)))
             .build();
         
        Note: A default (empty) Caffeine configuration is used as default if this builder method is skipped.

        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.
        Parameters:
        caffeineBuilder - Caffeine builder instance without final build step
        Returns:
        a builder instance for chaining additional methods
      • withFurySerializer

        public DistributedCaffeine.Builder<K,​V> withFurySerializer​(Class<?>... registerClasses)
        Specifies that cache entries are serialized with byte array representation using Apache Fury when stored in the MongoDB collection.

        Note: This is the default serializer if builder methods for serializers are skipped.

        Parameters:
        registerClasses - optional Class of the object (with additional classes of nested objects) to serialize
        Returns:
        a builder instance for chaining additional methods
      • withJavaObjectSerializer

        public DistributedCaffeine.Builder<K,​V> withJavaObjectSerializer()
        Specifies that cache entries are serialized with byte array representation using Java Object Serialization when stored in the MongoDB collection. Objects to serialize must implement the Serializable interface.

        Note: A serializer with byte array representation using Apache Fury is the default serializer if builder methods for serializers are skipped.

        Returns:
        a builder instance for chaining additional methods
      • withJsonSerializer

        public DistributedCaffeine.Builder<K,​V> withJsonSerializer​(com.fasterxml.jackson.databind.ObjectMapper objectMapper,
                                                                         Class<? super K> keyClass,
                                                                         Class<? super V> valueClass,
                                                                         boolean storeAsBson)
        Specifies that cache entries are serialized with JSON representation (encoded as String or BSON) using Jackson when stored in the MongoDB collection. If a default object mapper is sufficient, withJsonSerializer(Class, Class, boolean) can be used instead.

        Note: A serializer with byte array representation using Apache Fury is the default serializer if builder methods for serializers are skipped.

        Parameters:
        objectMapper - the customized ObjectMapper
        keyClass - the Class of the key object to serialize
        valueClass - the Class of the value object to serialize
        storeAsBson - true for BSON encoding or false for string encoding
        Returns:
        a builder instance for chaining additional methods
      • withJsonSerializer

        public DistributedCaffeine.Builder<K,​V> withJsonSerializer​(com.fasterxml.jackson.databind.ObjectMapper objectMapper,
                                                                         com.fasterxml.jackson.core.type.TypeReference<K> keyTypeReference,
                                                                         com.fasterxml.jackson.core.type.TypeReference<V> valueTypeReference,
                                                                         boolean storeAsBson)
        Specifies that cache entries are serialized with JSON representation (encoded as String or BSON) using Jackson when stored in the MongoDB collection. If a default object mapper is sufficient, withJsonSerializer(TypeReference, TypeReference, boolean) can be used instead.

        Note: A serializer with byte array representation using Apache Fury is the default serializer if builder methods for serializers are skipped.

        Parameters:
        objectMapper - the customized ObjectMapper
        keyTypeReference - the TypeReference of the key object
        valueTypeReference - the TypeReference of the value object
        storeAsBson - true for BSON encoding or false for string encoding
        Returns:
        a builder instance for chaining additional methods
      • withJsonSerializer

        public DistributedCaffeine.Builder<K,​V> withJsonSerializer​(Class<? super K> keyClass,
                                                                         Class<? super V> valueClass,
                                                                         boolean storeAsBson)
        Specifies that cache entries are serialized with JSON representation (encoded as String or BSON) using Jackson when stored in the MongoDB collection. If a customized object mapper is required, withJsonSerializer(ObjectMapper, Class, Class, boolean) can be used instead.

        Note: A serializer with byte array representation using Apache Fury is the default serializer if builder methods for serializers are skipped.

        Parameters:
        keyClass - the Class of the key object to serialize
        valueClass - the Class of the value object to serialize
        storeAsBson - true for BSON encoding or false for string encoding
        Returns:
        a builder instance for chaining additional methods
      • withJsonSerializer

        public DistributedCaffeine.Builder<K,​V> withJsonSerializer​(com.fasterxml.jackson.core.type.TypeReference<K> keyTypeReference,
                                                                         com.fasterxml.jackson.core.type.TypeReference<V> valueTypeReference,
                                                                         boolean storeAsBson)
        Specifies that cache entries are serialized with JSON representation (encoded as String or BSON) using Jackson when stored in the MongoDB collection. If a customized object mapper is required, withJsonSerializer(ObjectMapper, TypeReference, TypeReference, boolean) can be used instead.

        Note: A serializer with byte array representation using Apache Fury is the default serializer if builder methods for serializers are skipped.

        Parameters:
        keyTypeReference - the TypeReference of the key object
        valueTypeReference - the TypeReference of the value object
        storeAsBson - true for BSON encoding or false for string encoding
        Returns:
        a builder instance for chaining additional methods
      • withCustomKeySerializer

        public DistributedCaffeine.Builder<K,​V> withCustomKeySerializer​(Serializer<K,​?> keySerializer)
        Specifies a custom serializer to be used for serializing key objects. Custom serializers must implement one of the following interfaces:

        Note: A serializer with byte array representation using Apache Fury is the default serializer if builder methods for serializers are skipped.

        Parameters:
        keySerializer - the custom serializer for key objects
        Returns:
        a builder instance for chaining additional methods
      • withCustomValueSerializer

        public DistributedCaffeine.Builder<K,​V> withCustomValueSerializer​(Serializer<V,​?> valueSerializer)
        Specifies a custom serializer to be used for serializing value objects. Custom serializers must implement one of the following interfaces:

        Note: A serializer with byte array representation using Apache Fury is the default serializer if builder methods for serializers are skipped.

        Parameters:
        valueSerializer - the custom serializer for value objects
        Returns:
        a builder instance for chaining additional methods
      • build

        public DistributedLoadingCache<K,​V> build​(com.github.benmanes.caffeine.cache.CacheLoader<K,​V> cacheLoader)
        Same as Caffeine.build(CacheLoader). Constructs a DistributedLoadingCache instance (extends LoadingCache) with additional distributed synchronization functionality.
        Parameters:
        cacheLoader - the CacheLoader used to obtain new values
        Returns:
        the new DistributedLoadingCache instance