请用Java设计一个Least Recently Used (LRU) 缓存

LRU介绍:LRU是Least Recently Used的缩写,即最少使用页面置换算法,是为虚拟页式存储管理服务的,

思路介绍:

能够使用两个标准的数据结构来实现。Map和Queue。由于须要支持多线程。须要使用实现了java.utili.concurrent.*的Map和Queue。

主要思路是使用一个Queue来维护FIFO和Map来对数据进行排序。当向缓存加入新的元素时,共同拥有下面三种可能

1. 假设该元素已经在Cache中存在(Map),我们会从queue中删除改元素并将其加入到queue的第一个位置。

2. 假设缓存已满无法新增新的元素,我们会从queue和Map中删除最后面的那个元素并把新元素加入进来。

3. 同一时候在Map和Queue中添加新的元素

简单的代码例如以下:

package cn.kge.comdemo;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class LRUCache<K,V> {
	    /**
	     * LRU缓存的最大容量.
	     */
		private final int capacity;
		//用来保持近期使用的元素的Queue.
		private ConcurrentLinkedQueue<K> queue;
		private ConcurrentHashMap<K, V> map;
	 
		/**
		 * 初始化LRU缓存
		 * @param capacity
		 */
		public LRUCache(final int capacity) {
			this.capacity = capacity;
			this.queue	= new ConcurrentLinkedQueue<K>();
			this.map	= new ConcurrentHashMap<K, V>(capacity);
		}
	 
		/**
		 * 检查该元素释放在缓存中存在,假设不存在则返回null
		 * @param key
		 * @return 
		 */
		public V get(final K key) {
			return map.get(key);
		}
	 
		/**
		 * 将元素加入到LRU缓存。

假设Key已存在,则将其放到缓存的第一位置 * @param key * @param value * @throws NullPointerException */ public synchronized void put(final K key, final V value) { if(key == null || value == null) { throw new NullPointerException(); } if (map.containsKey(key)) { queue.remove(key); } while (queue.size() >= capacity) { K expiredKey = queue.poll(); if (expiredKey != null) { map.remove(expiredKey); } } queue.add(key); map.put(key, value); } }




原文地址:https://www.cnblogs.com/yjbjingcha/p/6925438.html