当前位置:首页 > PHP

lru php实现

2026-01-29 23:01:18PHP

PHP实现LRU缓存算法

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

使用双向链表+哈希表实现

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

    function __construct($capacity) {
        $this->capacity = $capacity;
        $this->head = new Node(null, null);
        $this->tail = new Node(null, null);
        $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 {
            if (count($this->map) >= $this->capacity) {
                $last = $this->tail->prev;
                $this->removeNode($last);
                unset($this->map[$last->key]);
            }
            $node = new Node($key, $value);
            $this->map[$key] = $node;
            $this->addToHead($node);
        }
    }

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

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

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

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

使用PHP内置数据结构实现

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

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

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

        // 更新访问顺序
        $index = array_search($key, $this->order);
        unset($this->order[$index]);
        array_unshift($this->order, $key);

        return $this->cache[$key];
    }

    public function put($key, $value) {
        if (isset($this->cache[$key])) {
            // 更新现有值
            $this->cache[$key] = $value;
            $index = array_search($key, $this->order);
            unset($this->order[$index]);
        } else {
            // 检查容量
            if (count($this->cache) >= $this->capacity) {
                $oldest = array_pop($this->order);
                unset($this->cache[$oldest]);
            }
            $this->cache[$key] = $value;
        }
        array_unshift($this->order, $key);
    }
}

实现要点

双向链表实现方式时间复杂度为O(1),适合高性能场景。使用哈希表存储节点引用,双向链表维护访问顺序。

简单实现方式利用PHP数组特性,代码更简洁但性能稍差,array_search操作时间复杂度为O(n)。

实际应用中可根据需求选择:

lru php实现

  • 高频访问场景建议使用双向链表实现
  • 简单业务场景可使用数组实现
  • 生产环境建议考虑成熟的缓存库如Redis

标签: lruphp
分享给朋友:

相关文章

php实现文件的下载

php实现文件的下载

PHP 实现文件下载的方法 在 PHP 中实现文件下载功能通常需要设置正确的 HTTP 头信息,并输出文件内容。以下是几种常见的实现方式: 使用 header() 函数强制下载 通过设置 Conte…

php实现分类

php实现分类

PHP实现分类的方法 数据库设计 创建分类表时,通常需要包含id、名称、父级id等字段。父级id用于实现多级分类结构。 CREATE TABLE categories ( id INT AU…

php实现接口

php实现接口

PHP 实现接口的方法 在 PHP 中,接口(Interface)用于定义一组方法的规范,而不实现具体功能。类可以实现一个或多个接口,并必须实现接口中定义的所有方法。 定义接口 接口使用 inter…

php搜索功能实现

php搜索功能实现

实现基本的PHP搜索功能 使用MySQL数据库和PHP实现简单的关键词搜索功能。需要确保数据库中有可供搜索的表和数据。 // 连接数据库 $conn = new mysqli('localhost'…

php实现博客

php实现博客

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

php 实现评论

php 实现评论

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