Laravel - Cache - Registering The Driver
To register the custom cache driver with Laravel, we will use the extend
method on the Cache
facade. Since other service providers may attempt to read cached values within their boot
method, we will register our custom driver within a booting
callback. By using the booting
callback, we can ensure that the custom driver is registered just before the boot
method is called on our application's service providers but after the register
method is called on all of the service providers. We will register our booting
callback within the register
method of our application's App\Providers\AppServiceProvider
class:
app->booting(function () {
Cache::extend('mongo', function ($app) {
return Cache::repository(new MongoStore);
});
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
The first argument passed to the extend
method is the name of the driver. This will correspond to your driver
option in the config/cache.php
configuration file. The second argument is a closure that should return an Illuminate\Cache\Repository
instance. The closure will be passed an $app
instance, which is an instance of the service container.
Once your extension is registered, update your config/cache.php
configuration file's driver
option to the name of your extension.