mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
优化缓存模块的命中计数器
This commit is contained in:
parent
d60b315a20
commit
0361d7684c
@ -9,7 +9,7 @@ import java.util.Iterator;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.LongAdder;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.concurrent.locks.StampedLock;
|
import java.util.concurrent.locks.StampedLock;
|
||||||
@ -58,11 +58,11 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
|
|||||||
/**
|
/**
|
||||||
* 命中数,即命中缓存计数
|
* 命中数,即命中缓存计数
|
||||||
*/
|
*/
|
||||||
protected AtomicLong hitCount = new AtomicLong();
|
protected LongAdder hitCount = new LongAdder();
|
||||||
/**
|
/**
|
||||||
* 丢失数,即未命中缓存计数
|
* 丢失数,即未命中缓存计数
|
||||||
*/
|
*/
|
||||||
protected AtomicLong missCount = new AtomicLong();
|
protected LongAdder missCount = new LongAdder();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存监听
|
* 缓存监听
|
||||||
@ -133,14 +133,14 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
|
|||||||
* @return 命中数
|
* @return 命中数
|
||||||
*/
|
*/
|
||||||
public long getHitCount() {
|
public long getHitCount() {
|
||||||
return hitCount.get();
|
return hitCount.sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return 丢失数
|
* @return 丢失数
|
||||||
*/
|
*/
|
||||||
public long getMissCount() {
|
public long getMissCount() {
|
||||||
return missCount.get();
|
return missCount.sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -188,10 +188,10 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
|
|||||||
|
|
||||||
// 未命中
|
// 未命中
|
||||||
if (null == co) {
|
if (null == co) {
|
||||||
missCount.getAndIncrement();
|
missCount.increment();
|
||||||
return null;
|
return null;
|
||||||
} else if (false == co.isExpired()) {
|
} else if (false == co.isExpired()) {
|
||||||
hitCount.getAndIncrement();
|
hitCount.increment();
|
||||||
return co.get(isUpdateLastAccess);
|
return co.get(isUpdateLastAccess);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -368,7 +368,7 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
|
|||||||
final CacheObj<K, V> co = cacheMap.remove(key);
|
final CacheObj<K, V> co = cacheMap.remove(key);
|
||||||
if (withMissCount) {
|
if (withMissCount) {
|
||||||
// 在丢失计数有效的情况下,移除一般为get时的超时操作,此处应该丢失数+1
|
// 在丢失计数有效的情况下,移除一般为get时的超时操作,此处应该丢失数+1
|
||||||
this.missCount.getAndIncrement();
|
this.missCount.increment();
|
||||||
}
|
}
|
||||||
return co;
|
return co;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user