站長資訊網(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)
        主站蜘蛛池模板: 色国产精品一区在线观看| 国产精品久久久久久福利69堂| 久久久免费精品re6| 国产精品美女久久久久AV福利| 精品久久久久久中文字幕人妻最新| 国产日韩高清三级精品人成| 高清在线亚洲精品国产二区| 亚洲AV永久纯肉无码精品动漫| 精品久久久久久无码中文字幕| 久久久国产精品福利免费| 国产精品视频一区二区三区无码| 亚洲中文字幕久久精品无码喷水| 久久国产精品免费一区| 粉嫩精品美女国产在线观看| 国产精品视频久久久| 国内精品久久久久久99| 亚洲精品WWW久久久久久| 久久精品?ⅴ无码中文字幕| 一区二区三区精品| 国产精品久久久久久一区二区三区 | 天天爽夜夜爽8888视频精品| 99RE8这里有精品热视频| 久久精品国产99国产电影网 | 久久最新精品国产| 97视频在线观看这里只有精品 | 精品国产欧美一区二区| 国产精品视频一区二区三区四 | 91大神精品全国在线观看| 秋霞午夜鲁丝片午夜精品久| 国内精品久久久久影院免费| 久久91综合国产91久久精品| 国产精品精品自在线拍| 国内精品人妻无码久久久影院| 亚洲精品岛国片在线观看| 自拍偷自拍亚洲精品被多人伦好爽| 亚洲综合精品网站| 无码精品A∨在线观看| 奇米精品视频一区二区三区| 精品无码人妻一区二区免费蜜桃 | 国产成人精品综合久久久| 国产精品成人啪精品视频免费|