当前位置:首页 > PHP

php 实现lru

2026-01-29 22:01:12PHP

LRU 缓存实现原理

LRU(Least Recently Used)缓存淘汰算法会优先移除最近最少使用的数据。PHP 中可通过数组结合时间戳或双向链表实现,以下是两种常见实现方式:

基于数组 + 时间戳的实现

利用数组存储键值对,并通过时间戳记录访问顺序:

class LRUCache {
    private $capacity;
    private $cache = [];

    public function __construct($capacity) {
        $this->capacity = $capacity;
    }

    public function get($key) {
        if (!isset($this->cache[$key])) {
            return -1;
        }
        // 更新访问时间
        $this->cache[$key]['time'] = microtime(true);
        return $this->cache[$key]['value'];
    }

    public function put($key, $value) {
        // 存在则更新
        if (isset($this->cache[$key])) {
            $this->cache[$key] = [
                'value' => $value,
                'time' => microtime(true)
            ];
            return;
        }

        // 超过容量时移除LRU项
        if (count($this->cache) >= $this->capacity) {
            $minTime = PHP_FLOAT_MAX;
            $lruKey = null;
            foreach ($this->cache as $k => $v) {
                if ($v['time'] < $minTime) {
                    $minTime = $v['time'];
                    $lruKey = $k;
                }
            }
            unset($this->cache[$lruKey]);
        }

        $this->cache[$key] = [
            'value' => $value,
            'time' => microtime(true)
        ];
    }
}

基于双向链表的实现

双向链表能高效处理节点移动,结合哈希表实现 O(1) 访问:

php 实现lru

class ListNode {
    public $key;
    public $value;
    public $prev;
    public $next;

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

class LRUCache {
    private $capacity;
    private $map = [];
    private $head;
    private $tail;

    public function __construct($capacity) {
        $this->capacity = $capacity;
        $this->head = new ListNode(-1, -1);
        $this->tail = new ListNode(-1, -1);
        $this->head->next = $this->tail;
        $this->tail->prev = $this->head;
    }

    private function moveToHead($node) {
        $this->removeNode($node);
        $this->addNode($node);
    }

    private function addNode($node) {
        $node->prev = $this->head;
        $node->next = $this->head->next;
        $this->head->next->prev = $node;
        $this->head->next = $node;
    }

    private function removeNode($node) {
        $node->prev->next = $node->next;
        $node->next->prev = $node->prev;
    }

    public function get($key) {
        if (!isset($this->map[$key])) {
            return -1;
        }
        $node = $this->map[$key];
        $this->moveToHead($node);
        return $node->value;
    }

    public function put($key, $value) {
        if (isset($this->map[$key])) {
            $node = $this->map[$key];
            $node->value = $value;
            $this->moveToHead($node);
            return;
        }

        if (count($this->map) >= $this->capacity) {
            $lru = $this->tail->prev;
            $this->removeNode($lru);
            unset($this->map[$lru->key]);
        }

        $node = new ListNode($key, $value);
        $this->addNode($node);
        $this->map[$key] = $node;
    }
}

性能对比

  • 数组实现:代码简单但效率较低,put 操作需遍历所有元素找 LRU,时间复杂度 O(n)。
  • 链表实现getput 均为 O(1),适合高频访问场景,但代码复杂度较高。

实际应用中推荐链表实现,或直接使用 PHP 扩展如 Redis 内置的 LRU 功能。

标签: phplru
分享给朋友:

相关文章

基于php的设计与实现

基于php的设计与实现

PHP 设计与实现的关键要素 环境搭建与框架选择 PHP开发需配置服务器环境(如Apache/Nginx)、PHP版本(建议7.4+)及数据库(MySQL/MariaDB)。主流框架包括Laravel…

php实现跳转

php实现跳转

PHP 实现页面跳转的方法 在 PHP 中实现页面跳转有多种方式,以下是常见的几种方法及其适用场景: 使用 header() 函数 通过 header() 函数发送 HTTP 头部信息实现跳转。注意…

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节点。每个…

php 实现类

php 实现类

PHP 类的基本实现 在 PHP 中,类通过 class 关键字定义,后跟类名和一对花括号。类名通常遵循大驼峰命名法(如 ClassName)。以下是基本语法: class ClassName {…

php 链表实现

php 链表实现

在 PHP 中实现链表可以通过类来模拟链表节点(Node)和链表(LinkedList)结构。以下是链表的完整实现示例,包含基础操作如插入、删除、遍历等。 链表节点类(Node) 链表的基本单元是节…

php 实现链表

php 实现链表

PHP 实现链表的方法 链表是一种线性数据结构,由节点组成,每个节点包含数据和指向下一个节点的指针。PHP 中可以通过类和对象来实现链表。 定义链表节点类 创建一个 ListNode 类,用于表示链…