站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        半天擼完一個(gè)PHP 實(shí)現(xiàn)LRU 算法的知識(shí)

        我們學(xué)習(xí)了解了這么多關(guān)于PHP的知識(shí),今天學(xué)習(xí)如何半天擼完一個(gè)PHP 實(shí)現(xiàn)LRU 算法的知識(shí),不知你們是否已經(jīng)完全掌握了呢,如果沒有,那就跟隨本篇文章一起繼續(xù)學(xué)習(xí)吧

        整體設(shè)計(jì)

        1:用數(shù)組保存緩存對象(Node);

        2:緩存對象(Node)之間通過nextKey,preKey組成一個(gè)雙向鏈表;

        3:保存鏈表頭 跟尾;

        處理流程如下圖:

        半天擼完一個(gè)PHP 實(shí)現(xiàn)LRU 算法的知識(shí)

        主要代碼

        1:Node 節(jié)點(diǎn)類

        /**  * 緩存值保存類,  * Class Node  * @package appcommonmodel  */ class Node{     private $preKey=null;//鏈表前一個(gè)節(jié)點(diǎn)     private $nextKey=null;//鏈表后一個(gè)節(jié)點(diǎn)     private $value=null;//當(dāng)前的值     private $key=null;//當(dāng)前key       public function __construct(string  $key,$value)     {         $this->value=$value;         $this->key=$key;     }      public function setPreKey($preValue){         $this->preKey=$preValue;     }     public function setNextKey($nextValue){         $this->nextKey=$nextValue;     }      public function getPreKey(){         return $this->preKey;     }      public function getNextKey(){         return $this->nextKey;     }      public function getValue(){         return $this->value;     }      public function setValue($value){         $this->value=$value;     }      public function setKey(string  $key){         $this->key=$key;     }      public function getKey(){         return $this->key;     } }

        2:緩存類

        /**  * 實(shí)現(xiàn)lru緩存  * Class LruCache  * @package appcommonmodel  */ class LruCache {     public $cacheTable =[];     private $headNode=null;     private $lastNode=null;     private $cacheCount=0;     private $cacheMax=100;       /**      * 測試輸出使用      */     public function dumpAllData(){         if (!empty($this->headNode)){             $node=$this->headNode;             while (!empty($node)){                 echo 'key='.$node->getKey().'  nextKey='.(empty($node->getNextKey())?'null':$node->getNextKey()->getKey()).' preKey='.(empty($node->getPreKey())?'null':$node->getPreKey()->getKey()).' value='.$node->getValue()."</br>";                 $node=$node->getNextKey();             }         }     }       /**      * @param int $count      */     public function setCacheMax(int $count){         $this->cacheMax=$count;     }      /**      * @param string $key      * @param $value      * @return bool      */     public function set(string $key,$value){          //設(shè)置值為null,則認(rèn)為刪除緩存節(jié)點(diǎn)         if ($value===null){             $this->del($key);             return true;         }          //判斷是否存在表中,存在則更新連表         if (!empty($this->cacheTable[$key])){             $this->updateList($key);             return true;         }          //先判斷是否要?jiǎng)h除         $this->shiftNode();         $this->addNode($key,$value);         return true;      }      /**      * @param string $key      * @return bool      */     public function del(string $key){         if (!empty($this->cacheTable[$key])){             $node=&$this->cacheTable[$key];              //摘出節(jié)點(diǎn)             $this->jumpNode($node);             //置空刪除             $node->setPreKey(null);             $node->setNextKey(null);             unset($this->cacheTable[$key]);             return true;         }          return false;     }      /**      * @param string $key      * @return null      */     public function get(string $key){         if (!empty($this->cacheTable[$key])){             $this->updateList($key);             return $this->cacheTable[$key]->getValue();         }          return null;     }       //直接添加節(jié)點(diǎn)     private function addNode($key,$value){         $addNode=new Node($key,$value);         if (!empty($this->headNode)){             $this->headNode->setPreKey($addNode);         }         $addNode->setNextKey($this->headNode);          //第一次保存最后一個(gè)節(jié)點(diǎn)為頭節(jié)點(diǎn)         if ($this->lastNode==null){             $this->lastNode=$addNode;         }         $this->headNode=$addNode;         $this->cacheTable[$key]=$addNode;         $this->cacheCount++;     }       //主動(dòng)刪超出的緩存     private function shiftNode(){         while ($this->cacheCount>=$this->cacheMax){             if (!empty($this->lastNode)){                 if (!empty($this->lastNode->getPreKey())){                     $this->lastNode->getPreKey()->setNextKey(null);                 }                 $lastKey=$this->lastNode->getKey();                 unset($this->cacheTable[$lastKey]);             }             $this->cacheCount--;         }      }      //更新節(jié)點(diǎn)鏈表     private function updateList($key){         //這里需要使用引用傳值         $node=&$this->cacheTable[$key];          //當(dāng)前結(jié)點(diǎn)為頭結(jié)點(diǎn) 直接不用處理         if ($this->headNode===$node){             return true;         }          //摘出結(jié)點(diǎn)         $this->jumpNode($node);         //跟頭結(jié)點(diǎn)交換         $node->setNextKey($this->headNode);         $this->headNode->setPreKey($node);         $node->setPreKey(null);         $this->headNode=$node;          return true;      }       //將某個(gè)節(jié)點(diǎn)摘出來     private function jumpNode(Node &$node){         if (!empty($node->getPreKey())){             $node->getPreKey()->setNextKey($node->getNextKey());         }          if (!empty($node->getNextKey())){             $node->getNextKey()->setPreKey($node->getPreKey());         }          //如果是最后一個(gè)節(jié)點(diǎn),則更新最后節(jié)點(diǎn)為它的前節(jié)點(diǎn)         if ($node->getNextKey()==null){             $this->lastNode=$node->getPreKey();         }          //如果是頭結(jié)點(diǎn)         if ($node->getPreKey()==null){             $this->headNode=$node->getNextKey();         }     }    }

        3:測試代碼

            public function tt(){     $cath=model("LruCache");     $cath->setCacheMax(3);     $cath->set("aa","aaaaaaaaaaa");     $cath->set("bb","bbbbbbbbbbbb");     $cath->set("cc","ccccccccccccc");     $cath->get("aa");  //        $cath->dumpAllData();         $cath->set("dd","ddddddddddddd"); //        $cath->del("cc"); //        var_dump($cath->cacheTable);         $cath->dumpAllData();         exit();        }

        推薦學(xué)習(xí):《PHP視頻教程》

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 国产99久久九九精品无码 | 国产高清国产精品国产专区| 97久人人做人人妻人人玩精品| 亚洲av永久无码精品网站| 精品久久久久久无码中文野结衣| 久久精品草草草| 久久综合精品国产二区无码| 国产精品免费大片一区二区| 亚洲精品免费在线观看| 亚洲av午夜国产精品无码中文字| 精品在线免费观看| 国产欧美日韩精品a在线观看| 亚洲国产精品第一区二区三区| 国产精品午夜福利在线无码| 日韩精品www| 国产韩国精品一区二区三区久久| 精品国产福利一区二区| 亚洲精品456播放| 亚洲国产精品国产自在在线| 久久99精品国产麻豆不卡| 国产精品一区12p| 成人一区二区三区精品| 亚洲成人精品久久| 国产一区二区精品| 黑人精品videos亚洲人| 国产亚洲综合成人91精品| 97精品国产一区二区三区| 99re8这里有精品热视频免费| 99热精品在线观看| 99久久国语露脸精品国产| 国产91大片精品一区在线观看| 国产成人久久精品区一区二区| 69国产成人综合久久精品| 成人午夜精品网站在线观看 | 一本久久a久久精品综合香蕉| 久久精品亚洲欧美日韩久久| 久久久这里有精品中文字幕| 四虎亚洲国产成人久久精品| 影院无码人妻精品一区二区| 亚洲精品成人片在线观看精品字幕 | 久久精品国产免费|