diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/AbstractCache.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/AbstractCache.java index a23c93ba6f..b6f023605d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/AbstractCache.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/AbstractCache.java @@ -185,7 +185,7 @@ public long tick() { long current = now(); for (Iterator> it = this.nodes(); it.hasNext();) { CacheNode node = it.next(); - if (current - node.time() > expireTime) { + if (current - node.time() >= expireTime) { // Remove item while iterating map (it must be ConcurrentMap) this.remove(node.key()); expireItems++; diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CacheTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CacheTest.java index ae9afe7ecb..ee6ae1c8ec 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CacheTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/unit/cache/CacheTest.java @@ -59,7 +59,7 @@ public void teardown() throws Exception { protected abstract Cache newCache(); - protected abstract Cache newCache(long capacity) ; + protected abstract Cache newCache(long capacity); protected abstract void checkSize(Cache cache, long size, Map kvs); @@ -673,5 +673,24 @@ public void testMutiThreadsGetAndUpdateWithGtCapacity() { // In fact, the size may be any value(such as 43) Assert.assertTrue(cache.size() < 10 + THREADS_NUM); } + + @Test + public void testKeyExpired() { + Cache cache = newCache(); + cache.expire(2000L); + + Id key = IdGenerator.of("key"); + cache.update(key, "value", -1000L); + + waitTillNext(1); + cache.tick(); + + Assert.assertFalse(cache.containsKey(key)); + + cache.update(key, "value", -2000L); + cache.tick(); + + Assert.assertFalse(cache.containsKey(key)); + } }