欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

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

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

          整體設計

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

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

          3:保存鏈表頭 跟尾;

          處理流程如下圖:

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

          主要代碼

          1:Node 節(jié)點類

          /**  * 緩存值保存類,  * Class Node  * @package appcommonmodel  */ class Node{     private $preKey=null;//鏈表前一個節(jié)點     private $nextKey=null;//鏈表后一個節(jié)點     private $value=null;//當前的值     private $key=null;//當前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:緩存類

          /**  * 實現(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){          //設置值為null,則認為刪除緩存節(jié)點         if ($value===null){             $this->del($key);             return true;         }          //判斷是否存在表中,存在則更新連表         if (!empty($this->cacheTable[$key])){             $this->updateList($key);             return true;         }          //先判斷是否要刪除         $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é)點             $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é)點     private function addNode($key,$value){         $addNode=new Node($key,$value);         if (!empty($this->headNode)){             $this->headNode->setPreKey($addNode);         }         $addNode->setNextKey($this->headNode);          //第一次保存最后一個節(jié)點為頭節(jié)點         if ($this->lastNode==null){             $this->lastNode=$addNode;         }         $this->headNode=$addNode;         $this->cacheTable[$key]=$addNode;         $this->cacheCount++;     }       //主動刪超出的緩存     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é)點鏈表     private function updateList($key){         //這里需要使用引用傳值         $node=&$this->cacheTable[$key];          //當前結點為頭結點 直接不用處理         if ($this->headNode===$node){             return true;         }          //摘出結點         $this->jumpNode($node);         //跟頭結點交換         $node->setNextKey($this->headNode);         $this->headNode->setPreKey($node);         $node->setPreKey(null);         $this->headNode=$node;          return true;      }       //將某個節(jié)點摘出來     private function jumpNode(Node &$node){         if (!empty($node->getPreKey())){             $node->getPreKey()->setNextKey($node->getNextKey());         }          if (!empty($node->getNextKey())){             $node->getNextKey()->setPreKey($node->getPreKey());         }          //如果是最后一個節(jié)點,則更新最后節(jié)點為它的前節(jié)點         if ($node->getNextKey()==null){             $this->lastNode=$node->getPreKey();         }          //如果是頭結點         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();        }

          推薦學習:《PHP視頻教程》

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號