您好,欢迎访问一九零五行业门户网

半天撸完一个PHP 实现LRU 算法的知识

我们学习了解了这么多关于php的知识,今天学习如何半天撸完一个php 实现lru 算法的知识,不知你们是否已经完全掌握了呢,如果没有,那就跟随本篇文章一起继续学习吧整体设计1:用数组保存缓存对象(node);
2:缓存对象(node)之间通过nextkey,prekey组成一个双向链表;
3:保存链表头 跟尾;
处理流程如下图:
主要代码1:node 节点类
/** * 缓存值保存类, * class node * @package app\common\model */class node{ private $prekey=null;//链表前一个节点 private $nextkey=null;//链表后一个节点 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:缓存类
/** * 实现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){ //设置值为null,则认为删除缓存节点 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]; //摘出节点 $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; } //直接添加节点 private function addnode($key,$value){ $addnode=new node($key,$value); if (!empty($this->headnode)){ $this->headnode->setprekey($addnode); } $addnode->setnextkey($this->headnode); //第一次保存最后一个节点为头节点 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--; } } //更新节点链表 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; } //将某个节点摘出来 private function jumpnode(node &$node){ if (!empty($node->getprekey())){ $node->getprekey()->setnextkey($node->getnextkey()); } if (!empty($node->getnextkey())){ $node->getnextkey()->setprekey($node->getprekey()); } //如果是最后一个节点,则更新最后节点为它的前节点 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视频教程》
以上就是半天撸完一个php 实现lru 算法的知识的详细内容。
其它类似信息

推荐信息