当前位置:Gxlcms > PHP教程 > 使用PHP实现LRU缓存淘汰算法

使用PHP实现LRU缓存淘汰算法

时间:2021-07-01 10:21:17 帮助过:15人阅读

LRU(cache)

LRU 介绍

缓存是一种提高数据读取性能的技术。但是对于计算机来说,并不可能缓存所有的数据,在达到它的临界空间时,我们需要通过一些规则用新的数据取代掉一部分的缓存数据。这时候你会如果选择替换呢?

替换的策略有很多种,常用的有以下几种:

● FIFO (先进先出策略)

● LFU (最少使用策略)

● LRU (最近最少使用策略)

● NMRU (在最近没有使用的缓存中随机选择一个替换)

介于我这篇主要实现 LRU,所以就不去介绍其他的了,可以自行去了解。

假设你已经有 5 个女朋友了,此时你成功勾搭上一个新女朋友,在你沉迷女色的同时,你惊奇的发现,你已经不能像年轻时一样以一敌六了,你必须舍弃若干个女朋友,这时候,身拥六个女朋友的渣男你,彻底展示出你的渣男本色,和最近最少秀恩爱的小姐姐说再见:“对不起,国篮此时需要我挺身发边线球,我楠辞琦咎,再见。”,就这样在你成功勾搭一个新小姐姐,你的身体临界点的同时,你就必须舍弃其他的小姐姐。

下面来张实际点的图搞清楚他的原理。

9ace0279c955cd4b1f070ade5df7ffe.png

基于上述图片,我们知道,对于 LRU 的操作,无非在于插入 (insert), 删除 (delete),以及替换,针对替换来说,如果缓存空间满了,那么就是 insert to head and delete for tail。如果未满,也分为两种,一种是缓存命中的话,只需要把缓存的值 move to head。如果之前不存在,那么就是 insert to head。

实现过程

接下来就是数据结构的选择了。数组的存储是连续的内存空间,虽然查询的时间复杂度是 O (1), 但是删除和插入为了保存内存空间的连续性,需要进行搬移,那么时间复杂度就是 O (n), 为了实现能快速删除,故而采用双向链表。但是链表的查询时间复杂度是 O (n), 那么就需要 hash table。屁话说了这么多,代码实现。其实之前刷过这道题目。特地拿出来讲一下。

  1. class LRUCache {
  2. private $capacity;
  3. private $list;
  4. /**
  5. * @param Integer $capacity
  6. */
  7. function __construct($capacity) {
  8. $this->capacity=$capacity;
  9. $this->list=new HashList();
  10. }
  11. /**
  12. * @param Integer $key
  13. * @return Integer
  14. */
  15. function get($key) {
  16. if($key<0) return -1;
  17. return $this->list->get($key);
  18. }
  19. /**
  20. * @param Integer $key
  21. * @param Integer $value
  22. * @return NULL
  23. */
  24. function put($key, $value) {
  25. $size=$this->list->size;
  26. $isHas=$this->list->checkIndex($key);
  27. if($isHas || $size+1 > $this->capacity){
  28. $this->list->removeNode($key);
  29. }
  30. $this->list->addAsHead($key,$value);
  31. }
  32. }
  33. class HashList{
  34. public $head;
  35. public $tail;
  36. public $size;
  37. public $buckets=[];
  38. public function __construct(Node $head=null,Node $tail=null){
  39. $this->head=$head;
  40. $this->tail=$tail;
  41. $this->size=0;
  42. }
  43. //检查键是否存在
  44. public function checkIndex($key){
  45. $res=$this->buckets[$key];
  46. if($res){
  47. return true;
  48. }
  49. return false;
  50. }
  51. public function get($key){
  52. $res=$this->buckets[$key];
  53. if(!$res) return -1;
  54. $this->moveToHead($res);
  55. return $res->val;
  56. }
  57. //新加入的节点
  58. public function addAsHead($key,$val)
  59. {
  60. $node=new Node($val);
  61. if($this->tail==null && $this->head !=null){
  62. $this->tail=$this->head;
  63. $this->tail->next=null;
  64. $this->tail->pre=$node;
  65. }
  66. $node->pre=null;
  67. $node->next=$this->head;
  68. $this->head->pre=$node;
  69. $this->head=$node;
  70. $node->key=$key;
  71. $this->buckets[$key]=$node;
  72. $this->size++;
  73. }
  74. //移除指针(已存在的键值对或者删除最近最少使用原则)
  75. public function removeNode($key)
  76. {
  77. $current=$this->head;
  78. for($i=1;$i<$this->size;$i++){
  79. if($current->key==$key) break;
  80. $current=$current->next;
  81. }
  82. unset($this->buckets[$current->key]);
  83. //调整指针
  84. if($current->pre==null){
  85. $current->next->pre=null;
  86. $this->head=$current->next;
  87. }else if($current->next ==null){
  88. $current->pre->next=null;
  89. $current=$current->pre;
  90. $this->tail=$current;
  91. }else{
  92. $current->pre->next=$current->next;
  93. $current->next->pre=$current->pre;
  94. $current=null;
  95. }
  96. $this->size--;
  97. }
  98. //把对应的节点应到链表头部(最近get或者刚刚put进去的node节点)
  99. public function moveToHead(Node $node)
  100. {
  101. if($node==$this->head) return ;
  102. //调整前后指针指向
  103. $node->pre->next=$node->next;
  104. $node->next->pre=$node->pre;
  105. $node->next=$this->head;
  106. $this->head->pre=$node;
  107. $this->head=$node;
  108. $node->pre=null;
  109. }
  110. }
  111. class Node{
  112. public $key;
  113. public $val;
  114. public $next;
  115. public $pre;
  116. public function __construct($val)
  117. {
  118. $this->val=$val;
  119. }
  120. }
  121. /**
  122. * Your LRUCache object will be instantiated and called as such:
  123. * $obj = LRUCache($capacity);
  124. * $ret_1 = $obj->get($key);
  125. * $obj->put($key, $value);

Github 整理地址:https://github.com/wuqinqiang/leetcode-php

更多PHP知识,请访问PHP中文网!

以上就是使用 PHP 实现 LRU 缓存淘汰算法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行