Package io.github.dhruv1110.jcachex.impl
Class DefaultDistributedCache<K,V>
- java.lang.Object
-
- io.github.dhruv1110.jcachex.impl.DefaultDistributedCache<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this cacheV
- 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DefaultDistributedCache.Builder<K,V>
-
Nested classes/interfaces inherited from interface io.github.dhruv1110.jcachex.distributed.DistributedCache
DistributedCache.ClusterTopology, DistributedCache.ConsistencyLevel, DistributedCache.DistributedMetrics, DistributedCache.NodeInfo, DistributedCache.NodeStatus
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<Void>
addNode(String nodeAddress)
Adds a new node to the cluster.void
clear()
Removes all mappings from this cache.CompletableFuture<Void>
clearAsync()
Asynchronously removes all mappings from this cache.CompletableFuture<Void>
clearGlobally()
Clears all entries across all nodes in the cluster.CacheConfig<K,V>
config()
Returns the cache configuration.boolean
containsKey(K key)
Returns true if this cache contains a mapping for the specified key.Set<Map.Entry<K,V>>
entries()
Returns a view of all the key-value pairs in this cache.V
get(K key)
Returns the value associated with the key in this cache, or null if there is no cached value for the key.CompletableFuture<V>
getAsync(K key)
Asynchronously returns the value associated with the key in this cache.DistributedCache.ClusterTopology
getClusterTopology()
Returns the current cluster topology.DistributedCache.ConsistencyLevel
getConsistencyLevel()
Returns the consistency level used by this cache.DistributedCache.DistributedMetrics
getDistributedMetrics()
Returns detailed metrics about distributed operations.Map<String,DistributedCache.NodeStatus>
getNodeStatuses()
Returns the status of each node in the cluster.Map<String,CacheStats>
getPerNodeStats()
Returns cache statistics for each node in the cluster.int
getReplicationFactor()
Returns the replication factor for this cache.CompletableFuture<V>
getWithConsistency(K key, DistributedCache.ConsistencyLevel consistencyLevel)
Gets a value from the cache with the specified consistency level.CompletableFuture<Void>
invalidateGlobally(Collection<K> keys)
Invalidates multiple keys across all nodes in the cluster.CompletableFuture<Void>
invalidateGlobally(K key)
Invalidates a key across all nodes in the cluster.boolean
isReadRepairEnabled()
Returns whether read repair is enabled.Set<K>
keys()
Returns a view of all the keys in this cache.void
put(K key, V value)
Associates the specified value with the specified key in this cache.CompletableFuture<Void>
putAsync(K key, V value)
Asynchronously associates the specified value with the specified key in this cache.CompletableFuture<Void>
putWithConsistency(K key, V value, DistributedCache.ConsistencyLevel consistencyLevel)
Puts a value in the cache with the specified consistency level.CompletableFuture<Void>
rebalance()
Forces a manual rebalancing of the cache across nodes.V
remove(K key)
Removes the mapping for a key from this cache if it is present.CompletableFuture<V>
removeAsync(K key)
Asynchronously removes the mapping for a key from this cache if it is present.CompletableFuture<Void>
removeNode(String nodeId)
Removes a node from the cluster.void
setReadRepairEnabled(boolean enabled)
Enables or disables read repair for this cache.long
size()
Returns the approximate number of entries in this cache.CacheStats
stats()
Returns the cache statistics.Collection<V>
values()
Returns a view of all the values in this cache.
-
-
-
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.
-
put
public void put(K key, V value)
Description copied from interface:Cache
Associates the specified value with the specified key in this cache.
-
remove
public V remove(K key)
Description copied from interface:Cache
Removes the mapping for a key from this cache if it is present.
-
clear
public void clear()
Description copied from interface:Cache
Removes all mappings from this cache.
-
size
public long size()
Description copied from interface:Cache
Returns the approximate 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 interfaceCache<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.
-
values
public Collection<V> values()
Description copied from interface:Cache
Returns a view of all the values 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.
-
stats
public CacheStats stats()
Description copied from interface:Cache
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.
-
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.
-
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 interfaceCache<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 interfaceCache<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.
-
putWithConsistency
public CompletableFuture<Void> putWithConsistency(K key, V value, DistributedCache.ConsistencyLevel consistencyLevel)
Description copied from interface:DistributedCache
Puts a value in the cache with the specified consistency level.- Specified by:
putWithConsistency
in interfaceDistributedCache<K,V>
- Parameters:
key
- the keyvalue
- the valueconsistencyLevel
- the consistency level for this operation- Returns:
- CompletableFuture that completes when the operation is done
-
getWithConsistency
public CompletableFuture<V> getWithConsistency(K key, DistributedCache.ConsistencyLevel consistencyLevel)
Description copied from interface:DistributedCache
Gets a value from the cache with the specified consistency level.- Specified by:
getWithConsistency
in interfaceDistributedCache<K,V>
- Parameters:
key
- the keyconsistencyLevel
- the consistency level for this operation- Returns:
- CompletableFuture containing the value or null if not found
-
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 interfaceDistributedCache<K,V>
- Parameters:
key
- the key to invalidate- Returns:
- CompletableFuture that completes when invalidation is propagated
-
invalidateGlobally
public CompletableFuture<Void> invalidateGlobally(Collection<K> keys)
Description copied from interface:DistributedCache
Invalidates multiple keys across all nodes in the cluster.- Specified by:
invalidateGlobally
in interfaceDistributedCache<K,V>
- Parameters:
keys
- the keys to invalidate- Returns:
- CompletableFuture that completes when invalidation is propagated
-
clearGlobally
public CompletableFuture<Void> clearGlobally()
Description copied from interface:DistributedCache
Clears all entries across all nodes in the cluster.- Specified by:
clearGlobally
in interfaceDistributedCache<K,V>
- Returns:
- CompletableFuture that completes when clear is propagated
-
getClusterTopology
public DistributedCache.ClusterTopology getClusterTopology()
Description copied from interface:DistributedCache
Returns the current cluster topology.- Specified by:
getClusterTopology
in interfaceDistributedCache<K,V>
- Returns:
- cluster topology information
-
getPerNodeStats
public Map<String,CacheStats> getPerNodeStats()
Description copied from interface:DistributedCache
Returns cache statistics for each node in the cluster.- Specified by:
getPerNodeStats
in interfaceDistributedCache<K,V>
- Returns:
- map of node ID to cache statistics
-
getNodeStatuses
public Map<String,DistributedCache.NodeStatus> getNodeStatuses()
Description copied from interface:DistributedCache
Returns the status of each node in the cluster.- Specified by:
getNodeStatuses
in interfaceDistributedCache<K,V>
- Returns:
- map of node ID to node status
-
getConsistencyLevel
public DistributedCache.ConsistencyLevel getConsistencyLevel()
Description copied from interface:DistributedCache
Returns the consistency level used by this cache.- Specified by:
getConsistencyLevel
in interfaceDistributedCache<K,V>
- Returns:
- the default consistency level
-
getReplicationFactor
public int getReplicationFactor()
Description copied from interface:DistributedCache
Returns the replication factor for this cache.- Specified by:
getReplicationFactor
in interfaceDistributedCache<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 interfaceDistributedCache<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 interfaceDistributedCache<K,V>
- Parameters:
nodeAddress
- the address of the new node- Returns:
- CompletableFuture that completes when the node is added
-
removeNode
public CompletableFuture<Void> removeNode(String nodeId)
Description copied from interface:DistributedCache
Removes a node from the cluster.- Specified by:
removeNode
in interfaceDistributedCache<K,V>
- Parameters:
nodeId
- the ID of the node to remove- Returns:
- CompletableFuture that completes when the node is removed
-
getDistributedMetrics
public DistributedCache.DistributedMetrics getDistributedMetrics()
Description copied from interface:DistributedCache
Returns detailed metrics about distributed operations.- Specified by:
getDistributedMetrics
in interfaceDistributedCache<K,V>
- Returns:
- distributed cache metrics
-
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 interfaceDistributedCache<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 interfaceDistributedCache<K,V>
- Returns:
- true if read repair is enabled
-
-