Laravel 使用 Dropbox 作为磁盘的图像缓存
Laravel image cache using Dropbox as disk
我正在使用这个包 (https://github.com/spatie/flysystem-dropbox) 来存储和从 Dropbox 获取图像。
这工作正常,但每次刷新页面时都必须加载图像。我想知道您是否知道任何适用于这种情况的图像缓存解决方案,如果可以请提供一个最小的工作示例。
谢谢。
解决此问题的一种方法是创建您自己的缓存系统。如果您的本地文件系统中不存在图像,请从 Dropbox 中提取它,然后将其保存到本地文件系统并提供服务。如果它已经存在于本地文件系统中,只需从本地文件系统提供它。
1 条路线
从他们自己的路线提供图像。
Route::get('images/{filename}', [
'uses' => 'ImageController@getImage'
]);
2 个控制器
检查本地文件系统,看文件是否已经存在,否则从dropbox中拉取并存储在本地文件系统中。
<?php
namespace App\Http\Controllers;
class ImageController extends Controller
public function __construct()
{
parent::__construct();
}
public function getImage($filename)
{
// If the file doesn't exist
if(!file_exists('/path/to/' . $filename)) {
// 1. Get the image from dropbox
// 2. Save the image to local storage
}
// 3. Serve the image from local storage
}
}
我正在使用这个包 (https://github.com/spatie/flysystem-dropbox) 来存储和从 Dropbox 获取图像。
这工作正常,但每次刷新页面时都必须加载图像。我想知道您是否知道任何适用于这种情况的图像缓存解决方案,如果可以请提供一个最小的工作示例。
谢谢。
解决此问题的一种方法是创建您自己的缓存系统。如果您的本地文件系统中不存在图像,请从 Dropbox 中提取它,然后将其保存到本地文件系统并提供服务。如果它已经存在于本地文件系统中,只需从本地文件系统提供它。
1 条路线
从他们自己的路线提供图像。
Route::get('images/{filename}', [
'uses' => 'ImageController@getImage'
]);
2 个控制器
检查本地文件系统,看文件是否已经存在,否则从dropbox中拉取并存储在本地文件系统中。
<?php
namespace App\Http\Controllers;
class ImageController extends Controller
public function __construct()
{
parent::__construct();
}
public function getImage($filename)
{
// If the file doesn't exist
if(!file_exists('/path/to/' . $filename)) {
// 1. Get the image from dropbox
// 2. Save the image to local storage
}
// 3. Serve the image from local storage
}
}