Skip to content

Commit

Permalink
fix: concurrent problem in LinkedHashMapCache, affect 2.7.5 and 2.7.6 (
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Sep 12, 2024
1 parent 405ad14 commit 8d9dafd
Showing 1 changed file with 2 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;

/**
* @author huangli
Expand Down Expand Up @@ -48,8 +47,7 @@ public void cleanExpiredEntry() {
final class LRUMap extends LinkedHashMap implements InnerMap {

private final int max;
// private final Object lockObj;
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private final ReentrantLock lock = new ReentrantLock();

public LRUMap(int max) {
super((int) (max * 1.4f), 0.75f, true);
Expand All @@ -63,7 +61,6 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
}

void cleanExpiredEntry() {
Lock lock = readWriteLock.writeLock();
lock.lock();
long t = System.currentTimeMillis();
try {
Expand Down Expand Up @@ -95,7 +92,6 @@ void cleanExpiredEntry() {

@Override
public Object getValue(Object key) {
Lock lock = readWriteLock.readLock();
lock.lock();
try{
return get(key);
Expand All @@ -106,7 +102,6 @@ public Object getValue(Object key) {

@Override
public Map getAllValues(Collection keys) {
Lock lock = readWriteLock.readLock();
lock.lock();
Map values = new HashMap();
try{
Expand All @@ -124,7 +119,6 @@ public Map getAllValues(Collection keys) {

@Override
public void putValue(Object key, Object value) {
Lock lock = readWriteLock.writeLock();
lock.lock();
try{
put(key, value);
Expand All @@ -135,7 +129,6 @@ public void putValue(Object key, Object value) {

@Override
public void putAllValues(Map map) {
Lock lock = readWriteLock.writeLock();
lock.lock();
try{
Set<Map.Entry> set = map.entrySet();
Expand All @@ -149,7 +142,6 @@ public void putAllValues(Map map) {

@Override
public boolean removeValue(Object key) {
Lock lock = readWriteLock.writeLock();
lock.lock();
try{
return remove(key) != null;
Expand All @@ -160,7 +152,6 @@ public boolean removeValue(Object key) {

@Override
public void removeAllValues(Collection keys) {
Lock lock = readWriteLock.writeLock();
lock.lock();
try{
for (Object k : keys) {
Expand All @@ -174,7 +165,6 @@ public void removeAllValues(Collection keys) {
@Override
@SuppressWarnings("unchecked")
public boolean putIfAbsentValue(Object key, Object value) {
Lock lock = readWriteLock.writeLock();
lock.lock();
try{
CacheValueHolder h = (CacheValueHolder) get(key);
Expand Down

0 comments on commit 8d9dafd

Please sign in to comment.