mrmanchot/simple-cache
Composer 安装命令:
composer require mrmanchot/simple-cache
包简介
A simple and efficient PHP library for cache management.
README 文档
README
SimpleCache is a lightweight and efficient PHP caching library, designed for ease of use and flexibility. Whether you're caching strings, arrays, objects, or booleans, SimpleCache provides a straightforward and intuitive API to speed up your PHP applications. With features like cache expiration, subdirectory organization, and security measures, it's an ideal solution for both small projects and large-scale applications.
Features
- Easy to use
- Supports multiple data types: strings, arrays, objects, and booleans
- Allows setting cache expiration time
- Option to bypass cache
- Sanitizes cache keys for security
- Graceful error handling with warnings instead of exceptions
Installation
Use Composer to install this library:
composer require mrmanchot/simple-cache
Usage
Initialization
use Mrmanchot\SimpleCache\SimpleCache; require 'vendor/autoload.php'; $cache = new SimpleCache('/path/to/cache/directory/');
Basic Usage
// Set cache $cache->set('key', 'value'); // Get cache $value = $cache->get('key');
Using Expiration Time
You can specify an expiration time in minutes using the $delayMinutes parameter.
// Set cache $cache->set('key', 'value'); // Get cache, valid for 10 minutes $value = $cache->get('key', 10);
Storing Arrays, Objects, and Booleans
You can also store arrays, objects, and booleans.
// Storing an array $cache->set('array_key', ['a' => 1, 'b' => 2]); // Retrieving an array $array = $cache->get('array_key'); // Storing an object $object = new stdClass(); $object->property = 'value'; $cache->set('object_key', $object); // Retrieving an object $object = $cache->get('object_key'); // Storing a boolean $cache->set('boolean_key', true); // Retrieving a boolean $boolean = $cache->get('boolean_key');
Handling Cache Hits and Misses
When retrieving data from the cache, you can distinguish between a cache hit and a cache miss by checking if the returned value is null. If the value is null, it indicates that the data is not present in the cache (a cache miss), and you may need to compute or generate the value. If the value is not null, it is a cache hit, and you can use the cached value directly.
Here's an example of how to handle cache hits and misses:
$cachedValue = $cache->get('some_key'); if ($cachedValue === null) { // The value is not in the cache, compute/generate the value }
Using Subdirectories as Keys
You can use subdirectories in keys for better organization. Note that keys are sanitized for security purposes, so only certain characters are allowed.
// Set cache in a subdirectory $cache->set('user/1', 'value'); // Get cache from a subdirectory $value = $cache->get('user/1');
Clearing the Cache
The clear method allows you to remove cached items based on a pattern. This is useful for batch invalidation of cache items.
// Clear a specific cache item $cache->clear('key'); // Clear all cache items in a subdirectory $cache->clear('user/*'); // Clear all cache items $cache->clear('*');
Run PHPUnit
phpunit test/SimpleCacheTest.php
mrmanchot/simple-cache 适用场景与选型建议
mrmanchot/simple-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 10 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mrmanchot/simple-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mrmanchot/simple-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-10-20