volkerschulz / file-cache
PHP 文件缓存处理器
1.2.0
2023-11-01 23:30 UTC
Requires
- php: >=8.0.0
README
PHP 文件缓存处理器
安装
安装 FileCache 的推荐方式是通过 Composer。
composer require volkerschulz/file-cache
用法
最小化
use volkerschulz\FileCache; // Create instance $filecache = new FileCache('testfiles/cache.png'); // Respond to request $filecache->respond();
带有自定义头部
use volkerschulz\FileCache; // Create instance for file 'testfiles/utf.json' // with default options $filecache = new FileCache('testfiles/utf.json'); // Set additional custom headers (optional) $filecache->addHeader('Content-Type', 'application/json; charset=UTF8'); // Respond to request $filecache->respond();
带有自定义选项
use volkerschulz\FileCache; $options = [ 'use_checksum' => true, 'hash_algo' => 'xxh128', 'fresh_for' => 300 ]; // Create instance for file 'testfiles/utf.json' // with custom options $filecache = new FileCache('testfiles/utf.json', $options); // Set additional custom headers (optional) $filecache->addHeader('Content-Type', 'application/json; charset=UTF8'); // Respond to request $filecache->respond();
选项
use_filetime 布尔型
默认值: true - 在创建 ETag 时是否使用文件的最后修改时间。
use_filesize 布尔型
默认值: true - 在创建 ETag 时是否使用文件的大小。
use_checksum 布尔型
默认值: false - 在创建 ETag 时是否使用文件的哈希值。根据文件大小,这可能会消耗大量的 CPU 和 I/O 时间。
至少需要设置 use_filetime、use_filesize 或 use_checksum 中的一个为 true 才能创建或比较 ETag。
hash_algo 字符串
默认值: 'crc32' - 当 use_checksum 为 true 时,使用哪种哈希算法。必须由当前的 PHP 版本支持。
use_etag 布尔型
默认值: true - 是否始终使用 ETag。强烈建议将此选项设置为 true。
fresh_for 整数
默认值: 0 - 从现在起多少秒内资源保证不会过时,并且不应该重新验证。
add_missing_headers 布尔型
默认值: true - 如果设置为 true,如果可能,将自动添加缺少的头部(目前只有 Content-Type
)。
安全
如果您在此软件包中发现安全漏洞,请发送电子邮件至 security@volkerschulz.de。所有安全漏洞都将得到及时处理。请勿在修复发布之前公开安全相关的问题。
许可
此软件包在 MIT 许可证(MIT)下提供。有关更多信息,请参阅 许可证文件。