代码之家  ›  专栏  ›  技术社区  ›  Gjaa

使用Dropbox作为磁盘的Laravel图像缓存

  •  0
  • Gjaa  · 技术社区  · 5 年前

    我用这个包裹( https://github.com/spatie/flysystem-dropbox )从Dropbox中存储和获取图像。

    这很好,但是每次刷新页面时都必须加载图像。我想知道您是否知道在这种情况下可以使用的任何图像缓存解决方案,如果可以,请提供一个最小的工作示例。

    谢谢。

    0 回复  |  直到 5 年前
        1
  •  1
  •   Jeemusu    5 年前

    解决这个问题的一种方法是创建自己的缓存系统。如果本地文件系统上不存在该图像,请将其从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
        }
    }