Skip to content

itime/cacheable-auth-user

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cacheable-auth-user

Latest Stable Version Total Downloads Latest Unstable Version License

Cacheable Auth::user() for Laravel 5.3 or 5.4.

Installation

Composer

composer require hobbiot/cacheable-auth-user

Laravel

app.php

In your config/app.php add HobbIoT\Auth\CacheableAuthUserServiceProvider::class to the end of the providers array:

'providers' => [
    ...
        HobbIoT\Auth\CacheableAuthUserServiceProvider::class,
    ...
],

auth.php

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.

Supplementary Explanation

  • 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.

About

Cacheable Auth::user() for Laravel 5.3

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%