经过大量调试后发现,神秘的行为取决于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());
}
}