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
Modifier and TypeMethodDescriptionstatic voidclearTTL()Clears the TTL value for the current thread.static voidExecutes the givenConsumerwith the effective TTL value.static <R> RExecutes the givenFunctionwith the effective TTL value and returns its result.static DurationgetTTL()Returns the TTL value that has been set for the current thread, ornullif no TTL has been set or it has been cleared.static voidSets the TTL value for the current thread.
-
Constructor Details
-
TTLContext
public TTLContext()
-
-
Method Details
-
doWithTTL
Executes the givenConsumerwith the effective TTL value. If a TTL has been previously set viasetTTL(Duration), that value is used; otherwise, the provideddefaultTTLis used. The TTL is cleared after the consumer completes.Example Usage
doWithTTL(d -> { System.out.println("TTL duration: " + d); }, Duration.ofMillis(10));- Parameters:
ttlFunction- the consumer to execute with the effective TTL durationdefaultTTL- the default TTL to use if no TTL has been set viasetTTL(Duration)
-
doWithTTL
Executes the givenFunctionwith the effective TTL value and returns its result. If a TTL has been previously set viasetTTL(Duration), that value is used; otherwise, the provideddefaultTTLis used. The TTL is cleared after the function completes, even if an exception is thrown.Example Usage
Duration duration = Duration.ofMillis(10); Duration result = doWithTTL(d -> { return d; }, duration);- Type Parameters:
R- the type of the result returned by the function- Parameters:
ttlFunction- the function to execute with the effective TTL durationdefaultTTL- the default TTL to use if no TTL has been set viasetTTL(Duration)- Returns:
- the result of applying the function to the effective TTL
-
setTTL
Sets the TTL value for the current thread. This value will take precedence over thedefaultTTLparameter in subsequent calls todoWithTTL(Consumer, Duration)ordoWithTTL(Function, Duration).Example Usage
Duration duration = Duration.ofMillis(100); setTTL(duration); doWithTTL(d -> { // d will be the previously set duration (100ms), not the default (10ms) assertEquals(d, duration); }, Duration.ofMillis(10));- Parameters:
ttl- the TTL duration to set for the current thread
-
getTTL
Returns the TTL value that has been set for the current thread, ornullif no TTL has been set or it has been cleared.Example Usage
Duration duration = Duration.ofMillis(100); setTTL(duration); assertEquals(duration, getTTL());- Returns:
- the current thread's TTL duration, or
nullif not set
-
clearTTL
public static void clearTTL()Clears the TTL value for the current thread. After calling this method,getTTL()will returnnull.Example Usage
setTTL(Duration.ofMillis(100)); clearTTL(); assertNull(getTTL());
-