hobbiot/cacheable-auth-user

Cache Auth::user() for Laravel 5.3. This package add new driver to laravel.

1.0.1 2017-06-04 04:17 UTC

This package is not auto-updated.

Last update: 2024-04-12 20:29:37 UTC


README

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.