| Install | |
|---|---|
composer require hobbiot/cacheable-auth-user |
Cacheable Auth::user() for Laravel 5.3 or 5.4.
Composer
composer require hobbiot/cacheable-auth-user
In your config/app.php add HobbIoT\Auth\CacheableAuthUserServiceProvider::class to the end of the providers array:
'providers' => [
...
HobbIoT\Auth\CacheableAuthUserServiceProvider::class,
...
],
In your config/auth.php change User Providers' driver. You can now use "cacheableEloquent".
...
'providers' => [
'users' => [
'driver' => 'cacheableEloquent',
'model' => App\User::class,
],
// e.g.
'admin' => [
'driver' => 'cacheableEloquent',
'model' => App\Administrator::class,
],
],
...
],
Administrator::class needs to extend Authenticatable (Illuminate\Foundation\Auth\User) and use trait Notifiable (Illuminate\Notifications\Notifiable), just like App\User::class.
The cache is valid for 60 minutes.
The cache Key is ModelClassName_By_Id_id and ModelClassName_By_Id_Token_id.
Using Eloquent updated event listener to clear cache, need to use model->save(). When user update his name in profile page,
fire updated event automatically, (listen event and) clear cache. After that reload from resources (database).
Laravel Official Documentation said,
Note: When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.