Skip to content

io microsphere spring cache TTLContext

github-actions[bot] edited this page Jun 18, 2026 · 18 revisions

TTLContext

Type: Class | Module: microsphere-spring-context | Package: io.microsphere.spring.cache | Since: 1.0.0

Source: microsphere-spring-context/src/main/java/io/microsphere/spring/cache/TTLContext.java

Overview

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();
}

Declaration

public class TTLContext

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.27-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

// 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();

Method Examples

doWithTTL

doWithTTL(d -> {
      System.out.println("TTL duration: " + d);
  }, Duration.ofMillis(10));

doWithTTL

Duration duration = Duration.ofMillis(10);
  Duration result = doWithTTL(d -> {
      return d;
  }, duration);

setTTL

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));

getTTL

Duration duration = Duration.ofMillis(100);
  setTTL(duration);
  assertEquals(duration, getTTL());

clearTTL

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

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-spring-context</artifactId>
    <version>${microsphere-spring.version}</version>
</dependency>

Tip: Use the BOM (microsphere-spring-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.spring.cache.TTLContext;

API Reference

Public Methods

Method Description
doWithTTL Executes the given Consumer with the effective TTL value. If a TTL has been
doWithTTL Executes the given Function with the effective TTL value and returns its result.
setTTL Sets the TTL value for the current thread. This value will take precedence over the
getTTL Returns the TTL value that has been set for the current thread, or null if
clearTTL Clears the TTL value for the current thread. After calling this method,

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));
}

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);
}

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));
}

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());
`

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());
`

This documentation was auto-generated from the source code of microsphere-spring.

Home

spring-context

spring-guice

spring-jdbc

spring-test

spring-web

spring-webflux

spring-webmvc

Clone this wiki locally