Interface DistributedCache<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this cacheV
- the type of mapped values
- All Superinterfaces:
Cache<K,V>
- All Known Implementing Classes:
DefaultDistributedCache
public interface DistributedCache<K,V> extends Cache<K,V>
Distributed cache interface that extends the base Cache interface with multi-node capabilities.This interface provides seamless scaling from local to distributed caching while maintaining the familiar Cache API. It supports various consistency models, replication strategies, and automatic failover mechanisms.
Key Features:
- Seamless Scaling: Start local, scale to distributed without API changes
- Multiple Consistency Models: Strong, eventual, and session consistency
- Automatic Replication: Data replicated across nodes for high availability
- Partition Tolerance: Continues operating during network partitions
- Self-Healing: Automatic node discovery and failure recovery
- Hybrid Performance: Local performance with distributed availability
Usage Examples:
// Basic distributed cache DistributedCache<String, User> cache = DistributedCache.<String, User>builder() .clusterName("user-service") .nodes("cache-node-1:8080", "cache-node-2:8080", "cache-node-3:8080") .replicationFactor(2) .consistencyLevel(ConsistencyLevel.EVENTUAL) .build(); // Use exactly like a local cache cache.put("user123", user); User retrievedUser = cache.get("user123"); // May come from any node // Distributed-specific operations cache.invalidateGlobally("user123"); // Invalidate across all nodes ClusterTopology topology = cache.getClusterTopology(); Map<String, CacheStats> nodeStats = cache.getPerNodeStats();
Consistency Models:
- STRONG: All nodes see the same data immediately (CP model)
- EVENTUAL: Nodes eventually converge to the same state (AP model)
- SESSION: Guarantees consistency within a session/thread
- MONOTONIC_READ: Once a value is read, subsequent reads return same or newer
- Since:
- 1.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
DistributedCache.Builder<K,V>
Builder interface for creating distributed caches.static class
DistributedCache.ClusterTopology
Cluster topology information.static class
DistributedCache.ConsistencyLevel
Consistency levels for distributed operations.static class
DistributedCache.DistributedMetrics
Metrics specific to distributed cache operations.static class
DistributedCache.NodeInfo
Information about a cluster node.static class
DistributedCache.NodeStatus
Node health status.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description CompletableFuture<Void>
addNode(String nodeAddress)
Adds a new node to the cluster.static <K,V>
DistributedCache.Builder<K,V>builder()
Creates a new distributed cache builder.CompletableFuture<Void>
clearGlobally()
Clears all entries across all nodes in the cluster.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.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.CompletableFuture<Void>
removeNode(String nodeId)
Removes a node from the cluster.void
setReadRepairEnabled(boolean enabled)
Enables or disables read repair for this cache.
-
-
-
Method Detail
-
putWithConsistency
CompletableFuture<Void> putWithConsistency(K key, V value, DistributedCache.ConsistencyLevel consistencyLevel)
Puts a value in the cache with the specified consistency level.- Parameters:
key
- the keyvalue
- the valueconsistencyLevel
- the consistency level for this operation- Returns:
- CompletableFuture that completes when the operation is done
-
getWithConsistency
CompletableFuture<V> getWithConsistency(K key, DistributedCache.ConsistencyLevel consistencyLevel)
Gets a value from the cache with the specified consistency level.- Parameters:
key
- the keyconsistencyLevel
- the consistency level for this operation- Returns:
- CompletableFuture containing the value or null if not found
-
invalidateGlobally
CompletableFuture<Void> invalidateGlobally(K key)
Invalidates a key across all nodes in the cluster.- Parameters:
key
- the key to invalidate- Returns:
- CompletableFuture that completes when invalidation is propagated
-
invalidateGlobally
CompletableFuture<Void> invalidateGlobally(Collection<K> keys)
Invalidates multiple keys across all nodes in the cluster.- Parameters:
keys
- the keys to invalidate- Returns:
- CompletableFuture that completes when invalidation is propagated
-
clearGlobally
CompletableFuture<Void> clearGlobally()
Clears all entries across all nodes in the cluster.- Returns:
- CompletableFuture that completes when clear is propagated
-
getClusterTopology
DistributedCache.ClusterTopology getClusterTopology()
Returns the current cluster topology.- Returns:
- cluster topology information
-
getPerNodeStats
Map<String,CacheStats> getPerNodeStats()
Returns cache statistics for each node in the cluster.- Returns:
- map of node ID to cache statistics
-
getNodeStatuses
Map<String,DistributedCache.NodeStatus> getNodeStatuses()
Returns the status of each node in the cluster.- Returns:
- map of node ID to node status
-
getConsistencyLevel
DistributedCache.ConsistencyLevel getConsistencyLevel()
Returns the consistency level used by this cache.- Returns:
- the default consistency level
-
getReplicationFactor
int getReplicationFactor()
Returns the replication factor for this cache.- Returns:
- number of replicas maintained for each entry
-
rebalance
CompletableFuture<Void> rebalance()
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.
- Returns:
- CompletableFuture that completes when rebalancing is done
-
addNode
CompletableFuture<Void> addNode(String nodeAddress)
Adds a new node to the cluster.- Parameters:
nodeAddress
- the address of the new node- Returns:
- CompletableFuture that completes when the node is added
-
removeNode
CompletableFuture<Void> removeNode(String nodeId)
Removes a node from the cluster.- Parameters:
nodeId
- the ID of the node to remove- Returns:
- CompletableFuture that completes when the node is removed
-
getDistributedMetrics
DistributedCache.DistributedMetrics getDistributedMetrics()
Returns detailed metrics about distributed operations.- Returns:
- distributed cache metrics
-
setReadRepairEnabled
void setReadRepairEnabled(boolean enabled)
Enables or disables read repair for this cache.Read repair detects and fixes inconsistencies during read operations.
- Parameters:
enabled
- whether to enable read repair
-
isReadRepairEnabled
boolean isReadRepairEnabled()
Returns whether read repair is enabled.- Returns:
- true if read repair is enabled
-
builder
static <K,V> DistributedCache.Builder<K,V> builder()
Creates a new distributed cache builder.- Type Parameters:
K
- the key typeV
- the value type- Returns:
- new builder instance
-
-