当前位置:首页 > PHP

lru php实现

2026-02-14 20:54:17PHP

LRU 缓存算法的 PHP 实现

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

使用数组和时间戳实现

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

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功能

lru php实现

标签: lruphp
分享给朋友:

相关文章

php如何实现直播

php如何实现直播

实现直播功能的方法 PHP可以通过结合其他技术和工具来实现直播功能。以下是几种常见的方法: 使用流媒体服务器 配置流媒体服务器如Nginx-RTMP、Red5或Wowza。这些服务器支持RTM…

php 路由实现

php 路由实现

PHP 路由实现方法 在 PHP 中实现路由功能有多种方式,以下是几种常见的实现方法: 使用原生 PHP 实现 通过解析 URL 并匹配对应的控制器和动作: $request = $_SERVER…

php无限分类的实现

php无限分类的实现

PHP无限分类的实现 无限分类是一种常见的树形结构数据组织方式,适用于商品分类、菜单管理、地区选择等场景。以下是几种实现方法: 递归实现法 递归是最直观的实现方式,通过函数自身调用来遍历子分类。数据…

php 实现排序

php 实现排序

PHP 实现数组排序的方法 PHP 提供了多种内置函数和方法来实现数组排序,适用于不同的排序需求。以下是一些常见的排序实现方式: 使用 sort() 函数对数组进行升序排序 sort() 函数对数组…

php搜索功能实现的

php搜索功能实现的

PHP 搜索功能实现方法 数据库搜索实现 使用 SQL 的 LIKE 语句进行模糊匹配,适用于小型网站和简单搜索需求。 $searchTerm = $_GET['search']; $query…

php 实现单点登录

php 实现单点登录

PHP 实现单点登录的方法 单点登录(SSO)允许用户通过一次登录访问多个相互信任的系统。以下是基于 PHP 的实现方案: 基于共享 Session 的方案 在同一个主域名下的子域名间可以通过共享…