最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
实现Redis的LRU缓存机制代码示例解析
时间:2022-06-29 10:43:35 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下实现Redis的LRU缓存机制代码示例解析,文章介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
第一种实现(使用LinkedHashMap)
public class LRUCache {
  int capacity;
  Map map;
  public LRUCache(int capacity){
    this.capacity = capacity;
    map = new LinkedHashMap<>();
  }
  public int get(int key){
    //如果没有找到
    if (!map.containsKey(key)){
      return -1;
    }
    //找到了就刷新数据
    Integer value = map.remove(key);
    map.put(key,value);
    return value;
  }
  public void put(int key,int value){
    if (map.containsKey(key)){
      map.remove(key);
      map.put(key,value);
      return;
    }
    map.put(key,value);
    //超出capacity,删除最久没用的即第一个,或者可以复写removeEldestEntry方法
    if (map.size() > capacity){
      map.remove(map.entrySet().iterator().next().getKey());
    }
  }
  public static void main(String[] args) {
    LRUCache lruCache = new LRUCache(10);
    for (int i = 0; i < 10; i++) {
      lruCache.map.put(i,i);
      System.out.println(lruCache.map.size());
    }
    System.out.println(lruCache.map);
    lruCache.put(10,200);
    System.out.println(lruCache.map);
  } 
第二种实现(双链表+hashmap)
public class LRUCache {
  private int capacity;
  private Mapmap;
  private ListNode head;
  private ListNode tail;
  public LRUCache2(int capacity){
    this.capacity = capacity;
    map = new HashMap<>();
    head = new ListNode(-1,-1);
    tail = new ListNode(-1,-1);
    head.next = tail;
    tail.pre = head;
  }
  public int get(int key){
    if (!map.containsKey(key)){
      return -1;
    }
    ListNode node = map.get(key);
    node.pre.next = node.next;
    node.next.pre = node.pre;
    return node.val;
  }
  public void put(int key,int value){
    if (get(key)!=-1){
      map.get(key).val = value;
      return;
    }
    ListNode node = new ListNode(key,value);
    map.put(key,node);
    moveToTail(node);
    if (map.size() > capacity){
      map.remove(head.next.key);
      head.next = head.next.next;
      head.next.pre = head;
    }
  }
  //把节点移动到尾巴
  private void moveToTail(ListNode node) {
    node.pre = tail.pre;
    tail.pre = node;
    node.pre.next = node;
    node.next = tail;
  }
  //定义双向链表节点
  private class ListNode{
    int key;
    int val;
    ListNode pre;
    ListNode next;
    //初始化双向链表
    public ListNode(int key,int val){
      this.key = key;
      this.val = val;
      pre = null;
      next = null;
    }
  }
} 
像第一种方式,如果复写removeEldestEntry会更简单,这里简单的展示一下
public class LRUCache extends LinkedHashMap{ private int capacity; @Override protected boolean removeEldestEntry(Map.Entry eldest) { return size() > capacity; } } 
相关文章
- 暗喻幻想召唤士阿基态种类及解锁方法 10-31
- 暗喻幻想王家召唤师解锁方法攻略分享 10-31
- 迷失混沌王国永恒之骰兑换码分享 永恒之骰最新兑换码2025 10-31
- 钻核公司兑换码是什么 钻核公司Drill Core最新2025兑换码一览 10-31
- 死域Rogue兑换码分享 死域Rogue最新2025兑换码大全 10-31
- 梦幻西游手游怎么跨服拜师-跨服拜师方法 10-31
 
             
                                 
                                 
                                 
                                 
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                         
                                            
                                        