package com.saroj.cache;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class LRUCache {
	private int size;
	private ConcurrentHashMap map;
	private ConcurrentLinkedQueue queue;
	
	public LRUCache(int maxSize){
		this.size=maxSize;
		map = new ConcurrentHashMap();
		queue = new ConcurrentLinkedQueue();
	}
	
	public void put(final Key key, final Value value){
		if(map.containsKey(key)){
			queue.add(key);
		}
		while(queue.size() >=size){
			Key oldestKey = queue.poll();
			if(null !=oldestKey){
				map.remove(oldestKey);
			}
		}
		queue.add(key);
		map.put(key, value);
	}
	
	public Value get(final Key key){
		return map.get(key);
	}
}