Spring缓存使用bean属性过期 [英] spring cache expire using bean property

查看:84
本文介绍了Spring缓存使用bean属性过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以根据缓存的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>

推荐答案

我想您错过了节名称这是缓存基础结构的抽象;它不是缓存提供程序. ConcurrentMapCacheManager 是我们提供的非常简单的实现,用于测试目的或用于超简单的用例.如果需要驱逐策略,请选择一个支持该策略的缓存库.想到Ehcache,Guava或Hazelcast.它们都有一个 CacheManager 实现.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆