Class DistributedCaffeine.Builder<K,V>
- java.lang.Object
-
- io.github.oberhoff.distributedcaffeine.DistributedCaffeine.Builder<K,V>
-
- Type Parameters:
K
- the key type of the cacheV
- the value type of the cache
- Enclosing class:
- DistributedCaffeine<K,V>
public static final class DistributedCaffeine.Builder<K,V> extends Object
Builder for configuring and constructing cache instances of typeDistributedCache
(extendsCache
) or of typeDistributedLoadingCache
(extendsLoadingCache
). To construct a builder,DistributedCaffeine.newBuilder(MongoCollection)
must be used.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DistributedCache<K,V>
build()
Same asCaffeine.build()
.DistributedLoadingCache<K,V>
build(com.github.benmanes.caffeine.cache.CacheLoader<K,V> cacheLoader)
Same asCaffeine.build(CacheLoader)
.DistributedCaffeine.Builder<K,V>
withCaffeineBuilder(com.github.benmanes.caffeine.cache.Caffeine<?,?> caffeineBuilder)
Specifies the Caffeine builder to be used for internal caching.DistributedCaffeine.Builder<K,V>
withCustomKeySerializer(Serializer<K,?> keySerializer)
Specifies a custom serializer to be used for serializing key objects.DistributedCaffeine.Builder<K,V>
withCustomValueSerializer(Serializer<V,?> valueSerializer)
Specifies a custom serializer to be used for serializing value objects.DistributedCaffeine.Builder<K,V>
withDistributionMode(DistributionMode distributionMode)
Specifies the mode used for distributed synchronization between cache instances.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.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.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.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.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.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.
-
-
-
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 ofCaffeine.build()
orCaffeine.build(CacheLoader)
, this builder'sbuild()
orbuild(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
-
withDistributionMode
public DistributedCaffeine.Builder<K,V> withDistributionMode(DistributionMode distributionMode)
Specifies the mode used for distributed synchronization between cache instances.Note:
DistributionMode.POPULATION_AND_INVALIDATION_AND_EVICTION
is the default mode if this method is skipped.- Parameters:
distributionMode
-DistributionMode
used for distributed synchronization- 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
- optionalClass
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 theSerializable
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.
-
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 customizedObjectMapper
keyTypeReference
- theTypeReference
of the key objectvalueTypeReference
- theTypeReference
of the value objectstoreAsBson
-true
for BSON encoding orfalse
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.
-
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
- theTypeReference
of the key objectvalueTypeReference
- theTypeReference
of the value objectstoreAsBson
-true
for BSON encoding orfalse
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:ByteArraySerializer
for serializing an object to a byte array representationStringSerializer
for serializing an object to a string representationJsonSerializer
for serializing an object to a JSON representation (encoded as String or BSON)
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:ByteArraySerializer
for serializing an object to a byte array representationStringSerializer
for serializing an object to a string representationJsonSerializer
for serializing an object to a JSON representation (encoded as String or BSON)
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 DistributedCache<K,V> build()
Same asCaffeine.build()
. Constructs aDistributedCache
instance (extendsCache
) with additional distributed synchronization functionality.- Returns:
- the new
DistributedCache
instance
-
build
public DistributedLoadingCache<K,V> build(com.github.benmanes.caffeine.cache.CacheLoader<K,V> cacheLoader)
Same asCaffeine.build(CacheLoader)
. Constructs aDistributedLoadingCache
instance (extendsLoadingCache
) with additional distributed synchronization functionality.- Parameters:
cacheLoader
- theCacheLoader
used to obtain new values- Returns:
- the new
DistributedLoadingCache
instance
-
-