Spring缓存使用bean属性过期 [英] spring cache expire using bean property
问题描述
是否有一种方法可以根据缓存的POJO的属性来指示缓存对象过期/退出.
Is there a way of indicating to expire/evict the cache object based on a property of the POJO cached.
在下面的代码中,它缓存 Foo
实例.Foo类具有 expiresIn
属性Foo类{日期expires;}
In below code, it caches Foo
instance. Foo class has a expiresIn
property
class Foo {
Date expiresIn;
}
我想根据缓存元素的 expiresIn
属性值暗示弹簧使缓存过期.这可行吗?
I want to hint to spring to expire the cache based on the value of expiresIn
property of cached element. Is this feasible?
@Cacheable("my-cache-key")
Foo getCachedToken(String userName, String password) throws AuthException
我的Cache.xml如下:
My Cache.xml is below:
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">
<property name="cacheManagers">
<list>
<ref bean="mapCacheManager"/>
</list>
</property>
</bean>
<bean id="mapCacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager">
<property name="cacheNames">
<list>
<value>my-cache-key</value>
</list>
</property>
</bean>
推荐答案
This is a cache infrastructure abstraction; It's not a cache provider. ConcurrentMapCacheManager
is a very simple implementation we provide for testing purposes or for super-simple use cases. If you need an eviction policy, choose a caching library that does support that. Ehcache, Guava or Hazelcast come to mind. All of them have a CacheManager
implementation.
这篇关于Spring缓存使用bean属性过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!