LRU最少最近使用缓存策略

LRU(Least Recently Used)直译为“最近最少使用”

缓存的容量是有限的
当缓存容量不足以存放需要缓存的新数据时,必须丢掉最不常用的缓存数据

使用hashmap存储数据,双向链表对数据排序,和LinkedHashMap相似

写入缓存,或查询缓存时,把数据移到头结点

package com.study.demo;

import java.util.HashMap;

/**
 * Created by Administrator on 2019\5\4 0004.
 */
public class LRUCache<K, V> {

    private int currentCacheSize;
    private int CacheCapcity;
    private HashMap<K,CacheNode> caches;
    private CacheNode first;
    private CacheNode last;

    public LRUCache(int size){
        currentCacheSize = 0;
        this.CacheCapcity = size;
        caches = new HashMap<K,CacheNode>(size);
    }

    public void put(K k,V v){
        CacheNode node = caches.get(k);
        if(node == null){
            if(caches.size() >= CacheCapcity){

                caches.remove(last.key);
                removeLast();
            }
            node = new CacheNode();
            node.key = k;
        }
        node.value = v;
        moveToFirst(node);
        caches.put(k, node);
    }

    public Object  get(K k){
        CacheNode node = caches.get(k);
        if(node == null){
            return null;
        }
        moveToFirst(node);
        return node.value;
    }

    public Object remove(K k){
        CacheNode node = caches.get(k);
        if(node != null){
            if(node.pre != null){
                node.pre.next=node.next;
            }
            if(node.next != null){
                node.next.pre=node.pre;
            }
            if(node == first){
                first = node.next;
            }
            if(node == last){
                last = node.pre;
            }
        }

        return caches.remove(k);
    }

    public void clear(){
        first = null;
        last = null;
        caches.clear();
    }



    private void moveToFirst(CacheNode node){
        if(first == node){
            return;
        }
        if(node.next != null){
            node.next.pre = node.pre;
        }
        if(node.pre != null){
            node.pre.next = node.next;
        }
        if(node == last){
            last= last.pre;
        }
        if(first == null || last == null){
            first = last = node;
            return;
        }

        node.next=first;
        first.pre = node;
        first = node;
        first.pre=null;

    }

    private void removeLast(){
        if(last != null){
            last = last.pre;
            if(last == null){
                first = null;
            }else{
                last.next = null;
            }
        }
    }
    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        CacheNode node = first;
        while(node != null){
            sb.append(String.format("%s:%s ", node.key,node.value));
            node = node.next;
        }

        return sb.toString();
    }

    class CacheNode{
        CacheNode pre;
        CacheNode next;
        Object key;
        Object value;
        public CacheNode(){

        }
    }

    public static void main(String[] args) {

        LRUCache<Integer,String> lru = new LRUCache<Integer,String>(3);

        lru.put(1, "a");    // 1:a
        System.out.println(lru.toString());
        
        lru.put(2, "b");    // 2:b 1:a
        System.out.println(lru.toString());
        
        lru.put(3, "c");    // 3:c 2:b 1:a
        System.out.println(lru.toString());
        
        lru.put(4, "d");    // 4:d 3:c 2:b
        System.out.println(lru.toString());
        
        lru.put(1, "aa");   // 1:aa 4:d 3:c
        System.out.println(lru.toString());
        
        lru.put(4, "dd");   // 4:dd 1:aa 3:c 
        System.out.println(lru.toString());

        System.out.println(lru.get(1));//aa
        System.out.println(lru.toString());//1:aa 4:dd 3:c

    }

}

猜你喜欢

转载自blog.csdn.net/mingwulipo/article/details/89813256
今日推荐