Skip to content

Commit ac9d836

Browse files
committed
Use ReentrantLock instead of ReentrantReadWriteLock
Using `ReentrantReadWriteLock` changes the behavior and it could increase the chance of cache stampede.
1 parent 79f33a4 commit ac9d836

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/main/java/org/apache/ibatis/cache/decorators/SynchronizedCache.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
1515
*/
1616
package org.apache.ibatis.cache.decorators;
1717

18-
import java.util.concurrent.locks.ReentrantReadWriteLock;
18+
import java.util.concurrent.locks.ReentrantLock;
1919

2020
import org.apache.ibatis.cache.Cache;
2121

@@ -24,7 +24,7 @@
2424
*/
2525
public class SynchronizedCache implements Cache {
2626

27-
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
27+
private final ReentrantLock lock = new ReentrantLock();
2828
private final Cache delegate;
2929

3030
public SynchronizedCache(Cache delegate) {
@@ -38,51 +38,51 @@ public String getId() {
3838

3939
@Override
4040
public int getSize() {
41-
readWriteLock.readLock().lock();
41+
lock.lock();
4242
try {
4343
return delegate.getSize();
4444
} finally {
45-
readWriteLock.readLock().unlock();
45+
lock.unlock();
4646
}
4747
}
4848

4949
@Override
5050
public void putObject(Object key, Object object) {
51-
readWriteLock.writeLock().lock();
51+
lock.lock();
5252
try {
5353
delegate.putObject(key, object);
5454
} finally {
55-
readWriteLock.writeLock().unlock();
55+
lock.unlock();
5656
}
5757
}
5858

5959
@Override
6060
public Object getObject(Object key) {
61-
readWriteLock.readLock().lock();
61+
lock.lock();
6262
try {
6363
return delegate.getObject(key);
6464
} finally {
65-
readWriteLock.readLock().unlock();
65+
lock.unlock();
6666
}
6767
}
6868

6969
@Override
7070
public Object removeObject(Object key) {
71-
readWriteLock.writeLock().lock();
71+
lock.lock();
7272
try {
7373
return delegate.removeObject(key);
7474
} finally {
75-
readWriteLock.writeLock().unlock();
75+
lock.unlock();
7676
}
7777
}
7878

7979
@Override
8080
public void clear() {
81-
readWriteLock.writeLock().lock();
81+
lock.lock();
8282
try {
8383
delegate.clear();
8484
} finally {
85-
readWriteLock.writeLock().unlock();
85+
lock.unlock();
8686
}
8787
}
8888

0 commit comments

Comments
 (0)