当前位置:首页 > PHP

php实现lru

2026-02-28 15:44:34PHP

PHP实现LRU缓存算法

LRU(Least Recently Used)是一种常用的缓存淘汰策略,它会优先淘汰最近最少使用的数据。以下是PHP实现LRU缓存的几种方法:

双向链表+哈希表实现

使用双向链表维护数据的使用顺序,哈希表存储键值对以实现快速访问。

php实现lru

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->remove($node);
        $this->add($node);
        return $node->value;
    }

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

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

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

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

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

使用PHP内置数组实现

利用PHP数组的键值对和array_shift等函数简化实现。

php实现lru

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

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

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

    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;
    }
}

使用SplDoublyLinkedList实现

PHP标准库提供的双向链表结构可以简化实现。

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

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

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

    function put($key, $value) {
        if (isset($this->map[$key])) {
            $node = $this->map[$key];
            $this->list->detach($node);
        }
        $node = new SplDoublyLinkedListNode($value);
        $node->key = $key;
        $this->map[$key] = $node;
        $this->list->push($node);
        if (count($this->map) > $this->capacity) {
            $oldest = $this->list->shift();
            unset($this->map[$oldest->key]);
        }
    }
}

性能比较

双向链表+哈希表实现提供了O(1)时间复杂度的get和put操作,是最优的实现方式。PHP数组实现简单但性能较差,特别是在缓存较大时。SplDoublyLinkedList实现介于两者之间。

实际应用中应根据具体需求选择实现方式,对于高性能要求的场景推荐使用第一种实现。

标签: phplru
分享给朋友:

相关文章

php实现图片上传

php实现图片上传

实现图片上传的基本步骤 创建HTML表单用于文件上传,表单需设置enctype="multipart/form-data"属性,确保文件数据正确传输。 <form action="upload…

php实现排序

php实现排序

PHP 数组排序方法 PHP 提供了多种内置函数用于数组排序,适用于不同的排序需求。以下是常见的排序方法: 按值升序排序 sort() 函数对数组按值升序排序,重置键名(索引数组适用): $n…

php队列的实现

php队列的实现

PHP队列的实现方式 PHP中实现队列功能可以通过多种方式,根据需求选择适合的方案: 使用数组实现基础队列 PHP数组本身支持队列操作,利用array_push和array_shift函数:…

php实现一个

php实现一个

PHP 实现一个简单的计算器 创建一个基本的计算器,支持加、减、乘、除运算。以下是一个完整的实现代码示例: <?php $result = ""; if ($_SERVER["REQUEST…

无限分类实现 php

无限分类实现 php

无限分类的实现方法 无限分类通常用于构建多层级结构的数据,如商品分类、部门架构等。以下是几种常见的实现方式: 邻接列表模式(Adjacency List) 邻接列表是最简单的实现方式,通过在每个记…

php实现https

php实现https

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