EhCache 3.1.3 持久化问题
EhCache 3.1.3 persistence issue
我在 ehcache 中遇到持久性问题。缓存本身在第一次启动时工作正常,但在我重新启动 tomcat servlet 时它不记得旧值。
这是我初始化 ehcache 的代码。我没有使用任何 xml.
import org.ehcache.Cache;
import org.ehcache.PersistentCacheManager;
import org.ehcache.Status;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
PersistentCacheManager persistentCacheManager =
CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence("/cache"))
.withCache("test-cache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
Integer.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(1, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB)
)
).build(true);
Cache cache = persistentCacheManager.getCache("test-cache", Integer.class, String.class);
log.info("Cache get 1 before putting: " + cache.get(1));
for(int i = 0; i< 1000; i++) {
cache.put(i,"Hello");
}
log.info("Cache get 1 after putting: " + cache.get(1));
"before" 日志语句的结果在重新启动后始终为空。
您需要将磁盘资源池声明为持久的。默认情况下不是。
.disk(20, MemoryUnit.MB, true)
那么你的测试就完美了。
包含关闭缓存管理器的完整代码:
import org.ehcache.Cache;
import org.ehcache.PersistentCacheManager;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.ehcache.config.builders.CacheConfigurationBuilder.*;
import static org.ehcache.config.builders.CacheManagerBuilder.*;
import static org.ehcache.config.builders.ResourcePoolsBuilder.*;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
try(PersistentCacheManager persistentCacheManager =
newCacheManagerBuilder()
.with(persistence("cache"))
.withCache("test-cache",
newCacheConfigurationBuilder(
Integer.class, String.class,
newResourcePoolsBuilder()
.heap(1, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB, true)
)
).build(true)) {
Cache cache = persistentCacheManager.getCache("test-cache", Integer.class, String.class);
log.info("Cache get 1 before putting: " + cache.get(1));
for (int i = 0; i < 1000; i++) {
cache.put(i, "Hello");
}
log.info("Cache get 1 after putting: " + cache.get(1));
}
}
}
我在 ehcache 中遇到持久性问题。缓存本身在第一次启动时工作正常,但在我重新启动 tomcat servlet 时它不记得旧值。 这是我初始化 ehcache 的代码。我没有使用任何 xml.
import org.ehcache.Cache;
import org.ehcache.PersistentCacheManager;
import org.ehcache.Status;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
PersistentCacheManager persistentCacheManager =
CacheManagerBuilder.newCacheManagerBuilder()
.with(CacheManagerBuilder.persistence("/cache"))
.withCache("test-cache",
CacheConfigurationBuilder.newCacheConfigurationBuilder(
Integer.class, String.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(1, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB)
)
).build(true);
Cache cache = persistentCacheManager.getCache("test-cache", Integer.class, String.class);
log.info("Cache get 1 before putting: " + cache.get(1));
for(int i = 0; i< 1000; i++) {
cache.put(i,"Hello");
}
log.info("Cache get 1 after putting: " + cache.get(1));
"before" 日志语句的结果在重新启动后始终为空。
您需要将磁盘资源池声明为持久的。默认情况下不是。
.disk(20, MemoryUnit.MB, true)
那么你的测试就完美了。
包含关闭缓存管理器的完整代码:
import org.ehcache.Cache;
import org.ehcache.PersistentCacheManager;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.ehcache.config.builders.CacheConfigurationBuilder.*;
import static org.ehcache.config.builders.CacheManagerBuilder.*;
import static org.ehcache.config.builders.ResourcePoolsBuilder.*;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
try(PersistentCacheManager persistentCacheManager =
newCacheManagerBuilder()
.with(persistence("cache"))
.withCache("test-cache",
newCacheConfigurationBuilder(
Integer.class, String.class,
newResourcePoolsBuilder()
.heap(1, EntryUnit.ENTRIES)
.offheap(1, MemoryUnit.MB)
.disk(20, MemoryUnit.MB, true)
)
).build(true)) {
Cache cache = persistentCacheManager.getCache("test-cache", Integer.class, String.class);
log.info("Cache get 1 before putting: " + cache.get(1));
for (int i = 0; i < 1000; i++) {
cache.put(i, "Hello");
}
log.info("Cache get 1 after putting: " + cache.get(1));
}
}
}