当前位置:首页 > PHP

php实现lru

2026-01-29 18:29:01PHP

PHP实现LRU缓存算法

LRU(Least Recently Used)是一种常见的缓存淘汰策略,核心思想是当缓存空间不足时优先移除最近最少使用的数据。以下是PHP实现LRU缓存的两种典型方式:

双向链表+哈希表实现

使用双向链表维护访问顺序,哈希表实现快速查找,时间复杂度O(1):

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

    function __construct($capacity) {
        $this->capacity = $capacity;
        $this->head = new Node(0, 0);
        $this->tail = new Node(0, 0);
        $this->head->next = $this->tail;
        $this->tail->prev = $this->head;
    }

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

    function put($key, $value) {
        if (isset($this->map[$key])) {
            $node = $this->map[$key];
            $node->value = $value;
            $this->removeNode($node);
            $this->addToHead($node);
        } else {
            $node = new Node($key, $value);
            $this->map[$key] = $node;
            $this->addToHead($node);
            if (count($this->map) > $this->capacity) {
                $last = $this->tail->prev;
                $this->removeNode($last);
                unset($this->map[$last->key]);
            }
        }
    }

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

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

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

使用PHP内置数组模拟

简化版实现,利用array_keys()维护访问顺序:

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

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

    public function get($key) {
        if (!isset($this->cache[$key])) return null;
        $value = $this->cache[$key];
        unset($this->cache[$key]);
        $this->cache[$key] = $value; // 移动到"最近使用"位置
        return $value;
    }

    public function put($key, $value) {
        if (isset($this->cache[$key])) {
            unset($this->cache[$key]);
        } elseif (count($this->cache) >= $this->capacity) {
            array_shift($this->cache); // 移除最久未使用的元素
        }
        $this->cache[$key] = $value;
    }
}

实际应用建议

生产环境推荐使用成熟的缓存库:

php实现lru

  1. 直接使用Redis等内存数据库的LRU功能
  2. 采用Symfony Cache或Laravel Cache组件
  3. 考虑使用SplObjectStorage等SPL数据结构优化性能

性能关键场景应选择双向链表实现,简单场景可用数组简化版。注意PHP数组的"最近使用"特性实际上是通过哈希表实现的,严格来说并非标准LRU。

标签: phplru
分享给朋友:

相关文章

php实现增删改查

php实现增删改查

以下是在PHP中实现增删改查(CRUD)操作的详细方法,以MySQL数据库为例: 数据库连接配置 创建config.php文件存储数据库连接信息: <?php $host = 'localh…

php实现博客

php实现博客

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

php数组实现

php数组实现

PHP 数组的基本实现 PHP 数组是一种非常灵活的数据结构,可以存储多个值,并且支持多种类型的键(整数或字符串)。PHP 数组实际上是有序映射(ordered map),可以看作是列表(vector…

php 实现评论

php 实现评论

实现 PHP 评论功能 数据库设计 创建数据库表存储评论数据,通常包含以下字段: id:评论唯一标识 user_id:用户ID content:评论内容 created_at:评论时间 parent…

php 链表实现

php 链表实现

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

php实现重定向

php实现重定向

使用header函数进行重定向 在PHP中,header()函数是实现重定向的常用方法。通过发送HTTP头信息Location,浏览器会自动跳转到指定URL。需确保在调用header()前没有输出任何…