spring cache expire using bean property

跟風遠走 提交于 2019-12-13 18:18:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!