Skip to content

Commit

Permalink
Merge pull request #2960 from oah1021/fix-remove-keymap-issue
Browse files Browse the repository at this point in the history
Fixed the removeObject method key-value pair removal issue
  • Loading branch information
hazendaz authored Sep 22, 2023
2 parents b444fda + 40e86ab commit 512a216
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public Object getObject(Object key) {

@Override
public Object removeObject(Object key) {
keyMap.remove(key);
return delegate.removeObject(key);
}

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/apache/ibatis/cache/LruCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.util.Map;

class LruCacheTest {

@Test
Expand Down Expand Up @@ -60,4 +63,18 @@ void shouldFlushAllItemsOnDemand() {
assertNull(cache.getObject(4));
}

@Test
void shouldCacheSizeEqualsKeyMapSize() throws Exception {
LruCache cache = new LruCache(new PerpetualCache("default"));
cache.setSize(5);
for (int i = 0; i < 5; i++) {
cache.putObject(i, i);
}
cache.removeObject(1);
Field keyMap = cache.getClass().getDeclaredField("keyMap");
keyMap.setAccessible(true);
Map<Object, Object> map = (Map<Object, Object>) keyMap.get(cache);
assertEquals(map.size(), cache.getSize());
}

}

0 comments on commit 512a216

Please sign in to comment.