Redisを手動で実現するLRUキャッシュメカニズムの例を詳しく説明します。


前言
最近はブログを見ていますが、Redisに関する面接問題を見ました。その中でRedisはメモリが最大限度に達した時にLRUなどの淘汰メカニズムを使っています。LRU全体は大体このようにします。最近使っているのは前に置いて、最近使っていないのは後ろに置いて、新しい数が来たら、メモリがいっぱいになります。古い数を淘汰する必要があります。データを移動しやすくするためには、必ずチェーン式のデータ構造を使います。加えて、このデータが最新または最新のものかどうかを判断するためには、hashmapなどのkey-value形式のデータ構造も使用されるべきである。
第一の実現(Linked HashMapを使う)

public class LRUCache {

  int capacity;
  Map<Integer,Integer> 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 Map<Integer,ListNode>map;
  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;
    }
  }
}
最初のようにremoveEldest Entryを復唱すればもっと簡単になります。ここで簡単に展示してください。

public class LRUCache extends LinkedHashMap<Integer,Integer> {
  private int capacity;
  
  @Override
  protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
    return size() > capacity;
  }
}
ここでは、手動でRedisを実現するLRUキャッシュメカニズムについての記事を紹介します。これに関連して、RedisのLRUキャッシュメカニズムの内容は以前の文章を検索したり、下記の関連記事を引き続き閲覧したりしてください。これからもよろしくお願いします。