Class TtlManager
java.lang.Object
com.loomcache.server.lease.TtlManager
Centralized manager for TTL (Time-To-Live) policies across all distributed cache data structures.
Supports multiple TTL policies (fixed, sliding, adaptive, per-key, eternal) and coordinates expiration across different data structures. Uses an ExpirationScheduler for efficient background expiration processing.
Features:
- Set TTL policy per data structure
- Per-key TTL override for PerKeyTtl policies
- Get remaining TTL for any key
- Touch operation for sliding TTL reset
- Event listener support for expiration and TTL changes
- Background expiration processing (configurable interval)
- Statistics tracking: total managed, total expired, avg/min TTL
- Thread-safe with ReentrantLock for complex operations
- Since:
- 1.5
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordStatistics about TTL management. -
Constructor Summary
ConstructorsConstructorDescriptionTtlManager(long scanIntervalMs, Consumer<ExpirationTask> onExpiration) Creates a new TtlManager with configurable scan interval.TtlManager(Consumer<ExpirationTask> onExpiration) Creates a new TtlManager with default 1-second scan interval. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(TtlEventListener listener) Registers a TTL event listener.booleanImmediately expires a key.getExpiredEntries(String dsName) Gets all expired keys for a data structure.getRemainingTtl(String dsName, String key) Gets the remaining TTL for a key.getStats()Gets statistics about TTL management.booleanGets whether the manager is running.voidremoveListener(TtlEventListener listener) Unregisters a TTL event listener.voidsetPolicyForDataStructure(String dsName, TtlPolicy policy) Sets the TTL policy for a data structure.voidSets the TTL for a specific key.voidshutdown()Stops background expiration processing.voidstart()Starts background expiration processing.voidTouches a key to reset its sliding TTL.
-
Constructor Details
-
TtlManager
Creates a new TtlManager with configurable scan interval.- Parameters:
scanIntervalMs- interval in milliseconds between expiration scans (default 1000)onExpiration- callback for expired tasks
-
TtlManager
Creates a new TtlManager with default 1-second scan interval.- Parameters:
onExpiration- callback for expired tasks
-
-
Method Details
-
setPolicyForDataStructure
Sets the TTL policy for a data structure.All entries in this data structure will follow the specified policy, unless overridden by per-key TTL settings.
- Parameters:
dsName- the data structure name (e.g., "users")policy- the TTL policy to apply- Throws:
NullPointerException- if dsName or policy is null
-
setTtl
Sets the TTL for a specific key.This is used to override the data structure's default policy for a single key. Only applies if the data structure uses PerKeyTtl policy.
- Parameters:
dsName- the data structure namekey- the cache keyttl- the time-to-live duration- Throws:
NullPointerException- if dsName, key, or ttl is null
-
getRemainingTtl
-
touch
-
expireNow
-
getExpiredEntries
Gets all expired keys for a data structure.This scans the TTL metadata and identifies keys whose absolute expiration deadline has elapsed. Note: keys are not automatically removed; this method provides a list for explicit cleanup.
- Parameters:
dsName- the data structure name- Returns:
- list of expired keys
-
addListener
Registers a TTL event listener.- Parameters:
listener- the listener to register
-
removeListener
Unregisters a TTL event listener.- Parameters:
listener- the listener to remove
-
start
public void start()Starts background expiration processing. -
shutdown
public void shutdown()Stops background expiration processing. -
isRunning
public boolean isRunning()Gets whether the manager is running.- Returns:
- true if background processing is active
-
getStats
Gets statistics about TTL management.- Returns:
- TtlStats record with current statistics
-