集册 Java实例教程 通用LRU缓存

通用LRU缓存

欢马劈雪     最近更新时间:2020-01-02 10:19:05

549
下面的LRU缓存是包含项的循环arrayList。

import java.util.HashMap;/** nowjava.com - 时  代  Java 提 供 **/


class LRU<K, V> {


  private transient Entry<K, V> header = new Entry<K, V>(null, null, null, null);

  public HashMap<K, IndexNode<Entry<K, V>>> indexMap = new HashMap<K, IndexNode<Entry<K, V>>>();

  private final int CACHE_LIMIT = 3;

  private int size;


  public LRU() {

    header.next = header.previous = header;

    this.size = 0;

  }

  /*
   from n o w j a v a . c o m - 时代Java 
  */

  public void put(K key, V value) {

    Entry<K, V> newEntry = new Entry<K, V>(key, value, null, null);

    addBefore(newEntry, header);

  }


  private void addBefore(Entry<K, V> newEntry, Entry<K, V> entry) {

    if ((size + 1) < (CACHE_LIMIT + 1)) {

      newEntry.next = entry;

      newEntry.previous = entry.previous;

      IndexNode<Entry<K, V>> indexNode = new IndexNode<Entry<K, V>>(newEntry);

      indexMap.put(newEntry.key, indexNode);

      newEntry.previous.next = newEntry;

      newEntry.next.previous = newEntry;

      size++;

    } else {

      Entry<K, V> entryRemoved = remove(header.next);

      indexMap.remove(entryRemoved.key);

      addBefore(newEntry, entry);

    }

  }


  public void get(K key) {

    if (indexMap.containsKey(key)) {

      Entry<K, V> newEntry = remove(indexMap.get(key).pointer);

      addBefore(newEntry, header);

    } else {

      System.out.println("No such element was cached. Go and get it from Disk");

    }

  }


  private Entry<K, V> remove(Entry<K, V> entry) {

    entry.previous.next = entry.next;

    entry.next.previous = entry.previous;

    size--;

    return entry;

  }


  public void display() {

    for (Entry<K, V> curr = header.next; curr != header; curr = curr.next) {

      System.out.println("key : " + curr.key + " value : " + curr.value);

    }

  }


  private static class IndexNode<Entry> {

    private Entry pointer;


    public IndexNode(Entry pointer) {

      this.pointer = pointer;

    }

  }


  private static class Entry<K, V> {

    K key;

    V value;

    Entry<K, V> previous;

    Entry<K, V> next;


    Entry(K key, V value, Entry<K, V> next, Entry<K, V> previous) {

      this.key = key;

      this.value = value;

      this.next = next;

      this.previous = previous;

    }

  }


}


public class Main {

  public static void main(String[]
展开阅读全文