LRU全称Least Recently Used,也就是最近最少使用的意思,是一种内存管理算法,该算法最早应用于Linux操作系统。
    这个算法基于一种假设:长期不被使用的数据,在未来被用到的几率也不大。因此,当数据所占内存达到一定阈值时,我们要移除掉最近最少被使用的数据。
    可以使用哈希链表来实现LRU算法。


    我们需要抽出一个用户系统,向各个业务系统提供用户的基本信息。


    以用户信息的需求为例,来演示一下LRU算法的基本思路。

    1. 假设使用哈希链表来缓存用户信息,目前缓存了4个用户,这4个用户是按照被访问的时间顺序依次从链表右端插入的。

    image.png

    1. 如果这时业务方访问用户5,由于哈希链表中没有用户5的数据,需要从数据库中读取出来,插入到缓存中。此时,链表最右端是最新被访问的用户5,最左端是最近最少被访问的用户1。

    image.png

    1. 接下来,如果业务方访问用户2,哈希链表中已经存在用户2的数据,这时我们把用户2从它的前驱节点和后继节点之间移除,重新插入链表的最右端。此时,链表的最右端变成了最新被访问的用户2,最左端仍然是最近最少被访问的用户1。

    image.png

    1. 接下来,如果业务方请求修改用户4的信息。同样的道理,我们会把用户4从原来的位置移动到链表的最右侧,并把用户信息的值更新。这时,链表的最右端是最新被访问的用户4,最左端仍然是最近最少被访问的用户1。

    image.png

    1. 后来业务方又要访问用户6,用户6在缓存里没有,需要插入哈希链表中。假设这时缓存容量已经达到上限,必须先删除最近最少被访问的数据,那么位于哈希链表最左端的用户1就会被删除,然后再把用户6插入最右端的位置。

    image.png
    以上,就是LRU算法的基本思路。


    1. class Node {
    2. public Node pre;
    3. public Node next;
    4. public String key;
    5. public String value;
    6. public Node(String key, String value) {
    7. this.key = key;
    8. this.value = value;
    9. }
    10. }
    11. public class LRUCache {
    12. private Node head;
    13. private Node end;
    14. // 缓存存储上限
    15. private int limit;
    16. private HashMap<String, Node> hashMap;
    17. // 删除节点
    18. private String removeNode(Node node) {
    19. if (node == head && node == end) {
    20. // 移除唯一的节点
    21. head = null;
    22. end = null;
    23. } else if (node == end) {
    24. // 移除尾结点
    25. end = end.pre;
    26. end.next = null;
    27. } else if (node == head) {
    28. // 移除头节点
    29. head = head.next;
    30. head.pre = null;
    31. } else {
    32. // 移除中间节点
    33. node.pre.next = node.next;
    34. node.next.pre = node.pre;
    35. }
    36. return node.key;
    37. }
    38. // 尾部插入节点
    39. private void addNode(Node node) {
    40. if (end != null) {
    41. end.next = node;
    42. node.pre = end;
    43. node.next = null;
    44. }
    45. end = node;
    46. if (head == null) {
    47. head = node;
    48. }
    49. }
    50. // 刷新被访问节点位置
    51. private void refreshNode(Node node) {
    52. // 如果访问的是尾节点,则无需移动节点
    53. if (node == end) return;
    54. // 移除节点
    55. removeNode(node);
    56. // 重新插入节点
    57. addNode(node);
    58. }
    59. public LRUCache(int limit) {
    60. this.limit = limit;
    61. hashMap = new HashMap<String, Node>();
    62. }
    63. public String get(String key) {
    64. Node node = hashMap.get(key);
    65. if (node == null) return null;
    66. refreshNode(node);
    67. return node.value;
    68. }
    69. public void put(String key, String value) {
    70. Node node = hashMap.get(key);
    71. if (node == null) {
    72. // 如果 key 不存在,则插入 key-value
    73. if (hashMap.size() >= limit) {
    74. String oldKey = removeNode(head);
    75. hashMap.remove(oldKey);
    76. }
    77. node = new Node(key, value);
    78. addNode(node);
    79. hashMap.put(key, node);
    80. } else {
    81. // 如果 key 存在,则刷新 key-value
    82. node.value = value;
    83. refreshNode(node);
    84. }
    85. }
    86. public void remove(String key) {
    87. Node node = hashMap.get(key);
    88. removeNode(node);
    89. hashMap.remove(key);
    90. }
    91. public static void main(String[] args) {
    92. LRUCache lruCache = new LRUCache(5);
    93. lruCache.put("001", "用户1");
    94. lruCache.put("002", "用户2");
    95. lruCache.put("003", "用户3");
    96. lruCache.put("004", "用户4");
    97. lruCache.put("005", "用户5");
    98. lruCache.get("002");
    99. lruCache.put("004", "用户44");
    100. lruCache.put("006", "用户6");
    101. System.out.println(lruCache.get("001"));
    102. System.out.println(lruCache.get("006"));
    103. }
    104. }

    需要注意的是,这段代码不是线程安全的代码,要想做到线程安全,需要加上synchronized修饰符。
    对于用户系统的需求,你也可以使用缓存数据库Redis来实现,Redis底层也实现了类似LRU的回收算法。