Package io.microsphere.spring.cache
Class TTLContext
java.lang.Object
io.microsphere.spring.cache.TTLContext
A context class that manages Time-To-Live (TTL) values using a thread-local variable.
This class provides utility methods to execute operations with a specified TTL,
supporting both void and return-value operations via functional interfaces.
Example Usage
// Using doWithTTL with a Consumer to handle TTL value
TTLContext.doWithTTL(ttl -> {
System.out.println("Current TTL: " + ttl);
}, Duration.ofSeconds(30));
// Using doWithTTL with a Function to handle TTL and return a result
String result = TTLContext.doWithTTL(ttl -> {
return "TTL is: " + ttl;
}, Duration.ofSeconds(60));
// Setting TTL manually
TTLContext.setTTL(Duration.ofMinutes(5));
Duration currentTTL = TTLContext.getTTL();
// Clearing TTL manually
TTLContext.clearTTL();
- Since:
- 1.0.0
- Author:
- Mercy
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
TTLContext
public TTLContext()
-
-
Method Details
-
doWithTTL
-
doWithTTL
-
setTTL
-
getTTL
-
clearTTL
public static void clearTTL()
-