我可以使用isKeyInCache並將putIfAbsent方法替換爲ehcache嗎?我可以使用isKeyInCache並將putIfAbsent方法替換爲ehcache嗎?
這裏是我的測試代碼,性能差別很大,
// putIfAbsent
time = System.nanoTime();
ehcache.putIfAbsent(new Element(assetUid, asset));
estimated = System.nanoTime();
System.out.println(estimated - time);
// isKeyInCache and put
time = System.nanoTime();
if (!ehcache.isKeyInCache(assetUid)) {
ehcache.put(new Element(assetUid, asset));
}
estimated = System.nanoTime();
System.out.println(estimated - time);
和控制檯輸出
1693409
18235
或者你有其他的建議?謝謝