Simple Way Of Caching Laravel Eloquent Query Results
This package is auto-updated.
Last update: 2021-01-11 22:03:52 UTC
This is a package to cache your eloquent query results.
Require this package in your composer.json and run composer update (or run
composer require farzinft/laravel-model-caching:dev-master directly):
You need to publish the config file for this package. This will add the file
config/model-caching.php, where you can configure this package.
$ php artisan vendor:publish --provider="Fthi\ModelCaching\ServiceProvider"
You just need add your model to model caching construct and write your eloquent queries in a normal way. this package uses Redis for caching results;
suppose you have a customer model and you wrote a query like this:
"$customers = Customer::with('books')->where(['status' => 1])->get()"
and you want cache result 50 seconds, change cache_expire_time (in second) in model-caching.php or inside your model define a public variable named $cacheExpireTime, then:
"$customers = (new ModelCaching(new Customer))->with('books')->where(['status' => 1])->get()"
when cache time expired results back from database.
just tested on laravel 5.x, maybe it works on other versions, test it :)