fix SimpleCache

This commit is contained in:
Looly 2022-04-10 19:26:48 +08:00
parent 8fc05b93db
commit f471677761
2 changed files with 27 additions and 8 deletions

View File

@ -7,6 +7,7 @@
### ❌不兼容特性 ### ❌不兼容特性
* 【core 】 StreamProgress#progress方法参数变更为2个pr#594@Gitee * 【core 】 StreamProgress#progress方法参数变更为2个pr#594@Gitee
* 【core 】 SimpleCache的raw key使用Mutable
### 🐣新特性 ### 🐣新特性
* 【core 】 CopyOptions支持以Lambda方式设置忽略属性列表pr#590@Gitee * 【core 】 CopyOptions支持以Lambda方式设置忽略属性列表pr#590@Gitee

View File

@ -1,6 +1,9 @@
package cn.hutool.core.lang; package cn.hutool.core.lang;
import cn.hutool.core.collection.TransIter;
import cn.hutool.core.lang.func.Func0; import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.lang.mutable.Mutable;
import cn.hutool.core.lang.mutable.MutableObj;
import java.io.Serializable; import java.io.Serializable;
import java.util.Iterator; import java.util.Iterator;
@ -25,7 +28,7 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
/** /**
* *
*/ */
private final Map<K, V> cache; private final Map<Mutable<K>, V> rawMap;
// 乐观读写锁 // 乐观读写锁
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
/** /**
@ -50,8 +53,8 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
* *
* @param initMap 初始Map用于定义Map类型 * @param initMap 初始Map用于定义Map类型
*/ */
public SimpleCache(Map<K, V> initMap) { public SimpleCache(Map<Mutable<K>, V> initMap) {
this.cache = initMap; this.rawMap = initMap;
} }
/** /**
@ -63,7 +66,7 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
public V get(K key) { public V get(K key) {
lock.readLock().lock(); lock.readLock().lock();
try { try {
return cache.get(key); return rawMap.get(MutableObj.of(key));
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
} }
@ -126,7 +129,7 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
// 独占写锁 // 独占写锁
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
cache.put(key, value); rawMap.put(MutableObj.of(key), value);
} finally { } finally {
lock.writeLock().unlock(); lock.writeLock().unlock();
} }
@ -143,7 +146,7 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
// 独占写锁 // 独占写锁
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
return cache.remove(key); return rawMap.remove(MutableObj.of(key));
} finally { } finally {
lock.writeLock().unlock(); lock.writeLock().unlock();
} }
@ -156,7 +159,7 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
// 独占写锁 // 独占写锁
lock.writeLock().lock(); lock.writeLock().lock();
try { try {
this.cache.clear(); this.rawMap.clear();
} finally { } finally {
lock.writeLock().unlock(); lock.writeLock().unlock();
} }
@ -164,6 +167,21 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
@Override @Override
public Iterator<Map.Entry<K, V>> iterator() { public Iterator<Map.Entry<K, V>> iterator() {
return this.cache.entrySet().iterator(); return new TransIter<>(this.rawMap.entrySet().iterator(), (entry)-> new Map.Entry<K, V>() {
@Override
public K getKey() {
return entry.getKey().get();
}
@Override
public V getValue() {
return entry.getValue();
}
@Override
public V setValue(V value) {
return entry.setValue(value);
}
});
} }
} }