Skip to content

Commit

Permalink
Improve Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
puppy4c committed Nov 2, 2024
1 parent 0dd4487 commit d06c5fe
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
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));
}

}

0 comments on commit d06c5fe

Please sign in to comment.