当前位置:首页 > PHP

php 实现lru

2026-02-14 19:53:27PHP

LRU 缓存实现原理

LRU(Least Recently Used)是一种缓存淘汰策略,当缓存空间不足时,优先移除最久未被使用的数据。PHP 实现 LRU 通常需要结合哈希表(快速查找)和双向链表(维护访问顺序)。

php 实现lru

基于数组和链表的实现

PHP 中可以通过数组模拟哈希表,结合 SplDoublyLinkedList 实现 LRU:

php 实现lru

class LRUCache {
    private $capacity;
    private $map = [];
    private $list;

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

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

        $node = $this->map[$key];
        $this->list->push($this->list->shift()); // 移动到链表尾部
        return $node['value'];
    }

    public function put($key, $value) {
        if (isset($this->map[$key])) {
            unset($this->map[$key]);
            $this->list->shift(); // 移除旧节点
        }

        if (count($this->map) >= $this->capacity) {
            $oldKey = $this->list->shift();
            unset($this->map[$oldKey]);
        }

        $this->list->push(['key' => $key, 'value' => $value]);
        $this->map[$key] = end($this->list);
    }
}

使用 SplDoublyLinkedList 优化

SplDoublyLinkedList 提供高效的头尾操作,适合 LRU 场景:

class LRUCache {
    private $capacity;
    private $map = [];
    private $list;

    public function __construct($capacity) {
        $this->capacity = $capacity;
        $this->list = new SplDoublyLinkedList();
        $this->list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO);
    }

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

        $node = $this->map[$key];
        $this->list->push($node);
        $this->list->offsetUnset($this->list->key());
        return $node['value'];
    }

    public function put($key, $value) {
        if (isset($this->map[$key])) {
            $this->list->offsetUnset($this->list->key());
        }

        if (count($this->map) >= $this->capacity) {
            $oldNode = $this->list->shift();
            unset($this->map[$oldNode['key']]);
        }

        $node = ['key' => $key, 'value' => $value];
        $this->list->push($node);
        $this->map[$key] = $node;
    }
}

纯数组实现方案

如果环境限制不能使用 SPL,可以用纯数组模拟:

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

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

    public function get($key) {
        if (!isset($this->cache[$key])) {
            return -1;
        }

        unset($this->order[array_search($key, $this->order)]);
        $this->order[] = $key;
        return $this->cache[$key];
    }

    public function put($key, $value) {
        if (isset($this->cache[$key])) {
            unset($this->order[array_search($key, $this->order)]);
        }

        if (count($this->cache) >= $this->capacity) {
            $oldKey = array_shift($this->order);
            unset($this->cache[$oldKey]);
        }

        $this->cache[$key] = $value;
        $this->order[] = $key;
    }
}

时间复杂度分析

  • 哈希表操作:O(1) 时间完成键值查找
  • 链表操作:移动节点到尾部或删除头部节点均为 O(1)
  • 数组实现array_search 导致 O(n) 时间复杂度,适合小规模数据

实际应用建议

  • 生产环境推荐使用 SplDoublyLinkedList 方案,性能最佳
  • 超大规模数据建议使用 Redis 等专业缓存系统内置的 LRU 策略
  • 缓存容量需要根据业务场景合理设置,过小会导致频繁淘汰,过大浪费内存

标签: phplru
分享给朋友:

相关文章

git php实现

git php实现

Git 与 PHP 的集成实现 使用 PHP 执行 Git 命令 通过 PHP 的 exec()、shell_exec() 或 passthru() 函数可以直接调用 Git 命令,适用于服务器端操作…

php实现

php实现

PHP 实现的基本方法 PHP 是一种服务器端脚本语言,广泛用于 Web 开发。以下是 PHP 实现的一些常见方法。 变量与数据类型 PHP 变量以 $ 开头,支持多种数据类型: $nam…

php 路由实现

php 路由实现

PHP 路由实现方法 在 PHP 中实现路由功能有多种方式,以下是几种常见的实现方法: 使用原生 PHP 实现 通过解析 URL 并匹配对应的控制器和动作: $request = $_SERV…

php怎么实现登录功能

php怎么实现登录功能

数据库准备 创建用户表存储登录信息,通常包括用户名、密码(需加密)等字段。示例SQL: CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY…

php 实现排序

php 实现排序

PHP 实现数组排序的方法 PHP 提供了多种内置函数和方法来实现数组排序,适用于不同的排序需求。以下是一些常见的排序实现方式: 使用 sort() 函数对数组进行升序排序 sort() 函数对数组…

php实现博客

php实现博客

使用PHP实现博客系统 数据库设计 创建数据库表存储博客文章和用户信息。常见的表包括posts(文章)和users(用户)。 CREATE TABLE users ( id INT AUT…