当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈Java如何实现一个基于LRU时间复杂度为O(1)的缓存

浅谈Java如何实现一个基于LRU时间复杂度为O(1)的缓存

2020年08月04日  | 移动技术网IT编程  | 我要评论

lru:least recently used最近最少使用,当缓存容量不足时,先淘汰最近最少使用的数据。就像jvm垃圾回收一样,希望将存活的对象移动到内存的一端,然后清除其余空间。

缓存基本操作就是读、写、淘汰删除。

读操作时间复杂度为o(1)的那就是hash操作了,可以使用hashmap索引 key。

写操作时间复杂度为o(1),使用链表结构,在链表的一端插入节点,是可以完成o(1)操作,但是为了配合读,还要再次将节点放入hashmap中,put操作最优是o(1),最差是o(n)。

不少童鞋就有疑问了,写入时又使用map进行了put操作,为何缓存不直接使用map?没错,首先使用map存储了节点数据就是采用空间换时间,但是淘汰删除不好处理,使用map如何去记录最近最少使用(涉及到时间、频次问题)。so,使用链表可以将活跃节点移动到链表的一端,淘汰时直接从另一端进行删除。

public class lrucache<k,v> {
	/** 这里简单点直接初始化了*/
  private int capacity = 2;
  private int size = 0;
  private hashmap<k,doublelistnode<k,v>> cache = new hashmap<>(capacity);
  private doublelistnode<k,v> lrunode = new doublelistnode<k, v>(null,null,null,null);
  private doublelistnode<k,v> mrunode = new doublelistnode<k, v>(null,null,null,null);

  public v get(k key){
    doublelistnode<k,v> target = cache.get(key);
    if (target == null) {
      return null;
    }
    /** 使用过就移动到右侧 */
    move2mru(target);
    return target.value;
  }

  public void put(k key,v value){
    if(cache.containskey(key)){
      doublelistnode<k,v> temp = cache.get(key);
      temp.value = value;
      /** 使用过就移动到右侧 */
      move2mru(temp);
      return;
    }

		/** 容量满了清除左侧 */
    if(size >= capacity){
      evict4lru();
    }
    doublelistnode<k,v> newnode = new doublelistnode<>(mrunode,null,key,value);
    if(size == 0){
      lrunode.next = newnode;
    }
    mrunode.next = newnode;
    mrunode = newnode;
    cache.put(key,newnode);
    size++;
  }

  private void move2mru(doublelistnode<k,v> newmru){
    doublelistnode<k,v> pre = newmru.pre;
    doublelistnode<k,v> next = newmru.next;
    pre.next = next;
    newmru.pre = mrunode;
    mrunode.next = newmru;
    mrunode = newmru;
  }

  private void evict4lru(){
  	cache.remove(lrunode.next.key);
    lrunode.next = lrunode.next.next;
    size--;
  }

  public string tostring(){
    stringbuffer sb = new stringbuffer("lru -> ");
    doublelistnode<k,v> temp = lrunode;
    while(temp!=null){
      sb.append(temp.key).append(":").append(temp.value);
      sb.append(" -> ");
      temp = temp.next;
    }
    sb.append(" -> mru ");
    return sb.tostring();
  }

  public static void main(string[] args) {
    lrucache<string,string> cache = new lrucache<>();
    cache.put("1","1");
    system.out.println(cache);
    cache.get("1");
    cache.put("2","2");
    system.out.println(cache);
    cache.put("3","3");
    system.out.println(cache);
    cache.put("4","4");
    system.out.println(cache);
  }
}

class doublelistnode<k,v>{
  k key;
  v value;
  doublelistnode<k,v> pre;
  doublelistnode<k,v> next;

  public doublelistnode(k key,v value){
    this.key = key;
    this.value = value;
  }

  public doublelistnode(doublelistnode<k,v> pre,doublelistnode<k,v> next,k key,v value){
    this.pre = pre;
    this.next = next;
    this.key = key;
    this.value = value;
  }
}

这里使用链表,及hashmap完成了基于lru的缓存,其中hashmap主要用来快速索引key,链表用来完成lru机制。当然尚有许多不足,包括缓存移除remove,缓存ttl,线程安全等。

到此这篇关于浅谈java如何实现一个基于lru时间复杂度为o(1)的缓存的文章就介绍到这了,更多相关java基于lru时间复杂度为o(1)的缓存内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网