lufiipe/simplecache
Composer 安装命令:
composer require lufiipe/simplecache
包简介
Simple file system cache library for PHP
关键字:
README 文档
README
SimpleCache
Simple cache library for PHP based on PSR-16. It uses a hybrid system: fast in-memory storage for temporary data and file storage for persistent data.
Install
composer require lufiipe/simplecache
Usage
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $cache->set('key', 'value'); $value = $cache->get('key');
Setup the cache
The Cache class accepts the following arguments:
- string
$cacheDir: Path to a writable folder for storing cache files. If not provided, default system 'tmp' folder will be used. - null|int|\DateInterval
$defaultTtl: Default Time to Live in secondes. By defaults 3600 secondes.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(__DIR__ . '/cache', 1800); // Equivalent to: $ttl = \DateInterval::createFromDateString('30 minutes'); $cache = new Cache(__DIR__ . '/cache', $ttl);
Retrieving items from the cache
Cache::get()
The get() method retrieves items from the cache. If the item is not found, it returns null.
Alternatively, you can provide a second argument to specify a default value to return when the item is missing.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $value = $cache->get('key'); $value = $cache->get('key', 'default');
Cache::getMultiple()
The getMultiple() method retrieves multiple cache items by their keys.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $value = $cache->getMultiple(['key1', 'key2', 'key3']);
Like the get() method, you may pass a second argument to specifying the default value you wish to be returned if the item doesn't exist.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $cache->set('key1', 'value'); $value = $cache->getMultiple(['key1', 'badkey'], 'default');
The above example will output:
array(2) {
[key1] => string(5) "value",
[badkey] => string(7) "default",
}
Storing items in the cache
Cache::set()
The set() method store items in the cache.
If the storage time is not passed to the set() method, the item will be stored using the cache instance's default TTL (by defaults 3600 secondes).
Instead of passing the number of seconds as an integer, you may also pass a DateInterval instance representing the desired expiration time of the cached item.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $cache->set('key', 'value'); $cache->set('key', 'value', 28800); $cache->set('key', 'value', new \DateInterval('PT8H'))
Cache::setMultiple()
setMultiple() is similar to set(), but instead of a single key/value item, it works on multiple items. The expiration time applies to all the items at once.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $cache->setMultiple([ 'key1' => 'value1', 'key2' => 'value2', ]); $cache->set('key', 'value', 28800); $cache->set('key', 'value', new \DateInterval('PT8H'));
Removing items from the cache
Cache::delete()
You can delete items from the cache using the delete() method.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $cache->delete('key1');
Cache::deleteMultiple()
The deleteMultiple() method removes multiple cache items at once using a list of keys.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $cache->deleteMultiple(['key1', 'key2']);
Cache::clear()
You can clear the entire cache with the clear() method.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); $cache->clear();
Checking if an item exists
The has() method checks whether an item exists in the cache. If the item is not found, it will return false.
use LuFiipe\SimpleCache\Cache; $cache = new Cache(); if ($cache->has('key')) { // ... }
Tests
By default, during testing, the cache uses the OS temporary files directory.
To use a specific directory, copy the file phpunit.xml.dist to phpunit.xml and update the value of the CACHE_DIR variable.
Then, run:
php vendor/bin/phpunit
License
The MIT License (MIT). Please see License File for more information.
lufiipe/simplecache 适用场景与选型建议
lufiipe/simplecache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 03 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「filesystem」 「cache」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lufiipe/simplecache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lufiipe/simplecache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lufiipe/simplecache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
repository php library
A PHP class providing static methods for reading, writing, copying, moving, and deleting files and directories, MIME type detection, image size detection, and file permission management
The flysystem adapter for yandex disk rest api.
A sleek PHP wrapper around rclone with Laravel-style fluent API syntax
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-28