经过一番折腾,事实证明神秘的行为取决于 L1 缓存是否知道 L2 缓存中的内容。附加的单元测试是成对执行的,对中的每个元素都在单独的 JVM 中运行,以避免 EHCache 单例。
我认为行为良好的 L1->L2 关系应该起作用的方式是,如果您执行 .put() 没有错误,您应该能够在没有问题的情况下立即执行同一键的 get()(假设没有其他并发运行的线程在搞乱东西)。但是,对于 Terracotta EHCache,如果 .put() 需要驱逐某些东西,则驱逐不会发生,并且 put() 会被静默忽略,除非 L1 客户端“知道”可以驱逐的密钥。在 *JVM2 测试中,它通过使用 .getAllWithLoader() 找出其他键,然后 *JVM2 测试按预期工作。
所以我们为解决我们的原始问题所做的就是让客户端定期执行 .getAllWithLoader()。然后我们可以确定所有的驱逐规则都被遵守了。
package com.mine;
import java.io.Serializable;
import junit.framework.TestCase;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.TerracottaClientConfiguration;
import net.sf.ehcache.config.TerracottaConfiguration;
public class CacheEvictionTest extends TestCase {
private static final String SERVER = "localhost:9510";
private CacheManager cacheManager;
private Cache cache;
private final Serializable keyA = "a";
private final Serializable keyB = "b";
private final Serializable keyC = "c";
@Override
protected void setUp() throws Exception {
Configuration configuration = new Configuration();
TerracottaClientConfiguration terracottaConfig = new TerracottaClientConfiguration();
terracottaConfig.setUrl(SERVER);
configuration.addTerracottaConfig(terracottaConfig);
int maxElementsInMemory = 1;
int maxElementsOnDisk = 2;
long timeToIdleSeconds = 15;
long timeToLiveSeconds = 15;
String cacheName = "TEST_CACHE";
CacheConfiguration amoebaCache = new CacheConfiguration(cacheName, maxElementsInMemory).statistics(true)
.terracotta(new TerracottaConfiguration())
.logging(true)
.maxElementsOnDisk(maxElementsOnDisk)
.timeToIdleSeconds(timeToIdleSeconds)
.timeToLiveSeconds(timeToLiveSeconds);
configuration.addCache(amoebaCache);
configuration.addDefaultCache(new CacheConfiguration("default", 0));
cacheManager = new CacheManager(configuration);
cache = cacheManager.getCache(cacheName);
}
@Override
protected void tearDown() throws Exception {
if (cache != null) {
cache.removeAll();
cache.clearStatistics();
}
}
public void testMaxElementOnDiskEvictionJVM1() throws Exception {
cache.clearStatistics();
cache.put(new Element(keyA, keyA));
cache.put(new Element(keyB, keyB));
cache = null;
}
public void testMaxElementOnDiskEvictionJVM2() throws Exception {
assertEquals(2, cache.getSize());
for (Object key : cache.getKeys()) {
cache.get(key;
}
cache.put(new Element(keyC, keyC));
assertEquals(2, cache.getSize());
assertNotNull(cache.get(keyC));
}
public void testEvictsExpiredElementsFromDiskWhenNotInMemoryAndWeNeverKnewAboutItJVM1() throws Exception {
cache.clearStatistics();
cache.put(new Element(keyA, keyA));
cache = null;
cacheManager = null;
}
public void testEvictsExpiredElementsFromDiskWhenNotInMemoryAndWeNeverKnewAboutItJVM2() throws Exception {
cache.clearStatistics();
for (Object key : cache.getKeys()) {
cache.get(key;
}
assertEquals(0, cache.getStatistics().getEvictionCount());
Thread.sleep(20000);
assertEquals(1, cache.getStatistics().getEvictionCount());
}
}