LRU

本文介绍了一种使用Linked List和HashMap实现的LRU(Least Recently Used)缓存策略。该策略通过维护一个双向链表和哈希映射来跟踪最近使用的数据项,并确保能够高效地更新缓存中的数据。
import java.util.*;


/*
 * we are using linked list with hashmap for Lru.Reason behind this is ,since HashMap is able to store data and key but
 * how do we get info about least recently used cache value?. For this we need to keep track all inserted data into map
 * by using linked list. When inserting new values , add this value to the top of linked list. When key,value is already
 * present then refresh it by removing it from the linked list and adding it to top of the linked list. 
 * */

public class CacheStrategies {
    
    public interface CacheStrategy<K, T>{
        T get(K key);
        void put(K key,T data);
    }
    
    class CacheStrategyLRU<K, T> implements CacheStrategy<K, T> {
        
        class Node{
            K key;
            T data;
            Node next;
            Node prev;
            
            public Node(K key, T data){
                this.key = key;
                this.data = data;
            }
        }
        
        Node head,tail;
        Map< K, Node> map;
        int maxsize;
        
        public CacheStrategyLRU(int mxsize){
            this.maxsize = mxsize;
            map = new HashMap<K ,Node>();
            head =  new Node(null,null);
            tail = new Node(null,null);
            head.next=tail;
            tail.prev=head;
        }
        
        private void  attach(Node head,Node node){
            node.prev = head;
            node.next = head.next;
            head.next.prev=node;
            head.next = node;
        }
        
        private void remove(Node node){
            node.prev.next = node.next;
            node.next.prev = node.prev;
        }
        
        
        @Override
        public T get(K key) {
            Node node = map.get(key);
            if(node==null){
                return null;
            }
            
            if(map.size()==1){
                return node.data;
            }
            remove(node);
            attach(head,node);
            return node.data;
        }

        @Override
        public void put(K key, T data) {
            if(maxsize<=0){
                return;
            }
            Node node = map.get(key);
            if(node!=null){
                remove(node);
                attach(head,node);
                node.data = data;
            }else{
                if(map.size() >= maxsize){
                    remove(tail.prev);//tail is node pointer ,its not containg any node so delete tail.prev
                    map.remove(tail.prev.key);
                }
                Node nd = new Node(key,data);
                map.put(key, nd);
                attach(head,nd);
            }
            
        }
        
    }  
    
}

 

转载于:https://www.cnblogs.com/leetcode/p/3552067.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值