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)
      Executes the given Consumer with the effective TTL value. If a TTL has been previously set via setTTL(Duration), that value is used; otherwise, the provided defaultTTL is 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 duration
      defaultTTL - the default TTL to use if no TTL has been set via setTTL(Duration)
    • doWithTTL

      public static <R> R doWithTTL(Function<Duration,R> ttlFunction, Duration defaultTTL)
      Executes the given Function with the effective TTL value and returns its result. If a TTL has been previously set via setTTL(Duration), that value is used; otherwise, the provided defaultTTL is 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 duration
      defaultTTL - the default TTL to use if no TTL has been set via setTTL(Duration)
      Returns:
      the result of applying the function to the effective TTL
    • setTTL

      public static void setTTL(Duration ttl)
      Sets the TTL value for the current thread. This value will take precedence over the defaultTTL parameter in subsequent calls to doWithTTL(Consumer, Duration) or doWithTTL(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

      public static Duration getTTL()
      Returns the TTL value that has been set for the current thread, or null if 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 null if not set
    • clearTTL

      public static void clearTTL()
      Clears the TTL value for the current thread. After calling this method, getTTL() will return null.

      Example Usage

      
         setTTL(Duration.ofMillis(100));
         clearTTL();
         assertNull(getTTL());