我使用Spring + Ehcache返回一个String列表:
@Cacheable(value = "test", key = "\"myKey"")
public List<String> getValues() {
return getMyValues();
}Ehcache可以配置为在缓存超时时自动重新填充缓存吗?我知道使用@CacheEvict清除缓存,但这是手动操作。
最新情况:
以下是一个可能的解决方案:
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;
}发布于 2014-11-07 08:34:04
Ehcache有一些内置的解决方案,但是它们需要在使用缓存的方式上进行一些调整。
有关更多详细信息,请参阅这个答案。
这些构造不是通过Spring的缓存抽象公开的,参见Spring缓存文档的第29.7点。
发布于 2014-11-06 17:23:35
我只想猜一猜:
bootstrapCacheLoaderFactory -指定一个BootstrapCacheLoader,它在初始化时由缓存调用,以预填充自身。
cacheLoaderFactory -指定一个CacheLoader,它可以异步和同步地用于将对象加载到缓存中。可以添加多个cacheLoaderFactory元素,在这种情况下,加载器形成一个按顺序执行的链。如果加载程序返回null,则调用链中的下一个。
timeToIdleSeconds可能是一种使数据更新并与下面的资源保持同步的方法。
timeToLiveSeconds:设置元素过期前的生存期。即从创建时间到元素过期之间的最长时间。只有在元素不是永恒的情况下才会使用。可选属性值为0意味着和元素可以存在无穷大。默认值为0。
可能有两件事要调查。我说的所有这些都是在Ehcache.xml文件中为您的恶棍设置的。
http://ehcache.org/ehcache.xml
你可以在ehcache的网站上找到大量的信息(和论坛,所以你可能想在那里发布你的问题,以获得更多的专业帮助)。
https://stackoverflow.com/questions/26784380
复制相似问题