Interface DistributedCache<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this cacheV
- the type of mapped values
- All Known Implementing Classes:
AbstractDistributedCache
,KubernetesDistributedCache
public interface DistributedCache<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.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 Instance Methods Abstract Methods Modifier and Type Method Description CompletableFuture<Void>
clearGlobally()
Clears all entries across all nodes in the cluster.V
get(K key)
CompletableFuture<V>
getAsync(K key)
Gets a value from the cache with the specified consistency level.DistributedCache.ClusterTopology
getClusterTopology()
Returns the current cluster topology.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<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.void
put(K key, V value)
CompletableFuture<Void>
putAsync(K key, V value)
Puts a value in the cache with the specified consistency level.CompletableFuture<Void>
rebalance()
Forces a manual rebalancing of the cache across nodes.
-
-
-
Method Detail
-
putAsync
CompletableFuture<Void> putAsync(K key, V value)
Puts a value in the cache with the specified consistency level.- Parameters:
key
- the keyvalue
- the value- Returns:
- CompletableFuture that completes when the operation is done
-
getAsync
CompletableFuture<V> getAsync(K key)
Gets a value from the cache with the specified consistency level.- Parameters:
key
- the key- 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
-
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
-
getDistributedMetrics
DistributedCache.DistributedMetrics getDistributedMetrics()
Returns detailed metrics about distributed operations.- Returns:
- distributed cache metrics
-
-