Class TTLContext

java.lang.Object
io.microsphere.spring.cache.TTLContext

public class TTLContext extends Object
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 Details

    • TTLContext

      public TTLContext()
  • Method Details

    • doWithTTL

      public static void doWithTTL(Consumer<Duration> ttlFunction, Duration defaultTTL)
    • doWithTTL

      public static <R> R doWithTTL(Function<Duration,R> ttlFunction, Duration defaultTTL)
    • setTTL

      public static void setTTL(Duration ttl)
    • getTTL

      public static Duration getTTL()
    • clearTTL

      public static void clearTTL()