首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Spring或scheduledExecutorService重新填充ehcache值

使用Spring或scheduledExecutorService重新填充ehcache值
EN

Stack Overflow用户
提问于 2014-11-06 16:25:10
回答 2查看 493关注 0票数 0

我使用Spring + Ehcache返回一个String列表:

代码语言:javascript
复制
@Cacheable(value = "test", key = "\"myKey"")
    public List<String> getValues()  {
        return getMyValues();
    }

Ehcache可以配置为在缓存超时时自动重新填充缓存吗?我知道使用@CacheEvict清除缓存,但这是手动操作。

最新情况:

以下是一个可能的解决方案:

代码语言:javascript
复制
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;

public class MyCacheScheduler {

    public void repopulateCache(){

        ScheduledExecutorService scheduledExecutorService =
                Executors.newScheduledThreadPool(5);

        @SuppressWarnings({ "rawtypes", "unchecked" })
        ScheduledFuture scheduledFuture =
            scheduledExecutorService.schedule(new Callable() {
                public Object call() throws Exception  {
                    myCache.clearCache();
                    myCache.populateCache();
                    return "";
                }
            },
            30,
            TimeUnit.SECONDS);

        try {
            System.out.println("result = " + scheduledFuture.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Autowired
    private MyCacheManagerImpl myCache;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-07 08:34:04

Ehcache有一些内置的解决方案,但是它们需要在使用缓存的方式上进行一些调整。

有关更多详细信息,请参阅这个答案

这些构造不是通过Spring的缓存抽象公开的,参见Spring缓存文档的第29.7点

票数 1
EN

Stack Overflow用户

发布于 2014-11-06 17:23:35

我只想猜一猜:

bootstrapCacheLoaderFactory -指定一个BootstrapCacheLoader,它在初始化时由缓存调用,以预填充自身。

cacheLoaderFactory -指定一个CacheLoader,它可以异步和同步地用于将对象加载到缓存中。可以添加多个cacheLoaderFactory元素,在这种情况下,加载器形成一个按顺序执行的链。如果加载程序返回null,则调用链中的下一个。

timeToIdleSeconds可能是一种使数据更新并与下面的资源保持同步的方法。

timeToLiveSeconds:设置元素过期前的生存期。即从创建时间到元素过期之间的最长时间。只有在元素不是永恒的情况下才会使用。可选属性值为0意味着和元素可以存在无穷大。默认值为0。

可能有两件事要调查。我说的所有这些都是在Ehcache.xml文件中为您的恶棍设置的。

http://ehcache.org/ehcache.xml

你可以在ehcache的网站上找到大量的信息(和论坛,所以你可能想在那里发布你的问题,以获得更多的专业帮助)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26784380

复制
相关文章

相似问题

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