com.g2forge.alexandria.java.associative.cache.LRUCacheEvictionPolicy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ax-java Show documentation
Show all versions of ax-java Show documentation
Standard Java library and the basis of the ${alexandria.name} project.
package com.g2forge.alexandria.java.associative.cache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class LRUCacheEvictionPolicy implements ICacheEvictionPolicy {
protected final int size;
protected final LinkedList keys = new LinkedList<>();
public LRUCacheEvictionPolicy(int size) {
this.size = size;
}
@Override
public Collection access(boolean create, K key) {
if (!create) keys.remove(key);
keys.add(key);
if (keys.size() > size) {
final List toEvict = keys.subList(0, keys.size() - size);
final ArrayList retVal = new ArrayList<>(toEvict);
toEvict.clear();
return retVal;
}
return Collections.emptyList();
}
}