Laravel - Cache - Writing The Driver
To create our custom cache driver, we first need to implement the Illuminate\Contracts\Cache\Store
contract. So, a MongoDB cache implementation might look something like this:
We just need to implement each of these methods using a MongoDB connection. For an example of how to implement each of these methods, take a look at the Illuminate\Cache\MemcachedStore
in the Laravel framework source code. Once our implementation is complete, we can finish our custom driver registration by calling the Cache
facade's extend
method:
Cache::extend('mongo', function ($app) {
return Cache::repository(new MongoStore);
});
If you're wondering where to put your custom cache driver code, you could create anExtensions
namespace within yourapp
directory. However, keep in mind that Laravel does not have a rigid application structure and you are free to organize your application according to your preferences.