Posts Learn Components Snippets Categories Tags Tools About
/

How to enable cache for s3 Filesystem Driver (Disk Caching)

Learn how to enable caching for s3 filesystem to enhance the performance of your Laravel apps

Created on Feb 01, 2022

184 views

In Laravel, you can enable s3 or other filesystem driver cache and by doing so you will be able to increase the performance of your website. The first step for this is to install the cache adapter using composer.

Install Laravel CachedAdapter


Do run the command below from your terminal.
composer require league/flysystem-cached-adapter "~1.0"

Enable the Cache Driver


And now to enable the cache driver you can specify the 'cache' key and specify the cache details. From the example docs it's using the "Memcached" store and it's set to 600 seconds to expire and has a prefix of "cache-prefix"
's3' => [
    'driver' => 's3',

    // Other Disk Options...

    'cache' => [
        'store' => 'memcached',
        'expire' => 600, // 10 mins
        'prefix' => 'cache-prefix',
    ],
],
But if you are using "redis" and you want it to cache for 1 hour then you can specify it like below.
's3' => [
    'driver' => 's3',
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('AWS_DEFAULT_REGION'),
    'bucket' => env('AWS_BUCKET'),
    'url' => env('AWS_URL'),
    'endpoint' => env('AWS_ENDPOINT'),
    'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),

    'cache' => [
        'store' => 'redis',
        'expire' => 3600, // 1 hour
        'prefix' => 's3-cache',
    ],
],
And now you will get a slight performance boost when accessing items from the disk.

Other Reads

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)