Skip to content

Commit

Permalink
LRUCacheMap optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Apr 5, 2017
1 parent 71a19f7 commit 015ebbe
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions redisson/src/main/java/org/redisson/cache/LRUCacheMap.java
Expand Up @@ -15,8 +15,14 @@
*/ */
package org.redisson.cache; package org.redisson.cache;


import java.util.Queue; import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue; 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.concurrent.atomic.AtomicLong;


/** /**
* LRU (least recently used) cache. * LRU (least recently used) cache.
Expand All @@ -28,24 +34,38 @@
*/ */
public class LRUCacheMap<K, V> extends AbstractCacheMap<K, V> { public class LRUCacheMap<K, V> extends AbstractCacheMap<K, V> {


private final Queue<CachedValue> queue = new ConcurrentLinkedQueue<CachedValue>(); private final AtomicLong index = new AtomicLong();
private final List<Collection<CachedValue<K, V>>> queues =
new ArrayList<Collection<CachedValue<K, V>>>(Runtime.getRuntime().availableProcessors()*2);


public LRUCacheMap(int size, long timeToLiveInMillis, long maxIdleInMillis) { public LRUCacheMap(int size, long timeToLiveInMillis, long maxIdleInMillis) {
super(size, timeToLiveInMillis, maxIdleInMillis); super(size, timeToLiveInMillis, maxIdleInMillis);

for (int i = 0; i < Runtime.getRuntime().availableProcessors()*2; i++) {
Set<CachedValue<K, V>> instance = Collections.synchronizedSet(new LinkedHashSet<CachedValue<K, V>>());
queues.add(instance);
}
} }


@Override @Override
protected void onValueCreate(CachedValue value) { protected void onValueCreate(CachedValue<K, V> value) {
Collection<CachedValue<K, V>> queue = getQueue(value);
queue.add(value); queue.add(value);
} }

private Collection<CachedValue<K, V>> getQueue(CachedValue<K, V> value) {
return queues.get(value.hashCode() % queues.size());
}


@Override @Override
protected void onValueRemove(CachedValue value) { protected void onValueRemove(CachedValue<K, V> value) {
Collection<CachedValue<K, V>> queue = getQueue(value);
queue.remove(value); queue.remove(value);
} }


@Override @Override
protected void onValueRead(CachedValue value) { protected void onValueRead(CachedValue<K, V> value) {
Collection<CachedValue<K, V>> queue = getQueue(value);
// move value to tail of queue // move value to tail of queue
if (queue.remove(value)) { if (queue.remove(value)) {
queue.add(value); queue.add(value);
Expand All @@ -54,15 +74,22 @@ protected void onValueRead(CachedValue value) {


@Override @Override
protected void onMapFull() { protected void onMapFull() {
CachedValue value = queue.poll(); Collection<CachedValue<K, V>> queue = queues.get((int)Math.abs(index.incrementAndGet() % queues.size()));
if (value != null) { synchronized (queue) {
map.remove(value.getKey(), value); Iterator<CachedValue<K, V>> iter = queue.iterator();
if (iter.hasNext()) {
CachedValue<K, V> value = iter.next();
iter.remove();
map.remove(value.getKey(), value);
}
} }
} }


@Override @Override
public void clear() { public void clear() {
queue.clear(); for (Collection<CachedValue<K, V>> collection : queues) {
collection.clear();
}
super.clear(); super.clear();
} }


Expand Down

0 comments on commit 015ebbe

Please sign in to comment.