jackardios / laravel-file-cache
在Laravel中从本地文件系统、云存储或公共Web服务器获取和缓存文件
Requires
- php: ^8.0
- ext-curl: *
- guzzlehttp/guzzle: ^7.0
- illuminate/console: ^9.0 || ^10.0
- illuminate/filesystem: ^9.0 || ^10.0
- illuminate/support: ^9.0 || ^10.0
- symfony/finder: ^6.0
Requires (Dev)
- laravel/laravel: ^9.0 || ^10.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-30 02:05:32 UTC
README
在Laravel或Lumen中从本地文件系统、云存储或公共Web服务器获取和缓存文件。
文件缓存专为与多个并行队列工作者的并发处理使用而设计。
安装
composer require jackardios/laravel-file-cache
Laravel
Laravel会自动发现服务提供者和FileCache
外观。
Lumen
将以下内容添加到bootstrap/app.php
$app->register(Biigle\FileCache\FileCacheServiceProvider::class); $app->register(Illuminate\Filesystem\FilesystemServiceProvider::class);
要使用FileCache
外观,启用$app->withFacades()
并将以下内容添加到bootstrap/app.php
if (!class_exists(FileCache::class)) { class_alias(Biigle\FileCache\Facades\FileCache::class, 'FileCache'); }
不使用外观,文件缓存实例作为app('file-cache')
可用。
用法
查看FileCache
合同,了解文件缓存的公共API。示例
use FileCache; use Biigle\FileCache\GenericFile; // Implements Biigle\FileCache\Contracts\File. $file = new GenericFile('https://example.com/images/image.jpg'); FileCache::get($file, function ($file, $path) { // do stuff });
如果文件URL指定了不同于http
或https
的协议(例如mydisk://images/image.jpg
),文件缓存将在filesystems.disks
中配置的适当存储磁盘中查找文件。您不能将本地文件路径用作URL(例如/vol/images/image.jpg
)。相反,请配置一个具有local
驱动器的存储磁盘。
配置
文件缓存附带合理的默认配置。您可以在file-cache
命名空间或使用环境变量中覆盖它。
file-cache.max_file_size
默认:-1
(任何大小)环境:FILE_CACHE_MAX_FILE_SIZE
允许的缓存文件最大大小(以字节为单位)。设置为-1
以允许任何大小。
file-cache.max_age
默认:60
环境:FILE_CACHE_MAX_AGE
缓存中文件的最大年龄(以分钟为单位)。较旧的文件将被删除。
file-cache.max_size
默认:1E+9
(1 GB) 环境:FILE_CACHE_MAX_SIZE
文件缓存的最大大小(软限制)(以字节为单位)。如果缓存超过此大小,则删除旧文件。
file-cache.path
默认:'storage/framework/cache/files'
用于文件缓存的目录。
file-cache.timeout
默认:5.0
环境:FILE_CACHE_TIMEOUT
获取远程文件的读取超时时间(以秒为单位)。如果在超过此期间(或无法建立)没有数据传输,则缓存文件失败。
file-cache.prune_interval
默认 '*/5 * * * *'
(每五分钟一次)
计划任务删除文件缓存的时间间隔。
file-cache.mime_types
默认:[]
(允许所有类型)
允许的缓存文件MIME类型的数组。其他类型的文件缓存将失败。
清除
在您调用php artisan cache:clear
时清除文件缓存。
测试
FileCache
外观提供模拟器,便于测试。模拟器实际上不会获取和存储任何文件,而只是执行带有模拟文件路径的回调函数。
use FileCache; use Biigle\FileCache\GenericFile; FileCache::fake(); $file = new GenericFile('https://example.com/image.jpg'); $path = FileCache::get($file, function ($file, $path) { return $path; }); $this->assertFalse($this->app['files']->exists($path));