首页后端开发其他后端知识PHP怎样实现LRU算法,详细代码是什么

PHP怎样实现LRU算法,详细代码是什么

时间2024-03-25 19:38:03发布访客分类其他后端知识浏览1282
导读:在实际案例的操作过程中,我们可能会遇到“PHP怎样实现LRU算法,详细代码是什么”这样的问题,那么我们该如何处理和解决这样的情况呢?这篇小编就给大家总结了一些方法,具有一定的借鉴价值,希望对大家有所帮助,接下来就让小编带领大家一起了解看看吧...
在实际案例的操作过程中,我们可能会遇到“PHP怎样实现LRU算法,详细代码是什么”这样的问题,那么我们该如何处理和解决这样的情况呢?这篇小编就给大家总结了一些方法,具有一定的借鉴价值,希望对大家有所帮助,接下来就让小编带领大家一起了解看看吧。



1.概念

LRU : 最近最少使用算法

2.代码

?php
class Node
{
    
    public $preKey = null;
     //链表前一个节点
    public $nextKey = null;
      //链表后一个节点
    public $key= null;
          //当前的值
    public $value= null;
    //当前key

    public function __construct($key,$value){
    
        $this->
    key = $key;
    
        $this->
    value = $value;

    }


    public function setPre($preKey)
    {
    
        $this->
    preKey = $preKey;

    }


    public function setNext($nextKey)
    {
    
        $this->
    nextKey = $nextKey;

    }

}


class LRUCache{
    
    public $cacheTable = [];
    
    /** Node null  */
    private $headNode = null;
    
    private $lastNode = null;
    
    private $cacheCount = 0;
    
    private $cacheMax = 3;


    public function addNode($key,$value)  //此处采用头插法
    {
    
        $addNode = new Node($key,$value);
    
        if ( !empty($this->
headNode))
        {
    
            $this->
    headNode->
    preKey = $addNode;
 //如果链表存在,将节点添加到节点前一个
        }
    
        $addNode->
    nextKey = $this->
    headNode;
    

        //第一次保存最后一个节点为头结点
        if ($this->
lastNode == null){
    
            $this->
    lastNode = $addNode;

        }
    

        $this->
    headNode = $addNode;
    
        $this->
    cacheTable[$key] = $addNode;
    
        $this->
    cacheCount++;


    }


    public function set($key,$value)
    {
    
        //先判断是否需要删除
        $this->
    shiftNode();
    
        $this->
    addNode($key,$value);
    
        return true;

    }


    //自动删除
    public function shiftNode()
    {
    
        while ($this->
    cacheCount >
    = $this->
cacheMax){
    
            if (!empty($this->
lastNode)){
    
                if ($this->
    lastNode->
preKey){
    
                    $this->
    lastNode->
    preKey->
    nextKey = null;
    
                    $this->
    lastNode = $this->
    lastNode->
    preKey;

                }
    

                unset($this->
    cacheTable[$this->
    lastNode->
    key]);

            }
    
            $this->
    cacheCount --;

        }

    }


    public function dumpAllData()
    {
    
        $node = $this->
    headNode;

        while (($node)){
    
            echo "key=".$node->
    key."value=".$node->
    value."\n";
    
            $node = $node->
    nextKey;

        }

    }


}
    


$Cache = new LRUCache();
    
$Cache->
    set("a","aaaaaaaaaaa");
    
$Cache->
    set("b","bbbbbbbbbbb");
    
$Cache->
    set("c","ccccccccccc");
    
$Cache->
    set("d","dddddddddddd");
    
$Cache->
    set("e","eeeeeeeeeeee");
    
//$Cache->
    set("f","ffffffffffff");
    
$Cache->
    dumpAllData();
    
//var_dump($Cache);
      //是一个深度的数组(对象)



结果

[root@VM-16-13-centos ~]# php LRU.php
key=evalue=eeeeeeeeeeee
key=dvalue=dddddddddddd
key=cvalue=ccccccccccc

总结


通过以上内容的阐述,相信大家对“PHP怎样实现LRU算法,详细代码是什么”已经有了进一步的了解,更多相关的问题,欢迎关注网络或到官网咨询客服。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: PHP怎样实现LRU算法,详细代码是什么
本文地址: https://pptw.com/jishu/652994.html
PHP将数字转中文大写的方法是什么? 如何快速了解PHP GC垃圾回收机制是什么

游客 回复需填写必要信息