当前位置:首页 > PHP

lru php实现

2026-02-14 20:54:17PHP

LRU 缓存算法的 PHP 实现

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

lru php实现

使用数组和时间戳实现

通过维护一个数组记录键值对,并为每个键附加最后访问时间戳来实现 LRU 逻辑。

lru php实现

class LRUCache {
    private $capacity;
    private $cache = [];
    private $timestamps = [];

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

    public function get($key) {
        if (isset($this->cache[$key])) {
            $this->timestamps[$key] = microtime(true);
            return $this->cache[$key];
        }
        return -1;
    }

    public function put($key, $value) {
        if (count($this->cache) >= $this->capacity && !isset($this->cache[$key])) {
            asort($this->timestamps);
            $oldest = key($this->timestamps);
            unset($this->cache[$oldest], $this->timestamps[$oldest]);
        }
        $this->cache[$key] = $value;
        $this->timestamps[$key] = 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 addNode($node) {
        $node->prev = $this->head;
        $node->next = $this->head->next;
        $this->head->next->prev = $node;
        $this->head->next = $node;
    }

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

    private function moveToHead($node) {
        $this->removeNode($node);
        $this->addNode($node);
    }

    private function popTail() {
        $node = $this->tail->prev;
        $this->removeNode($node);
        return $node;
    }

    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);
        } else {
            if (count($this->map) >= $this->capacity) {
                $tail = $this->popTail();
                unset($this->map[$tail->key]);
            }
            $node = new ListNode($key, $value);
            $this->map[$key] = $node;
            $this->addNode($node);
        }
    }
}

使用 SplDoublyLinkedList 实现

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

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

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

    public function get($key) {
        if (!isset($this->map[$key])) return -1;
        $this->list->unshift($this->map[$key]);
        $this->list->offsetUnset($this->list->key());
        return $this->map[$key]['value'];
    }

    public function put($key, $value) {
        if (isset($this->map[$key])) {
            $this->list->unshift(['key' => $key, 'value' => $value]);
            $this->list->offsetUnset($this->list->key());
        } else {
            if (count($this->map) >= $this->capacity) {
                $oldest = $this->list->pop();
                unset($this->map[$oldest['key']]);
            }
            $this->list->unshift(['key' => $key, 'value' => $value]);
        }
        $this->map[$key] = $this->list->bottom();
    }
}

实现要点说明

  • 数组实现简单但效率较低,适合小规模数据
  • 双向链表+哈希表实现性能最优,get/put操作均为O(1)
  • SplDoublyLinkedList实现代码更简洁但性能略低于自定义链表
  • 实际生产环境推荐使用Redis等专业缓存系统的LRU功能

标签: lruphp
分享给朋友:

相关文章

php搜索功能实现

php搜索功能实现

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

php实现博客

php实现博客

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

php实现统计

php实现统计

PHP 实现统计功能 在 PHP 中实现统计功能通常涉及数据的收集、处理和展示。以下是一些常见的统计实现方法: 数据库数据统计 通过 SQL 查询可以直接从数据库中获取统计结果: $conn =…

php 实现队列

php 实现队列

PHP 实现队列的方法 PHP 可以通过多种方式实现队列功能,以下是几种常见的实现方法: 使用数组实现队列 PHP 数组可以通过 array_push 和 array_shift 函数模拟队列的先进…

php实现定时

php实现定时

PHP 实现定时任务的方法 PHP 本身是脚本语言,通常需要借助外部工具或框架实现定时任务。以下是几种常见方法: 使用 Cron 任务 Linux 系统自带的 Cron 是常用的定时任务工具。通过编…

php 线程实现

php 线程实现

PHP 线程实现方法 PHP 本身是单线程语言,但可以通过扩展或库实现多线程功能。以下是几种常见的实现方式: 使用 pthreads 扩展 pthreads 是一个 PHP 扩展,允许在 PHP 中…