问题
Is there a way of indicating to expire/evict the cache object based on a property of the POJO cached.
In below code, it caches Foo
instance. Foo class has a expiresIn
property
class Foo {
Date 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
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>
回答1:
I guess you missed the section name How can I set the TTL/TTI/Eviction policy/XXX feature? from the reference guide.
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.
来源:https://stackoverflow.com/questions/29037234/spring-cache-expire-using-bean-property