m1n64/lru-cache
Composer 安装命令:
composer require m1n64/lru-cache
包简介
README 文档
README
Description
LRUCache is an implementation of a cache based on the Least Recently Used (LRU) algorithm, which automatically removes the least recently used items when the specified capacity limit is reached. This class is useful for optimizing performance by retaining only the most relevant data.
Installation
Make sure you have PHP version 8.1 or higher installed. You can install this package via Composer:
composer require m1n64/lru-cache
Usage
Example
<?php require 'vendor/autoload.php'; use Vasqo\LRU\LRUCache; $cache = new LRUCache(3); // Create a cache with a capacity of 3 items $cache->put('a', 1); $cache->put('b', 2); $cache->put('c', 3); // Get an item echo $cache->get('a'); // 1 $cache->put('d', 4); // 'b' will be removed as it is the least used // Get all items print_r($cache->all()); // ['a' => 1, 'c' => 3, 'd' => 4] // Remove an item $cache->remove('a'); print_r($cache->all()); // ['c' => 3, 'd' => 4]
Tests
To run the tests, run the following command:
composer test
Methods
__construct(int $capacity): Creates a new instance of the cache with the specified capacity.all(): array: Returns all items in the cache.get(string|int $key): mixed: Retrieves an item by key. If the item is found, it is moved to the front of the cache.put(string|int $key, mixed $value): void: Adds an item to the cache or updates an existing one. If the cache is full, the least recently used item will be removed.remove(string|int $key): void: Removes an item by key.count(): int: Returns the number of items in the cache.capacity(): int: Returns the maximum capacity of the cache.
License
This project is licensed under the MIT License. See the LICENSE file for more information.
Author
- m1n64
Feel free to modify any part of it as needed!
m1n64/lru-cache 适用场景与选型建议
m1n64/lru-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 m1n64/lru-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 m1n64/lru-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 59
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-28