Class NearCacheInvalidationStrategy
Purpose
Handles near-cache invalidation using either server-push (preferred) or TTL-based polling (fallback). If server-push listener registration fails, automatically falls back to periodic polling to ensure near-cache eventually becomes consistent.
Modes
- PUSH: Server pushes invalidations (low latency, preferred)
- POLL: Client polls TTL-based invalidations (eventual consistency, fallback)
Failover Logic
When listener registration fails:
- Retries with exponential backoff (100ms, 200ms, 400ms, 800ms, 1600ms, 3200ms, 5000ms cap)
- After max retries, falls back to polling mode
- A gauge metric tracks the current mode for observability
Thread-safe through atomic references and concurrent hash map.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumInvalidation strategy mode.static interfaceCallback interface for poll-based invalidation.static interface -
Constructor Summary
ConstructorsConstructorDescriptionNearCacheInvalidationStrategy(LoomClient client, long nearCacheTtlMs) Create a near-cache invalidation strategy. -
Method Summary
Modifier and TypeMethodDescriptionvoidClear tracked near-cache registrations after connection loss.voidGet the current invalidation mode.voidonNodeDisconnected(String nodeId) Drop per-node subscription bookkeeping for a connection that just closed.voidregisterListenerWithRetry(String mapName, LoomClient.MapChangeListener listener) Register a listener for near-cache invalidation with retry resilience.voidregisterMapWithRetry(String mapName) Register the map for server-push near-cache invalidation messages.voidSet the callback for poll-based invalidation.voidvoidshutdown()Shut down the strategy, canceling all retry and poll tasks.voidunregisterListener(String mapName, LoomClient.MapChangeListener listener) Unregister a listener for near-cache invalidation.voidunregisterMap(String mapName) Unregister a map from server-push near-cache invalidations.
-
Constructor Details
-
NearCacheInvalidationStrategy
Create a near-cache invalidation strategy.- Parameters:
client- the LoomClient instance (must not be null)nearCacheTtlMs- the near cache TTL in milliseconds (0 means infinite)- Throws:
NullPointerException- if client is nullIllegalArgumentException- if nearCacheTtlMs is negative
-
-
Method Details
-
getCurrentMode
Get the current invalidation mode. Useful for metrics/observability.- Returns:
- the current mode (PUSH or POLL, never null)
-
registerListenerWithRetry
Register a listener for near-cache invalidation with retry resilience.If registration fails, retries with exponential backoff. After max retries, the strategy falls back to TTL-based polling.
- Parameters:
mapName- the map to register for invalidation (must not be null)listener- the listener to invoke on invalidation (must not be null)- Throws:
NullPointerException- if mapName or listener is null
-
registerMapWithRetry
Register the map for server-push near-cache invalidation messages. -
unregisterListener
Unregister a listener for near-cache invalidation.- Parameters:
mapName- the map name (must not be null)listener- the listener to unregister (must not be null)- Throws:
NullPointerException- if mapName or listener is null
-
unregisterMap
Unregister a map from server-push near-cache invalidations. -
clearRegistrations
public void clearRegistrations()Clear tracked near-cache registrations after connection loss.Cancels any pending retry tasks and removes all registration bookkeeping so a later reconnect can re-register near-cache subscriptions cleanly.
-
shutdown
public void shutdown()Shut down the strategy, canceling all retry and poll tasks. -
onNodeDisconnected
Drop per-node subscription bookkeeping for a connection that just closed.Lets the client react to a partial disconnect without wiping registrations for the remaining healthy nodes. Callers should invoke this when the connection count is still greater than zero; a total disconnect should use
clearRegistrations()instead so later reconnect starts from a clean slate.- Parameters:
nodeId- the node that just disconnected (must not be null)
-
setPollInvalidationCallback
public void setPollInvalidationCallback(NearCacheInvalidationStrategy.PollInvalidationCallback callback) Set the callback for poll-based invalidation. Must be called before registration attempts so the POLL fallback works.- Parameters:
callback- the callback to invoke during poll cycles
-
setReconciliationCallback
public void setReconciliationCallback(NearCacheInvalidationStrategy.ReconciliationCallback callback) -
configureReconciliation
-