当前位置:首页 > 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) 访问:

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 功能。

php 实现lru

标签: phplru
分享给朋友:

相关文章

php实现文件上传

php实现文件上传

PHP 文件上传实现方法 在 PHP 中实现文件上传需要处理 HTML 表单和服务器端脚本。以下是详细步骤: 创建 HTML 表单 使用 enctype="multipart/form-data"…

php实现socket

php实现socket

PHP 实现 Socket 通信 PHP 可以通过 socket 扩展实现网络通信,支持 TCP/UDP 协议。以下是实现 Socket 通信的核心步骤和代码示例。 创建 TCP 服务器 // 创建…

php实现websocket

php实现websocket

PHP 实现 WebSocket WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议。PHP 可以通过多种方式实现 WebSocket 功能,以下是几种常见的方法: 使用 Rat…

php实现栈

php实现栈

栈的基本概念 栈(Stack)是一种遵循后进先出(LIFO)原则的数据结构。核心操作包括: push:元素入栈。 pop:元素出栈(返回并移除栈顶元素)。 peek:查看栈顶元素(不移…

php实现

php实现

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

php实现上传

php实现上传

PHP 文件上传实现 HTML 表单设置 创建一个包含文件上传字段的 HTML 表单,确保表单的 enctype 属性设置为 multipart/form-data,这是文件上传必需的设置。 &l…