Spring 后自动缓存失效
Automatic cache invalidation in Spring
我有一个 class 从服务 XXX 执行一些读取操作。这些读取操作最终将执行数据库读取,我想通过将每个方法的结果缓存在 class 中为每个方法指定的自定义键来优化这些调用。
Class a {
public Output1 func1(Arguments1 ...) {
...
}
public Output2 func2(Arguments2 ...) {
...
}
public Output3 func3(Arguments3 ...) {
...
}
public Output4 func4(Arguments4 ...) {
...
}
}
我正在考虑使用 Spring caching(@Cacheable annotation)
来缓存每种方法的结果。
但是,我希望缓存失效通过某种机制(ttl 等)自动发生。在 Spring 缓存中可能吗?我知道我们有一个 @CacheEvict
注释,但我希望自动进行驱逐。
如有任何帮助,我们将不胜感激。
根据 Spring documentation(第 36.8 节):
How can I set the TTL/TTI/Eviction policy/XXX feature?
Directly through your cache provider. The cache abstraction is...
well, an abstraction not a cache implementation. The solution you are
using might support various data policies and different topologies
which other solutions do not (take for example the JDK
ConcurrentHashMap) - exposing that in the cache abstraction would be
useless simply because there would no backing support. Such
functionality should be controlled directly through the backing cache,
when configuring it or through its native API.@
这意味着 Spring 不会直接公开 API 来设置 Time To Live ,而是依赖于缓存提供程序实现来设置它。这意味着如果缓存提供程序允许动态设置这些属性,则您需要设置 Time 以通过公开的缓存管理器生存。或者,您应该自己配置 Spring 使用 @Cacheable
注释的缓存区域。
为了找到 @Cacheable
公开的缓存区域的名称。您可以使用 JMX 控制台浏览应用程序中的可用缓存区域。
如果您正在使用 EHCache,例如一旦您知道了缓存区域,您就可以像这样提供 xml 配置:
<cache name="myCache"
maxEntriesLocalDisk="10000" eternal="false" timeToIdleSeconds="3600"
timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU">
</cache>
我再次重申,所有配置都是特定于缓存提供程序的,Spring 在处理它时不会公开接口。
REMARK:如果没有定义缓存提供程序,则由Spring配置的默认缓存提供程序是ConcurrentHashMap
。它不支持生存时间。为了获得此功能,您必须切换到不同的缓存提供程序(例如 EHCache)。
我有一个 class 从服务 XXX 执行一些读取操作。这些读取操作最终将执行数据库读取,我想通过将每个方法的结果缓存在 class 中为每个方法指定的自定义键来优化这些调用。
Class a {
public Output1 func1(Arguments1 ...) {
...
}
public Output2 func2(Arguments2 ...) {
...
}
public Output3 func3(Arguments3 ...) {
...
}
public Output4 func4(Arguments4 ...) {
...
}
}
我正在考虑使用 Spring caching(@Cacheable annotation)
来缓存每种方法的结果。
但是,我希望缓存失效通过某种机制(ttl 等)自动发生。在 Spring 缓存中可能吗?我知道我们有一个 @CacheEvict
注释,但我希望自动进行驱逐。
如有任何帮助,我们将不胜感激。
根据 Spring documentation(第 36.8 节):
How can I set the TTL/TTI/Eviction policy/XXX feature?
Directly through your cache provider. The cache abstraction is... well, an abstraction not a cache implementation. The solution you are using might support various data policies and different topologies which other solutions do not (take for example the JDK ConcurrentHashMap) - exposing that in the cache abstraction would be useless simply because there would no backing support. Such functionality should be controlled directly through the backing cache, when configuring it or through its native API.@
这意味着 Spring 不会直接公开 API 来设置 Time To Live ,而是依赖于缓存提供程序实现来设置它。这意味着如果缓存提供程序允许动态设置这些属性,则您需要设置 Time 以通过公开的缓存管理器生存。或者,您应该自己配置 Spring 使用 @Cacheable
注释的缓存区域。
为了找到 @Cacheable
公开的缓存区域的名称。您可以使用 JMX 控制台浏览应用程序中的可用缓存区域。
如果您正在使用 EHCache,例如一旦您知道了缓存区域,您就可以像这样提供 xml 配置:
<cache name="myCache"
maxEntriesLocalDisk="10000" eternal="false" timeToIdleSeconds="3600"
timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU">
</cache>
我再次重申,所有配置都是特定于缓存提供程序的,Spring 在处理它时不会公开接口。
REMARK:如果没有定义缓存提供程序,则由Spring配置的默认缓存提供程序是ConcurrentHashMap
。它不支持生存时间。为了获得此功能,您必须切换到不同的缓存提供程序(例如 EHCache)。