我正在使用SpringFramework3.2.4编写一个java项目。
我有许多需要缓存10秒的SQL查询。
我知道,通过@cacheable注释,我可以缓存函数结果。
我不明白的是如何只缓存10秒。我知道您可以将条件添加到可缓存的注释中,但我很难弄清楚如何在这些条件中添加时间。
如能提供有关这一问题的任何资料,将不胜感激。
发布于 2013-11-04 13:38:39
Spring不提供这一点,但它支持适配器,例如,您可以使用番石榴适配器,它允许配置过期超时。
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<list>
<bean name="testCache"
class="org.hypoport.springGuavaCacheAdapter.SpringGuavaCacheAdapter">
<property name="expireAfterAccessInSeconds" value="10"/>
<property name="expireAfterWriteInSeconds" value="10"/>
</bean>
</list>
</property>
</bean>发布于 2015-06-11 07:01:47
您可以使用Scheduler定期调用驱逐缓存的服务方法。
调度程序:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.beans.factory.annotation.Autowired;
public class Scheduler {
@Autowired
private SomeService someService;
@Scheduled(fixedRate = 10000)
public void evictCaches() {
someService.evictCaches();
}
}服务:
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class SomeService {
@CacheEvict(value = { "cache1", "cache2" }, allEntries = true)
public void evictAllCaches() {
}
}https://stackoverflow.com/questions/19768481
复制相似问题