Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve SoftCache, WeakCache, and FifoCache #3288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/org/apache/ibatis/cache/decorators/FifoCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,6 +49,7 @@ public int getSize() {

public void setSize(int size) {
this.size = size;
resizeKeyList();
}

@Override
Expand Down Expand Up @@ -82,4 +83,11 @@ private void cycleKeyList(Object key) {
}
}

private void resizeKeyList() {
while (keyList.size() > size) {
Object oldestKey = keyList.removeFirst();
delegate.removeObject(oldestKey);
}
}

}
13 changes: 13 additions & 0 deletions src/main/java/org/apache/ibatis/cache/decorators/SoftCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public int getSize() {

public void setSize(int size) {
this.numberOfHardLinks = size;
resizeHardLinks();
}

@Override
Expand All @@ -78,6 +79,7 @@ public Object getObject(Object key) {
// See #586 (and #335) modifications need more than a read lock
lock.lock();
try {
hardLinksToAvoidGarbageCollection.remove(result);
hardLinksToAvoidGarbageCollection.addFirst(result);
if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
hardLinksToAvoidGarbageCollection.removeLast();
Expand Down Expand Up @@ -117,6 +119,17 @@ private void removeGarbageCollectedItems() {
}
}

private void resizeHardLinks() {
lock.lock();
try {
while (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
hardLinksToAvoidGarbageCollection.removeLast();
}
} finally {
lock.unlock();
}
}

private static class SoftEntry extends SoftReference<Object> {
private final Object key;

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/apache/ibatis/cache/decorators/WeakCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public int getSize() {

public void setSize(int size) {
this.numberOfHardLinks = size;
resizeHardLinks();
}

@Override
Expand All @@ -77,6 +78,7 @@ public Object getObject(Object key) {
} else {
lock.lock();
try {
hardLinksToAvoidGarbageCollection.remove(result);
hardLinksToAvoidGarbageCollection.addFirst(result);
if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
hardLinksToAvoidGarbageCollection.removeLast();
Expand Down Expand Up @@ -116,6 +118,17 @@ private void removeGarbageCollectedItems() {
}
}

private void resizeHardLinks() {
lock.lock();
try {
while (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
hardLinksToAvoidGarbageCollection.removeLast();
}
} finally {
lock.unlock();
}
}

private static class WeakEntry extends WeakReference<Object> {
private final Object key;

Expand Down
17 changes: 16 additions & 1 deletion src/test/java/org/apache/ibatis/cache/FifoCacheTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,4 +72,19 @@ void shouldRiseConflictInBeyondFiveEntries() {
assertNotNull(cache.getObject(0));
}

@Test
void shouldResizeKeyList() {
FifoCache cache = new FifoCache(new PerpetualCache("default"));
cache.setSize(5);
for (int i = 0; i < 5; i++) {
cache.putObject(i, i);
}
cache.setSize(3);
assertNull(cache.getObject(0));
assertNull(cache.getObject(1));
assertNotNull(cache.getObject(2));
assertNotNull(cache.getObject(3));
assertNotNull(cache.getObject(4));
}

}