biigle / laravel-image-cache
Requires
- illuminate/console: ^6.0|^7.0|^8.0
- illuminate/filesystem: ^6.0|^7.0|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
- league/flysystem: ^1.0
Requires (Dev)
- laravel/laravel: ^6.0|^7.0|^8.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^9.0
README
在Laravel或Lumen中从本地文件系统、云存储或公共web服务器获取和缓存文件。
文件缓存专门设计用于与多个并行队列工作进程进行并发处理。
安装
composer require biigle/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));