Class DefaultDistributedCache<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this cache
    V - the type of mapped values
    All Implemented Interfaces:
    Cache<K,​V>, DistributedCache<K,​V>

    public class DefaultDistributedCache<K,​V>
    extends Object
    implements DistributedCache<K,​V>
    Default implementation of DistributedCache interface.

    This implementation provides a production-ready distributed cache with: - Automatic node discovery and failure detection - Configurable consistency models - Partition tolerance and self-healing - Built-in load balancing and replication

    Since:
    1.0.0
    • Method Detail

      • get

        public V get​(K key)
        Description copied from interface: Cache
        Returns the value associated with the key in this cache, or null if there is no cached value for the key.
        Specified by:
        get in interface Cache<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value to which the specified key is mapped, or null if this cache contains no mapping for the key
      • put

        public void put​(K key,
                        V value)
        Description copied from interface: Cache
        Associates the specified value with the specified key in this cache.
        Specified by:
        put in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
      • remove

        public V remove​(K key)
        Description copied from interface: Cache
        Removes the mapping for a key from this cache if it is present.
        Specified by:
        remove in interface Cache<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the cache
        Returns:
        the previous value associated with key, or null if there was no mapping for key
      • clear

        public void clear()
        Description copied from interface: Cache
        Removes all mappings from this cache.
        Specified by:
        clear in interface Cache<K,​V>
      • size

        public long size()
        Description copied from interface: Cache
        Returns the approximate number of entries in this cache.
        Specified by:
        size in interface Cache<K,​V>
        Returns:
        the number of entries in this cache
      • containsKey

        public boolean containsKey​(K key)
        Description copied from interface: Cache
        Returns true if this cache contains a mapping for the specified key.
        Specified by:
        containsKey in interface Cache<K,​V>
        Parameters:
        key - key whose presence in this cache is to be tested
        Returns:
        true if this cache contains a mapping for the specified key
      • keys

        public Set<K> keys()
        Description copied from interface: Cache
        Returns a view of all the keys in this cache.
        Specified by:
        keys in interface Cache<K,​V>
        Returns:
        a set view of the keys contained in this cache
      • values

        public Collection<V> values()
        Description copied from interface: Cache
        Returns a view of all the values in this cache.
        Specified by:
        values in interface Cache<K,​V>
        Returns:
        a collection view of the values contained in this cache
      • entries

        public Set<Map.Entry<K,​V>> entries()
        Description copied from interface: Cache
        Returns a view of all the key-value pairs in this cache.
        Specified by:
        entries in interface Cache<K,​V>
        Returns:
        a set view of the mappings contained in this cache
      • stats

        public CacheStats stats()
        Description copied from interface: Cache
        Returns the cache statistics.
        Specified by:
        stats in interface Cache<K,​V>
        Returns:
        the cache statistics
      • getAsync

        public CompletableFuture<V> getAsync​(K key)
        Description copied from interface: Cache
        Asynchronously returns the value associated with the key in this cache.
        Specified by:
        getAsync in interface Cache<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        a CompletableFuture that will be completed with the value to which the specified key is mapped, or null if this cache contains no mapping for the key
      • putAsync

        public CompletableFuture<Void> putAsync​(K key,
                                                V value)
        Description copied from interface: Cache
        Asynchronously associates the specified value with the specified key in this cache.
        Specified by:
        putAsync in interface Cache<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        a CompletableFuture that will be completed when the operation is done
      • removeAsync

        public CompletableFuture<V> removeAsync​(K key)
        Description copied from interface: Cache
        Asynchronously removes the mapping for a key from this cache if it is present.
        Specified by:
        removeAsync in interface Cache<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the cache
        Returns:
        a CompletableFuture that will be completed with the previous value associated with key, or null if there was no mapping for key
      • clearAsync

        public CompletableFuture<Void> clearAsync()
        Description copied from interface: Cache
        Asynchronously removes all mappings from this cache.
        Specified by:
        clearAsync in interface Cache<K,​V>
        Returns:
        a CompletableFuture that will be completed when the operation is done
      • config

        public CacheConfig<K,​V> config()
        Description copied from interface: Cache
        Returns the cache configuration.
        Specified by:
        config in interface Cache<K,​V>
        Returns:
        the cache configuration
      • invalidateGlobally

        public CompletableFuture<Void> invalidateGlobally​(K key)
        Description copied from interface: DistributedCache
        Invalidates a key across all nodes in the cluster.
        Specified by:
        invalidateGlobally in interface DistributedCache<K,​V>
        Parameters:
        key - the key to invalidate
        Returns:
        CompletableFuture that completes when invalidation is propagated
      • getReplicationFactor

        public int getReplicationFactor()
        Description copied from interface: DistributedCache
        Returns the replication factor for this cache.
        Specified by:
        getReplicationFactor in interface DistributedCache<K,​V>
        Returns:
        number of replicas maintained for each entry
      • rebalance

        public CompletableFuture<Void> rebalance()
        Description copied from interface: DistributedCache
        Forces a manual rebalancing of the cache across nodes.

        This is typically done automatically, but can be triggered manually after adding or removing nodes from the cluster.

        Specified by:
        rebalance in interface DistributedCache<K,​V>
        Returns:
        CompletableFuture that completes when rebalancing is done
      • addNode

        public CompletableFuture<Void> addNode​(String nodeAddress)
        Description copied from interface: DistributedCache
        Adds a new node to the cluster.
        Specified by:
        addNode in interface DistributedCache<K,​V>
        Parameters:
        nodeAddress - the address of the new node
        Returns:
        CompletableFuture that completes when the node is added
      • setReadRepairEnabled

        public void setReadRepairEnabled​(boolean enabled)
        Description copied from interface: DistributedCache
        Enables or disables read repair for this cache.

        Read repair detects and fixes inconsistencies during read operations.

        Specified by:
        setReadRepairEnabled in interface DistributedCache<K,​V>
        Parameters:
        enabled - whether to enable read repair
      • isReadRepairEnabled

        public boolean isReadRepairEnabled()
        Description copied from interface: DistributedCache
        Returns whether read repair is enabled.
        Specified by:
        isReadRepairEnabled in interface DistributedCache<K,​V>
        Returns:
        true if read repair is enabled