arkonsoft / ps-file-cache
简单的文件缓存库
v1.0.0
2024-08-13 12:20 UTC
Requires
- php: >= 7.0
This package is auto-updated.
Last update: 2024-09-23 10:54:13 UTC
README
PHP的简单文件缓存
安装
composer require arkonsoft/ps-file-cache
使用
创建缓存的新实例
use Arkonsoft\PsModule\FileCache;
...
$cache = new FileCache(__DIR__ . '/cache/');
在缓存中存储一个值
$cache->store('key', 'value');
从缓存中获取一个值
$value = $cache->retrieve('key');
检索方法也将检查密钥是否已过期。您可以通过将第二个参数传递为false来关闭此功能
$value = $cache->retrieve('key', false);
重要
store()方法使用json_encode()序列化数据。您可以存储除了"resource"类型之外的所有数据。如果您尝试存储它,将抛出异常。
其他方法
有效期
基本有效期设置为3600秒(1小时)。
您可以在构造函数中更改它
$cache = new FileCache('path', 6000);
或者稍后使用
$cache->setLifetime(6000);
IsStored
检查密钥是否存储在缓存中且不为空
$cache->isStored('key');
IsExpired
检查密钥是否存储在缓存中且已过期
$cache->isExpired('key');