当前位置:首页 > PHP

php实现lru

2026-02-28 15:44:34PHP

PHP实现LRU缓存算法

LRU(Least Recently Used)是一种常用的缓存淘汰策略,它会优先淘汰最近最少使用的数据。以下是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等函数简化实现。

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实现介于两者之间。

php实现lru

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

标签: phplru
分享给朋友:

相关文章

php实现队列

php实现队列

PHP实现队列的方法 队列是一种先进先出(FIFO)的数据结构,PHP可以通过多种方式实现队列功能。 使用数组模拟队列 PHP数组可以模拟队列的基本操作,通过array_push和array_shi…

php实现聊天

php实现聊天

实现 PHP 聊天功能的方法 使用 WebSocket 和 PHP 实现实时聊天 WebSocket 是一种全双工通信协议,适合实现实时聊天功能。PHP 可以通过 Ratchet 库实现 WebSo…

php实现推送

php实现推送

PHP 实现推送功能的方法 PHP 实现推送功能可以通过多种方式完成,包括 WebSocket、Server-Sent Events (SSE)、长轮询或第三方推送服务。以下是几种常见的方法: 使用…

php 实现跳转

php 实现跳转

PHP 实现页面跳转的方法 在PHP中,实现页面跳转可以通过多种方式完成,以下是几种常用的方法: 使用header函数 header函数是PHP中最常用的跳转方法,通过发送HTTP头部信息实现跳转。…

php实现注册登录

php实现注册登录

PHP 注册功能实现 创建数据库表(以 MySQL 为例) CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, usern…

php实现域名

php实现域名

PHP实现域名操作的方法 获取当前域名 使用$_SERVER['HTTP_HOST']可以获取当前访问的域名: $currentDomain = $_SERVER['HTTP_HOST']; ech…