当前位置:首页 > 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中实现验证码功能通常涉及生成随机字符串或数字,将其转换为图像,并通过会话(Session)进行验证。以下是几种常见的实现方式: 使用GD库生成图像验证码 GD库是PHP内置的…

php 登录实现

php 登录实现

PHP 登录实现 PHP 登录功能通常包括用户认证、会话管理和安全性措施。以下是实现 PHP 登录功能的关键步骤。 数据库准备 创建一个用户表存储用户信息,通常包括用户名、密码(加密存储)和其他必要…

php 实现文件下载

php 实现文件下载

实现文件下载的基本方法 使用 PHP 实现文件下载的核心是通过设置 HTTP 头部信息,强制浏览器将文件作为附件下载而非直接显示。以下是一个基础实现示例: $file_path = '/path/t…

php 实现单链表

php 实现单链表

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

php实现https

php实现https

PHP 实现 HTTPS 请求 在 PHP 中实现 HTTPS 请求,可以通过多种方式完成,包括使用内置函数、cURL 扩展或第三方库。以下是几种常见的方法: 使用 file_get_content…

php实现异步图片上传

php实现异步图片上传

异步图片上传的实现方法 在PHP中实现异步图片上传通常需要结合前端JavaScript和后端PHP代码。以下是几种常见的方法: 使用XMLHttpRequest或Fetch API 前端使用Java…