I came across this library https://github.com/GeneaLabs/laravel-model-caching and trying to use it in a project of mine.
Firstly, I've added the following line to my .env
:
MODEL_CACHE_STORE=file
I am using file cache store to test, in production I will use redis.
Then I've created app/CacheableModel.php
with the following:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
abstract class CacheableModel extends Model
{
use Cachable;
}
and then I've updated all my models (except for app/User.php
) like this example:
<?php
namespace App;
class Company extends CacheableModel
{
...
}
and I tested it using a test route like this:
Route::get('test', function() {
$testUser = \App\User::where('email', 'test@domain.com')
->with('company.users', 'company.subscription')
->first();
dump($testUser->toArray());
});
When I visit test route, I can see the data loaded and dumped. If I refresh the page, it doesn't seem to be caching to be caching the above query.
I verified that it's not caching my eloquent queries by looking at the query log (using laravel telescope) as well as looking at storage/framework/cache/data
- it's empty.
Any ideas what might be wrong?
Is this not caching because app/User.php
model doesn't use the CacheableModel
base class and the test query starts from the user model?