mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix SimpleCache
This commit is contained in:
parent
8fc05b93db
commit
f471677761
@ -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)
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user