PHP實現(xiàn)LRU算法的示例代碼
原理
LRU是Least Recently Used 近期最少使用算法。 內(nèi)存管理的一種頁面置換算法,對于在內(nèi)存中但又不用的數(shù)據(jù)塊(內(nèi)存塊)叫做LRU,操作系統(tǒng)會根據(jù)哪些數(shù)據(jù)屬于LRU而將其移出內(nèi)存而騰出空間來加載另外的數(shù)據(jù)。
什么是LRU算法?LRU是Least Recently Used的縮寫,即最近最久未使用,常用于頁面置換算法,是為虛擬頁式存儲管理服務的。
關(guān)于操作系統(tǒng)的內(nèi)存管理,如何節(jié)省利用容量不大的內(nèi)存為最多的進程提供資源,一直是研究的重要方向。而內(nèi)存的虛擬存儲管理,是現(xiàn)在最通用,最成功的方式—— 在內(nèi)存有限的情況下,擴展一部分外存作為虛擬內(nèi)存,真正的內(nèi)存只存儲當前運行時所用得到信息。這無疑極大地擴充了內(nèi)存的功能,極大地提高了計算機的并發(fā)度。
虛擬頁式存儲管理,則是將進程所需空間劃分為多個頁面,內(nèi)存中只存放當前所需頁面,其余頁面放入外存的管理方式。
然而,有利就有弊,虛擬頁式存儲管理減少了進程所需的內(nèi)存空間,卻也帶來了運行時間變長這一缺點:進程運行過程中,不可避免地要把在外存中存放的一些信息和內(nèi)存中已有的進行交換,由于外存的低速,這一步驟所花費的時間不可忽略。
因而,采取盡量好的算法以減少讀取外存的次數(shù),也是相當有意義的事情。
基本原理
假設(shè) 序列為 4 3 4 2 3 1 4 2物理塊有3個 則首輪 4調(diào)入內(nèi)存 4次輪 3調(diào)入內(nèi)存 3 4之后 4調(diào)入內(nèi)存 4 3之后 2調(diào)入內(nèi)存 2 4 3之后 3調(diào)入內(nèi)存 3 2 4之后 1調(diào)入內(nèi)存 1 3 2(因為最少使用的是4,所以丟棄4)之后 4調(diào)入內(nèi)存 4 1 3(原理同上)最后 2調(diào)入內(nèi)存 2 4 1
規(guī)律就是,如果新存入或者訪問一個值,則將這個值放在隊列開頭。如果存儲容量超過上限cap,那么刪除隊尾元素,再存入新的值。
整體設(shè)計
用數(shù)組保存緩存對象(Node);
緩存對象(Node)之間通過nextKey,preKey組成一個雙向鏈表;
保存鏈表頭 跟尾;
處理流程
主要代碼
Node 節(jié)點類
/** ?*?緩存值保存類, ?*?Class?Node ?*?@package?app\common\model ?*/ 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; ????} }
緩存類
/** ?*?實現(xiàn)lru緩存 ?*?Class?LruCache ?*?@package?app\common\model ?*/ 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,則認為刪除緩存節(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]; ????????//當前結(jié)點為頭結(jié)點?直接不用處理 ????????if?($this->headNode===$node){ ????????????return?true; ????????} ????????//摘出結(jié)點 ????????$this->jumpNode($node); ????????//跟頭結(jié)點交換 ????????$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(); ????????} ????????//如果是頭結(jié)點 ????????if?($node->getPreKey()==null){ ????????????$this->headNode=$node->getNextKey(); ????????} ????} }
測試代碼
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的數(shù)組就是有序的,也可以直接用php數(shù)組實現(xiàn),這里只是提供一個實現(xiàn)的思路,僅供參考哈!
以上就是PHP實現(xiàn)LRU算法的示例代碼的詳細內(nèi)容,更多關(guān)于PHP LRU算法的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php中cookie與session的區(qū)別點總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于php中cookie與session的區(qū)別點總結(jié)內(nèi)容,有興趣的朋友們可以參考學習下。2021-12-12解析PHP留言本模塊主要功能的函數(shù)說明(代碼可實現(xiàn))
本篇文章是對PHP留言本中主要的函數(shù)以及代碼進行了詳細的分析介紹,需要的朋友參考下2013-06-06