Annotation Interface TTLCacheable


@Target({TYPE,METHOD}) @Retention(RUNTIME) @Inherited @Documented @Cacheable(cacheResolver="ttlCacheResolver") public @interface TTLCacheable
A variation of @Cacheable that supports Time-To-Live (TTL) settings for cached entries.

This annotation allows the specification of an expiration time for cache entries, making it possible to control how long values should be kept in the cache before being considered stale and evicted.

Example Usage


     @TTLCacheable(cacheNames = "userCache", expire = 10, timeUnit = TimeUnit.SECONDS)
     public User getUser(String userId) {
         // Method implementation
     }
 

In the example above, the result of the method will be stored in the "userCache" cache for 10 seconds. After that time, the entry will be removed, and the next invocation will cause the method to be executed and the result cached again.

It is typically used in Spring applications where fine-grained control over cache expiration is needed. The TTL behavior is handled by the associated cache resolver, which must support TTL-based eviction policies.

Since:
1.0.0
Author:
Mercy
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    long
    The expire time for cacheable entry
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The bean names of the custom CacheManager
    Names of the caches in which method invocation results are stored.
    Spring Expression Language (SpEL) expression used for making the method caching conditional.
    Spring Expression Language (SpEL) expression for computing the key dynamically.
    The bean name of the custom KeyGenerator to use.
    boolean
    Synchronize the invocation of the underlying method if several threads are attempting to load a value for the same key.
    The timeunit of expire
    Spring Expression Language (SpEL) expression used to veto method caching.
    Alias for cacheNames().
  • Element Details

    • expire

      long expire
      The expire time for cacheable entry
    • value

      @AliasFor(annotation=org.springframework.cache.annotation.Cacheable.class) String[] value
      Alias for cacheNames().
      Default:
      {}
    • cacheNames

      @AliasFor(annotation=org.springframework.cache.annotation.Cacheable.class) String[] cacheNames
      Names of the caches in which method invocation results are stored.

      Names may be used to determine the target cache (or caches), matching the qualifier value or bean name of a specific bean definition.

      Since:
      4.2
      See Also:
      Default:
      {}
    • key

      @AliasFor(annotation=org.springframework.cache.annotation.Cacheable.class) String key
      Spring Expression Language (SpEL) expression for computing the key dynamically.

      Default is "", meaning all method parameters are considered as a key, unless a custom keyGenerator() has been configured.

      The SpEL expression evaluates against a dedicated context that provides the following meta-data:

      • #root.method, #root.target, and #root.caches for references to the method, target object, and affected cache(s) respectively.
      • Shortcuts for the method name (#root.methodName) and target class (#root.targetClass) are also available.
      • Method arguments can be accessed by index. For instance the second argument can be accessed via #root.args[1], #p1 or #a1. Arguments can also be accessed by name if that information is available.
      Default:
      ""
    • keyGenerator

      @AliasFor(annotation=org.springframework.cache.annotation.Cacheable.class) String keyGenerator
      The bean name of the custom KeyGenerator to use.

      Mutually exclusive with the key() attribute.

      See Also:
      • CacheConfig.keyGenerator()
      Default:
      ""
    • cacheManagers

      String[] cacheManagers
      The bean names of the custom CacheManager
      Default:
      {}
    • condition

      @AliasFor(annotation=org.springframework.cache.annotation.Cacheable.class) String condition
      Spring Expression Language (SpEL) expression used for making the method caching conditional. Cache the result if the condition evaluates to true.

      Default is "", meaning the method result is always cached.

      The SpEL expression evaluates against a dedicated context that provides the following meta-data:

      • #root.method, #root.target, and #root.caches for references to the method, target object, and affected cache(s) respectively.
      • Shortcuts for the method name (#root.methodName) and target class (#root.targetClass) are also available.
      • Method arguments can be accessed by index. For instance the second argument can be accessed via #root.args[1], #p1 or #a1. Arguments can also be accessed by name if that information is available.
      Default:
      ""
    • unless

      @AliasFor(annotation=org.springframework.cache.annotation.Cacheable.class) String unless
      Spring Expression Language (SpEL) expression used to veto method caching. Veto caching the result if the condition evaluates to true.

      Unlike condition(), this expression is evaluated after the method has been called and can therefore refer to the result.

      Default is "", meaning that caching is never vetoed.

      The SpEL expression evaluates against a dedicated context that provides the following meta-data:

      • #result for a reference to the result of the method invocation. For supported wrappers such as Optional, #result refers to the actual object, not the wrapper
      • #root.method, #root.target, and #root.caches for references to the method, target object, and affected cache(s) respectively.
      • Shortcuts for the method name (#root.methodName) and target class (#root.targetClass) are also available.
      • Method arguments can be accessed by index. For instance the second argument can be accessed via #root.args[1], #p1 or #a1. Arguments can also be accessed by name if that information is available.
      Since:
      3.2
      Default:
      ""
    • sync

      @AliasFor(annotation=org.springframework.cache.annotation.Cacheable.class) boolean sync
      Synchronize the invocation of the underlying method if several threads are attempting to load a value for the same key. The synchronization leads to a couple of limitations:
      1. unless() is not supported
      2. Only one cache may be specified
      3. No other cache-related operation can be combined
      This is effectively a hint and the actual cache provider that you are using may not support it in a synchronized fashion. Check your provider documentation for more details on the actual semantics.
      Since:
      4.3
      See Also:
      • Cache.get(Object, Callable)
      Default:
      false
    • timeUnit

      TimeUnit timeUnit
      The timeunit of expire
      Default:
      MILLISECONDS