LRU cache实现 (Java)

来源:互联网 发布:程序员面试宝典 微盘 编辑:程序博客网 时间:2024/05/17 09:36

引子:

我们平时总会有一个电话本记录所有朋友的电话,但是,如果有朋友经常联系,那些朋友的电话号码不用翻电话本我们也能记住,但是,如果长时间没有联系了,要再次联系那位朋友的时候,我们又不得不求助电话本,但是,通过电话本查找还是很费时间的。但是,我们大脑能够记住的东西是一定的,我们只能记住自己最熟悉的,而长时间不熟悉的自然就忘记了。

其实,计算机也用到了同样的一个概念,我们用缓存来存放以前读取的数据,而不是直接丢掉,这样,再次读取的时候,可以直接在缓存里面取,而不用再重新查找一遍,这样系统的反应能力会有很大提高。但是,当我们读取的个数特别大的时候,我们不可能把所有已经读取的数据都放在缓存里,毕竟内存大小是一定的,我们一般把最近常读取的放在缓存里(相当于我们把最近联系的朋友的姓名和电话放在大脑里一样)。现在,我们就来研究这样一种缓存机制。

LRU Cache:

LRU缓存利用了这样的一种思想。LRU是Least Recently Used 的缩写,翻译过来就是“最不常使用”,也就是说,LRU缓存把最不常使用的数据移除,让给最新读取的数据。大多数情况下,最常读取的,也是读取次数最多的,所以,利用LRU缓存,我们能够提高系统的performance. LRU cache是非常频繁出现的一道面试题,一般来讲,当我们问到这道题时,面试官往往想得到的答案是利用 doubly linked list + hashtable 实现 LRU Cache, 那么我们现在就来讲一讲如何利用doubly linked list + hashtable实现LRU Cache的。

对于LRU cache,往往会有以下要求:

1. 假设Cache里面的 entry 都是按照序列保存的,那么,对于新的entry,我们把它放置在最前面。

2. 如果一个entry已经存在,我们再次访问到该entry的时候,我们需要把它放在cache的最前面。

3. 当cache满了的时候,需要把最后一个entry 从cache里面移除出去,然后再往里插入 entry。

4. 以上所有的操作复杂度必须为 O(1).

对于操作复杂度,一旦看到要求为O(1), 一般我们都会立刻想到 hashtable, 所以,为了实现“顺序”的要求,我们需要有一个链表来连接所有的entry. 所以,在实现时,我们将Cache的所有 entry 都用doubly linked list 连接起来,当一个 entry 被命中之后,就将通过调整链表的指向,将该位置调整到链表头的位置,新加入的Cache直接加到链表头中。这样,在多次进行Cache操作后,最近被命中的,就会被向链表头方向移动,而没有命中的,而想链表后面移动,链表尾则表示最近最少使用的Cache。当需要替换内容时候,链表的最后位置就是最少被命中的位置,我们只需要淘汰链表最后的部分即可。

我们首先定义entry, 每一个entry包括键(key)和 值 (value),而且,每一个 entry 都带有两个指针分别指向它们的前一个和后一个 entry.

[java] view plaincopy
  1. class Entry {  
  2.     Entry prev;//前一节点  
  3.     Entry next;//后一节点  
  4.     Object value;//值  
  5.     Object key;//键  
  6. }  

在hashtable里,我们需要保存该entry, 这个时候,我们用entry的键作为hashtable 里的键,而hashtable的值呢就是entry。

[java] view plaincopy
  1. public class LRUCache {  
  2.       
  3.     private int cacheSize;  
  4.     private Hashtable<Object, Entry> nodes;//缓存容器  
  5.     private int currentSize;  
  6.     private Entry first;//链表头  
  7.     private Entry last;//链表尾  
  8.       
  9.     public LRUCache(int i) {  
  10.         currentSize = 0;  
  11.         cacheSize = i;  
  12.         nodes = new Hashtable<Object, Entry>(i);//缓存容器  
  13.     }  
  14.       
  15.     /** 
  16.      * 获取缓存中对象,并把它放在最前面 
  17.      */  
  18.     public Entry get(Object key) {  
  19.         Entry node = nodes.get(key);  
  20.         if (node != null) {  
  21.             moveToHead(node);  
  22.             return node;  
  23.         } else {  
  24.             return null;  
  25.         }  
  26.     }  
  27.       
  28.     /** 
  29.      * 添加 entry到hashtable, 并把entry  
  30.      */  
  31.     public void put(Object key, Object value) {  
  32.         //先查看hashtable是否存在该entry, 如果存在,则只更新其value  
  33.         Entry node = nodes.get(key);  
  34.           
  35.         if (node == null) {  
  36.             //缓存容器是否已经超过大小.  
  37.             if (currentSize >= cacheSize) {  
  38.                 nodes.remove(last.key);  
  39.                 removeLast();  
  40.             } else {  
  41.                 currentSize++;  
  42.             }             
  43.             node = new Entry();  
  44.         }  
  45.         node.value = value;  
  46.         //将最新使用的节点放到链表头,表示最新使用的.  
  47.         moveToHead(node);  
  48.         nodes.put(key, node);  
  49.     }  
  50.   
  51.     /** 
  52.      * 将entry删除, 注意:删除操作只有在cache满了才会被执行 
  53.      */  
  54.     public void remove(Object key) {  
  55.         Entry node = nodes.get(key);  
  56.         //在链表中删除  
  57.         if (node != null) {  
  58.             if (node.prev != null) {  
  59.                 node.prev.next = node.next;  
  60.             }  
  61.             if (node.next != null) {  
  62.                 node.next.prev = node.prev;  
  63.             }  
  64.             if (last == node)  
  65.                 last = node.prev;  
  66.             if (first == node)  
  67.                 first = node.next;  
  68.         }  
  69.         //在hashtable中删除  
  70.         nodes.remove(key);  
  71.     }  
  72.   
  73.     /** 
  74.      * 删除链表尾部节点,即使用最后 使用的entry 
  75.      */  
  76.     private void removeLast() {  
  77.         //链表尾不为空,则将链表尾指向null. 删除连表尾(删除最少使用的缓存对象)  
  78.         if (last != null) {  
  79.             if (last.prev != null)  
  80.                 last.prev.next = null;  
  81.             else  
  82.                 first = null;  
  83.             last = last.prev;  
  84.         }  
  85.     }  
  86.       
  87.     /** 
  88.      * 移动到链表头,表示这个节点是最新使用过的 
  89.      */  
  90.     private void moveToHead(Entry node) {  
  91.         if (node == first)  
  92.             return;  
  93.         if (node.prev != null)  
  94.             node.prev.next = node.next;  
  95.         if (node.next != null)  
  96.             node.next.prev = node.prev;  
  97.         if (last == node)  
  98.             last = node.prev;  
  99.         if (first != null) {  
  100.             node.next = first;  
  101.             first.prev = node;  
  102.         }  
  103.         first = node;  
  104.         node.prev = null;  
  105.         if (last == null)  
  106.             last = first;  
  107.     }  
  108.     /* 
  109.      * 清空缓存 
  110.      */  
  111.     public void clear() {  
  112.         first = null;  
  113.         last = null;  
  114.         currentSize = 0;  
  115.     }  
  116.   
  117. }  

转自:http://blog.csdn.net/beiyetengqing


参考:http://gogole.iteye.com/blog/692103

http://blog.csdn.net/beiyeqingteng

0 0
原创粉丝点击