Annotation Interface TTLCachePut
CachePut
that supports time-to-live (TTL) settings for cache entries.
This annotation can be used on types and methods to indicate that the result of
invoking a method should be cached with a specific time-to-live duration. Unlike the
standard CachePut
, this annotation allows specifying an expiration time after
which the cached value will be automatically invalidated.
Example Usage
@TTLCachePut(cacheNames = "userCache", expire = 10, timeUnit = TimeUnit.SECONDS)
public User getUser(String id) {
return findUserById(id);
}
In the example above, the result of the getUser
method will be stored in the
cache named "userCache" and will expire after 10 seconds. Subsequent calls will re-cache
the result after expiration.
When using this annotation, the cache resolver is set to a TTL-aware implementation,
such as TTLCacheResolver
, which ensures that the specified TTL is respected.
- Since:
- 1.0.0
- Author:
- Mercy
-
Required Element Summary
Required Elements -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionString[]
The bean names of the customCacheManager
String[]
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 customKeyGenerator
to use.Thetimeunit
of expireSpring Expression Language (SpEL) expression used to veto method caching.String[]
Alias forcacheNames()
.
-
Element Details
-
expire
long expireThe expire time for cacheable entry
-
-
-
value
Alias forcacheNames()
.- Default:
- {}
-
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:
-
value()
CacheConfig.cacheNames()
- Default:
- {}
-
key
Spring Expression Language (SpEL) expression for computing the key dynamically.Default is
""
, meaning all method parameters are considered as a key, unless a customkeyGenerator()
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 themethod
, 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
The bean name of the customKeyGenerator
to use.Mutually exclusive with the
key()
attribute.- See Also:
-
CacheConfig.keyGenerator()
- Default:
- ""
-
cacheManagers
String[] cacheManagersThe bean names of the customCacheManager
- Default:
- {}
-
condition
Spring Expression Language (SpEL) expression used for making the method caching conditional. Cache the result if the condition evaluates totrue
.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 themethod
, 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
Spring Expression Language (SpEL) expression used to veto method caching. Veto caching the result if the condition evaluates totrue
.Unlike
condition()
, this expression is evaluated after the method has been called and can therefore refer to theresult
.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 asOptional
,#result
refers to the actual object, not the wrapper#root.method
,#root.target
, and#root.caches
for references to themethod
, 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:
- ""
-
timeUnit
TimeUnit timeUnitThetimeunit
of expire- Default:
- MILLISECONDS
-