├── .gitignore ├── README.md ├── composer.json └── src └── Auth ├── CacheableAuthUserServiceProvider.php └── CacheableEloquentUserProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cacheable-auth-user 2 | [![Latest Stable Version](https://poser.pugx.org/hobbiot/cacheable-auth-user/v/stable)](https://packagist.org/packages/hobbiot/cacheable-auth-user) [![Total Downloads](https://poser.pugx.org/hobbiot/cacheable-auth-user/downloads)](https://packagist.org/packages/hobbiot/cacheable-auth-user) [![Latest Unstable Version](https://poser.pugx.org/hobbiot/cacheable-auth-user/v/unstable)](https://packagist.org/packages/hobbiot/cacheable-auth-user) [![License](https://poser.pugx.org/hobbiot/cacheable-auth-user/license)](https://packagist.org/packages/hobbiot/cacheable-auth-user) 3 | 4 | Cacheable Auth::user() for Laravel 5.3 or 5.4. 5 | 6 | ## Installation 7 | Composer 8 | ```terminal 9 | composer require hobbiot/cacheable-auth-user 10 | ``` 11 | 12 | ### Laravel 13 | #### app.php 14 | In your `config/app.php` add `HobbIoT\Auth\CacheableAuthUserServiceProvider::class` to the end of the `providers` array: 15 | 16 | ```php 17 | 'providers' => [ 18 | ... 19 | HobbIoT\Auth\CacheableAuthUserServiceProvider::class, 20 | ... 21 | ], 22 | ``` 23 | 24 | #### auth.php 25 | In your `config/auth.php` change User Providers' driver. You can now use "__cacheableEloquent__". 26 | 27 | ```php 28 | ... 29 | 'providers' => [ 30 | 'users' => [ 31 | 'driver' => 'cacheableEloquent', 32 | 'model' => App\User::class, 33 | ], 34 | // e.g. 35 | 'admin' => [ 36 | 'driver' => 'cacheableEloquent', 37 | 'model' => App\Administrator::class, 38 | ], 39 | ], 40 | ... 41 | ], 42 | ``` 43 | Administrator::class needs to extend Authenticatable (Illuminate\\Foundation\\Auth\\User) and use trait Notifiable (Illuminate\\Notifications\\Notifiable), just like App\\User::class. 44 | 45 | ### Supplementary Explanation 46 | * The cache is valid for 60 minutes. 47 | * The cache Key is _ModelClassName_\_By\_Id\__id_ and _ModelClassName_\_By\_Id\_Token\__id_. 48 | * Using Eloquent _updated_ event listener to clear cache, need to use `model->save()`. When user update his name in profile page, 49 | fire _updated_ event automatically, (listen event and) clear cache. After that reload from resources (database). 50 | 51 | Laravel Official Documentation said, 52 | >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. 53 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hobbiot/cacheable-auth-user", 3 | "description": "Cache Auth::user() for Laravel 5.3. This package add new driver to laravel.", 4 | "keywords": ["laravel", "auth", "cache"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "HobbIoT", 9 | "email": "dev@hobbiot.co.jp", 10 | "homepage": "https://www.hobbiot.co.jp/" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.6.4", 15 | "laravel/framework": "5.3.* | 5.4.*" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "HobbIoT\\": "src/" 20 | } 21 | }, 22 | "minimum-stability": "stable" 23 | } 24 | -------------------------------------------------------------------------------- /src/Auth/CacheableAuthUserServiceProvider.php: -------------------------------------------------------------------------------- 1 | app['auth']->provider('cacheableEloquent', 18 | function($app, $config) { 19 | $config['model']::updated(function ($model) { 20 | CacheableEloquentUserProvider::clearCache($model); 21 | }); 22 | return new CacheableEloquentUserProvider($app['hash'], $config['model']); 23 | } 24 | ); 25 | } 26 | 27 | /** 28 | * Register the application services. 29 | * 30 | * @return void 31 | */ 32 | public function register() 33 | { 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/Auth/CacheableEloquentUserProvider.php: -------------------------------------------------------------------------------- 1 | remember($this->getModel() . '_By_Id_' . $identifier, 60, 19 | function() use ($identifier) { 20 | return $this->createModel()->newQuery()->find($identifier); 21 | } 22 | ); 23 | } 24 | 25 | /** 26 | * Retrieve a user by their unique identifier and "remember me" token. 27 | * - override - 28 | * with using cache. 29 | * 30 | * @param mixed $identifier 31 | * @param string $token 32 | * @return \Illuminate\Contracts\Auth\Authenticatable|null 33 | */ 34 | public function retrieveByToken($identifier, $token) 35 | { 36 | $model = $this->createModel(); 37 | 38 | return cache()->remember($this->getModel() . '_By_Id_Token_' . $identifier, 60, 39 | function() use ($model, $identifier, $token) { 40 | return $model->newQuery() 41 | ->where($model->getAuthIdentifierName(), $identifier) 42 | ->where($model->getRememberTokenName(), $token) 43 | ->first(); 44 | } 45 | ); 46 | } 47 | 48 | // キャッシュクリア 49 | public static function clearCache($model) 50 | { 51 | cache()->forget(get_class($model) . '_By_Id_' . $model->id); 52 | cache()->forget(get_class($model) . '_By_Id_Token_' . $model->id); 53 | } 54 | 55 | } 56 | --------------------------------------------------------------------------------