gpt4 book ai didi

java - 错误的工作 infinispan 树缓存

转载 作者:行者123 更新时间:2023-11-30 10:32:21 26 4
gpt4 key购买 nike

我使用来自 infinispan 8.2.6.Final 的树缓存。我有下一个服务:

public class TestService {
public static final String FIRST_CACHE = "firstCache";

private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

@MCacheable(value = FIRST_CACHE, key = "/#{id}/first")
public Object putInFirstCache(String id, String data) {
log.info("putInFirstCache");
return data;
}

@MCacheable(value = FIRST_CACHE, key = "/#{id}/second")
public Object putInSecondCache(String id, String data) {
log.info("putInSecondCache");
return data;
}

@MCacheEvict(value = FIRST_CACHE, key = "/#{id}")
public void evictFromCache(String id) {
log.info("evictFromCache");
}

}

Aspect @MCacheable 通过 treeCache.put(fqn, "data", result);

将方法的结果放入带有 fqn "key"的 TreeCache

Aspect @MCacheEvict 通过 treeCache.removeNode(fqn); 从缓存中删除值

方面的值是 hashMap 中 TreeCache 的关键。

所以,我针对这方面写了一个简单的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = CacheTestConfig.class, loader = AnnotationConfigContextLoader.class)
public class AspectTest {

@Autowired
private TestService testService;

@Autowired
private TreeCache treeCache;

@Test
public void testMCacheEvicts() throws InterruptedException {
String id = UUID.randomUUID().toString();
String data = System.currentTimeMillis() + "";

testService.putInFirstCache(id, data);
Thread.sleep(2000);
testService.putInSecondCache(id, data);
cacheDump(treeCache);

Thread.sleep(3500);
//miss here
cacheDump(treeCache);
assertEquals(null, treeCacheContainer.get(TestService.FIRST_CACHE).get(id + "/first", "data"));
testService.evictFromCache(id);
cacheDump(treeCache);
assertEquals(null, treeCacheContainer.get(TestService.FIRST_CACHE).get(id + "/second", "data"));

}

private void cacheDump(TreeCache<String, Object> treeCache) {
StringBuilder sb = new StringBuilder("{\r\n");
for (Map.Entry<?, ?> entry : treeCache.getCache().entrySet()) {
sb.append("[ ").append(entry.getKey()).append(",").append(entry.getValue()).append(" ]\r\n");
}
sb.append("}\r\n");

System.out.println("Cache dump: " + sb.toString());
}
}

缓存配置为:

@Bean
public DefaultCacheManager cacheManager() {
org.infinispan.configuration.cache.Configuration cfg = new ConfigurationBuilder()
.jmxStatistics().enable()
.eviction().strategy(EvictionStrategy.LRU).type(EvictionType.COUNT).size(5000)
.expiration().wakeUpInterval(2, TimeUnit.SECONDS).lifespan(5, TimeUnit.SECONDS).maxIdle(100, TimeUnit.SECONDS)
.invocationBatching().enable()
.build();
return new DefaultCacheManager(cfg, true);
}

这个测试的结果很奇怪:

Cache dump: {
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=1} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=0} ]
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5},AtomicHashMap{size=0} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5},AtomicHashMap{size=2} ]
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/first},AtomicHashMap{size=1} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/first},AtomicHashMap{size=0} ]
[ NodeKey{STRUCTURE, fqn=/},AtomicHashMap{size=1} ]
[ NodeKey{DATA, fqn=/},AtomicHashMap{size=0} ]
}

Cache dump: {
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=1} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=0} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5},AtomicHashMap{size=2} ]
}

removed: false, exists: false
Cache dump: {
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=1} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/second},AtomicHashMap{size=0} ]
[ NodeKey{STRUCTURE, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5},AtomicHashMap{size=2} ]
[ NodeKey{DATA, fqn=/e4c3b326-3205-4472-bbc7-3ae50e72ebb5/first},AtomicHashMap{size=0} ]
}

com.test.AspectTest > testMCacheEvicts FAILED
java.lang.AssertionError at AspectTest.java:55

expected:<null> but was:<1489165005306>
Expected :null
Actual :1489165005306
<Click to see difference>

如果子节点存在,谁知道为什么 infinispan 会过期缓存的根节点?

最佳答案

答案很简单,因为TreeCache 实现中没有任何内容来处理过期。

底层缓存分别使每个条目过期,当子项被修改为让他们活着。或者,TreeCache 可能更容易实现,可以确保在父级过期时从缓存中删除所有子级。

关于java - 错误的工作 infinispan 树缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42724342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com