All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.g2forge.alexandria.java.associative.cache.LRUCacheEvictionPolicy Maven / Gradle / Ivy

There is a newer version: 0.0.18
Show newest version
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();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy