如何设计实现一个LRU Cache?

1. 什么是LRU Cache?

之前,在LeetCode上看到一个LRU Cache实现的题目,题目描述是这样的:

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

简单的说,就是保证基本的get和set的功能的同时,还要保证最近访问(get或put)的节点保持在限定容量的Cache中,如果超过容量则应该把LRU(近期最少使用)的节点删除掉。

那么我们思考一个问题:如何设计实现一个LRU Cache?
那么,我们可能需要使用类似这样的数据结构去实现这个LRU Cache:

这不就是LinkedHashMap吗!

这样做的好处是,getset在不冲突的情况下可以保证O(1)的复杂度,同时,也可以通过双向链表来保证LRU的删除更新操作也能保证O(1)的复杂度。

2.实现思路

在学习了HashMap(#7 )和LinkedHashMap(#8 )后,是不是觉得这俩数据结构简直太适合做LRU Cache了!那么动手实现一下:
基于HashMap和双向链表的实现

public class LRUCache {
    class Node {
    	Node pre;
    	Node next;
    	Integer key;
    	Integer val;
    	
    	Node(Integer k, Integer v) {
    		key = k;
    		val = v;
    	}
    }
    
    Map<Integer, Node> map = new HashMap<Integer, Node>();
    // The head (eldest) of the doubly linked list.
    Node head;
    // The tail (youngest) of the doubly linked list.
    Node tail;
    int cap;
    public LRUCache(int capacity) {
        cap = capacity;
        head = new Node(null, null);
        tail = new Node(null, null);
        head.next = tail;
        tail.pre = head;
    }
    
    public int get(int key) {
        Node n = map.get(key);
        if(n!=null) {
        	n.pre.next = n.next;
        	n.next.pre = n.pre;
        	appendTail(n);
        	return n.val;
        }
        return -1;
    }
    
    public void set(int key, int value) {
        Node n = map.get(key);
        // existed
        if(n!=null) {
	        n.val = value;
	        map.put(key, n);
        	n.pre.next = n.next;
        	n.next.pre = n.pre;
        	appendTail(n);
        	return;
        }
        // else {
        if(map.size() == cap) {
        	Node tmp = head.next;
        	head.next = head.next.next;
        	head.next.pre = head;
        	map.remove(tmp.key);
        }
        n = new Node(key, value);
        // youngest node append taill
        appendTail(n);
        map.put(key, n);
    }

    private void appendTail(Node n) {
    	n.next = tail;
    	n.pre = tail.pre;
    	tail.pre.next = n;
    	tail.pre = n;
    }
}

基于LinkedHashMap的实现
HashMap+双向链表?这不就是LinkedHashMap吗!

public class LRUCache {
    
    private int capacity;
    private Map<Integer, Integer> cache;
    
    public LRUCache(int capacity) {
        this.capacity = capacity;
        this.cache = new java.util.LinkedHashMap<Integer, Integer> (capacity, 0.75f, true) {
            // 定义put后的移除规则,大于容量就删除eldest
            protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
                return size() > capacity;
            }
        };
    }
    
    public int get(int key) {
        if (cache.containsKey(key)) {
            return cache.get(key);
        } else
            return -1;
    }
    
    public void set(int key, int value) {
        cache.put(key, value);
    }
}


上面的两种实现方式都不是线程安全的

下面介绍用LinkedHashMap线程安全的实现LRU

public class LRULinkedHashMap<K,V> extends LinkedHashMap<K,V> {
	private final int maxCapacity;
	private static final float DEFAULT_FACTOR=0.75f;
	private final Lock lock = new ReentrantLock();
	public LRULinkedHashMap(int maxCapacity) {
		super(maxCapacity, DEFAULT_FACTOR,true);
		this.maxCapacity = maxCapacity;
	}
	
	@Override 
	protected boolean removeEldestEntry(Map.Entry<K,V> eldest){
		return size() > maxCapacity;
	}
	
	@Override
	public V get(Object key){
		try {
			lock.lock();
			return super.get(key);
		}finally {
			lock.unlock();
		}
	}
	
	@Override
	public V put(K key, V value){
		try {
			lock.lock();
			return super.put(key, value);
			
		} finally {
			lock.unlock();
		}
	}
	
//只有get and put是线程安全的
}


可以用jdk5的ReadWriteLock操作,get操作可以不用block住的。可以用读写锁来解决

public class LRULinkedHashMap2<K, V> extends LinkedHashMap<K,V> {
	private final int maxCapacity;
	private static final float DEFAULT_LOAD_FACTOR = 0.75f;
	private ReadWriteLock globalLock;
	private  Lock readLock;
	private Lock  writeLock;
	
	public LRULinkedHashMap2(int maxCapacity){
		super(maxCapacity, DEFAULT_LOAD_FACTOR, true);
		this.maxCapacity = maxCapacity;
		globalLock = new ReentrantReadWriteLock();
		readLock = globalLock.readLock();
		writeLock = globalLock.writeLock();
	}
	
	

	@Override
	protected boolean removeEldestEntry(Map.Entry<K, V> eldest){
		return size() > maxCapacity;
	}
  
	@Override
	public V get(Object key){
		readLock.lock();
		try{
			return super.get(key);
		}
		finally
		{
			readLock.unlock();
		}
	}
	
	@Override
	public  V put(K key, V value){
		writeLock.lock();
		try {
			return super.put(key, value);
		} finally{
			writeLock.unlock();
		}
	}
	
}



LRU 缓存是一种常见的缓存淘汰算法,它的设计思想是将最近最少使用的数据从缓存中淘汰出去,以保证缓存的数据都是热点数据,从而提高缓存的命中率。 下面是一个基于双向链表和哈希表实现LRU 缓存的 C++ 设计实现: ```cpp #include <unordered_map> struct Node { int key; int value; Node* prev; Node* next; Node(int k, int v) : key(k), value(v), prev(nullptr), next(nullptr) {} }; class LRUCache { public: LRUCache(int capacity) : size_(capacity), head_(nullptr), tail_(nullptr) {} int get(int key) { auto iter = cache_.find(key); if (iter == cache_.end()) { return -1; } else { Node* node = iter->second; move_to_head(node); return node->value; } } void put(int key, int value) { auto iter = cache_.find(key); if (iter == cache_.end()) { Node* node = new Node(key, value); add_to_head(node); cache_[key] = node; if (cache_.size() > size_) { Node* tail = remove_tail(); cache_.erase(tail->key); delete tail; } } else { Node* node = iter->second; node->value = value; move_to_head(node); } } private: int size_; Node* head_; Node* tail_; std::unordered_map<int, Node*> cache_; void add_to_head(Node* node) { if (head_ == nullptr) { head_ = node; tail_ = node; } else { node->next = head_; head_->prev = node; head_ = node; } } void move_to_head(Node* node) { if (node == head_) { return; } else if (node == tail_) { tail_ = tail_->prev; tail_->next = nullptr; } else { node->prev->next = node->next; node->next->prev = node->prev; } node->next = head_; head_->prev = node; node->prev = nullptr; head_ = node; } Node* remove_tail() { Node* node = tail_; if (head_ == tail_) { head_ = nullptr; tail_ = nullptr; } else { tail_ = tail_->prev; tail_->next = nullptr; } return node; } }; ``` 在这个实现中,我们使用了一个双向链表保存缓存中的数据,并使用一个哈希表来提高查找效率。在 `get` 操作中,如果缓存中不存在目标数据则返回 -1,否则将目标数据移到链表头部并返回其值。在 `put` 操作中,如果缓存中不存在目标数据则创建一个新的节点并将其添加到链表头部,如果缓存已满则删除链表尾部的节点。如果缓存中已存在目标数据则将其值更新并将其移动到链表头部。 需要注意的是,在 `move_to_head` 和 `remove_tail` 操作中,我们需要判断目标节点是否已经在链表的头部或尾部,以避免对空指针进行操作。此外,在每次操作中,我们还需要更新哈希表中对应节点的指针。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值