码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-03-30 16:16:40      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:nbsp   oid   max   string   bsp   front   arch   throw   while   

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(最近最少使用)策略 实现 缓存机制

标签:nbsp   oid   max   string   bsp   front   arch   throw   while   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!