本文隶属于分类

编程语言

广告推荐

技术交流学习或者有任何问题欢迎加群

编程技术交流群 : 154514123 爱上编程      Java技术交流群 : 6128790  Java

标签:shm   put   sea   cep   try   打印   mage   自定义   ast   

1. java提供了一个简单的方式实现LRU:  LinkedHashMap

技术分享图片
 

 2. 自定义实现

LRU至少需要两个主要操作: 添加(add)和搜索(search)

public class LRUCache {
    private Map<String, CacheEntry> map;
    private CacheEntry head , tail;
    private int maxSize;
    public LRUCache(int maxSize){
        if(maxSize < 1){
            throw new IllegalArgumentException("Cache maxSize has to be at least 1");
        }
        this.map = new HashMap<String, CacheEntry>();
        head = new CacheEntry("head","");
        tail = new CacheEntry("tail", "");
        head.setNext(tail);
        tail.setPrev(head);
        this.maxSize = maxSize;
    }
    public void add(String key, String value){
        CacheEntry cacheEntry = map.get(key);
        if(cacheEntry == null){
            cacheEntry = new CacheEntry(key, value);
            if(map.size() == maxSize){
                //缓存达到最大数目
                //删除最老的条目
                CacheEntry deleteEntry = tail.getPrev();
                //从map中移除
                map.remove(deleteEntry.getKey());
                //从队列中移除
                remove(deleteEntry);
            }
            //添加条目到队列
            addFront(cacheEntry);
            //添加到map
            map.put(key, cacheEntry);
        }else{
            //更新值
            cacheEntry.setValue(value);
            //访问条目
            accessed(cacheEntry);
        }
    }

    public String search(String key){
        CacheEntry entry = map.get(key);
        if(entry == null){
            return null;
        }
        accessed(entry);
        return entry.getValue();
    }
    //打印缓存内容
    public void print(){
        CacheEntry entry = head.getNext();
        while(entry != tail){
            System.out.println("{" + entry.getKey() + ":" + entry.getValue() + "}");
            entry = entry.getNext();
        }
        System.out.println();
    }

    public void accessed(CacheEntry entry) {
        if(entry.getPrev() != head){
            remove(entry);
            addFront(entry);
        }
    }

    private void addFront(CacheEntry entry) {
        //在队列的头部添加条目
        CacheEntry nextEntry = head.getNext();
        head.setNext(entry);
        entry.setPrev(head);
        entry.setNext(nextEntry);
        if(nextEntry != null){
            nextEntry.setPrev(entry);
        }
    }

    private void remove(CacheEntry entry) {
        if(entry == head || entry == tail){
            return;//错误
        }
        entry.getPrev().setNext(entry.getNext());
        if(entry.getNext() != null){
            entry.getNext().setPrev(entry.getPrev());
        }
    }
}
/**
 * Created by admin on 2018/3/30.
 */
public class CacheEntry {
    private CacheEntry prev, next;
    private String key, value;
    CacheEntry(String key, String value){
        this.key = key;
        this.value = value;
    }

    public CacheEntry getPrev() {
        return prev;
    }

    public void setPrev(CacheEntry prev) {
        this.prev = prev;
    }

    public CacheEntry getNext() {
        return next;
    }

    public void setNext(CacheEntry next) {
        this.next = next;
    }

    public String getKey() {
        return key;
    }


    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

 

java 自定义 LRU(最近最少使用)策略 实现 缓存机制

标签:shm   put   sea   cep   try   打印   mage   自定义   ast   

原文:https://www.cnblogs.com/rocky-fang/p/8676318.html

技术交流学习或者有任何问题欢迎加群

编程技术交流群 : 154514123 爱上编程      Java技术交流群 : 6128790  Java

广告推荐

讨论区