首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置spring @cacheable缓存10秒

设置spring @cacheable缓存10秒
EN

Stack Overflow用户
提问于 2013-11-04 13:15:46
回答 2查看 8.4K关注 0票数 2

我正在使用SpringFramework3.2.4编写一个java项目。

我有许多需要缓存10秒的SQL查询。

我知道,通过@cacheable注释,我可以缓存函数结果。

我不明白的是如何只缓存10秒。我知道您可以将条件添加到可缓存的注释中,但我很难弄清楚如何在这些条件中添加时间。

如能提供有关这一问题的任何资料,将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-04 13:38:39

Spring不提供这一点,但它支持适配器,例如,您可以使用番石榴适配器,它允许配置过期超时。

代码语言:javascript
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2015-06-11 07:01:47

您可以使用Scheduler定期调用驱逐缓存的服务方法。

调度程序:

代码语言:javascript
复制
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();
        }
}

服务:

代码语言:javascript
复制
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() {
        }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19768481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档