提交 bfc3a607 编写于 作者: N Nikita Koksharov

LRUCacheMap optimization

上级 9b64a712
......@@ -178,8 +178,7 @@ public abstract class AbstractCacheMap<K, V> implements Cache<K, V> {
return put(key, value, timeToLiveInMillis, TimeUnit.MILLISECONDS, maxIdleInMillis, TimeUnit.MILLISECONDS);
}
@Override
public V put(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) {
private V put(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) {
CachedValue<K, V> entry = create(key, value, ttlUnit.toMillis(ttl), maxIdleUnit.toMillis(maxIdleTime));
if (isFull(key)) {
if (!removeExpiredEntries()) {
......@@ -205,6 +204,10 @@ public abstract class AbstractCacheMap<K, V> implements Cache<K, V> {
}
protected boolean removeExpiredEntries() {
if (timeToLiveInMillis == 0 && maxIdleInMillis == 0) {
return false;
}
boolean removed = false;
// TODO optimize
for (CachedValue<K, V> value : map.values()) {
......
......@@ -16,7 +16,6 @@
package org.redisson.cache;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
*
......@@ -27,6 +26,4 @@ import java.util.concurrent.TimeUnit;
*/
public interface Cache<K, V> extends Map<K, V> {
V put(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit);
}
......@@ -15,13 +15,7 @@
*/
package org.redisson.cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
/**
......@@ -83,16 +77,21 @@ public class LRUCacheMap<K, V> extends AbstractCacheMap<K, V> {
if (startIndex == -1) {
startIndex = queueIndex;
}
Collection<CachedValue<K, V>> queue = queues.get(queueIndex);
CachedValue<K, V> removedValue = null;
synchronized (queue) {
Iterator<CachedValue<K, V>> iter = queue.iterator();
if (iter.hasNext()) {
CachedValue<K, V> value = iter.next();
removedValue = iter.next();
iter.remove();
map.remove(value.getKey(), value);
return;
}
}
if (removedValue != null) {
map.remove(removedValue.getKey(), removedValue);
return;
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册