├── LICENSE ├── README.md ├── composer.json └── src ├── CacheForget.php ├── CacheUserProvider.php ├── ServiceProvider.php ├── UserObserver.php └── config.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) YangJiSen <381722452@qq.com> 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### 更新日志 2 | * v3.2.0 3 | - 修改为支持 laravel 6.0以上版本 4 | * v3.1.0 5 | - 支持 Laravel10 6 | * v3.0.0 7 | - 增加模型需要实现 Illuminate\Contracts\Auth\Authenticatable 接口 8 | * v2.1.0 9 | - 修复 Undefined variable: model 错误 #2 10 | 11 | ### 安装 12 | 13 | ```shell 14 | composer require "yangjisen/laravel-cache-provider" 15 | ``` 16 | 17 | ### Laravel 应用 18 | 19 | * 在 `config/app.php` 注册 ServiceProvider (Laravel 5.5 + 无需手动注册) 20 | 21 | ```php 22 | 'providers' => [ 23 | /* 24 | * Package Service Providers... 25 | */ 26 | YangJiSen\CacheUserProvider\ServiceProvider::class, 27 | ] 28 | ``` 29 | 30 | * 创建配置文件: 31 | 32 | ```shell 33 | php artisan vendor:publish --provider="YangJiSen\CacheUserProvider\ServiceProvider" 34 | ``` 35 | 36 | * 配置文件说明 37 | ```php 38 | env('CACHE_USER_TTL', 3600), 52 | 53 | /* 54 | |-------------------------------------------------------------------------- 55 | | 缓存的保存方式 56 | |-------------------------------------------------------------------------- 57 | | 58 | | single: 所有的保存为一个缓存键值 59 | | every: 按照单个用户进行缓存 60 | | 61 | */ 62 | 'cache_channel' => env('CACHE_USER_CHANNEL', 'every'), 63 | 64 | /* 65 | |-------------------------------------------------------------------------- 66 | | 渴望加载的关联模型 67 | |-------------------------------------------------------------------------- 68 | | 69 | | String: a,b,c 70 | | 71 | | 使用关联加载时,关联的数据仅在第一次查询时加载,缓存后不会自动进行更新,需要自行实现关联更新时删除缓存数据 72 | | 73 | | @param \Illuminate\Database\Eloquent\Model $user 74 | | 调用删除方法: YangJiSen\CacheUserProvider\CacheForget::CacheForget($user); 75 | | 76 | */ 77 | 'model_with' => env('CACHE_USER_MODEL_WITH'), 78 | ]; 79 | ``` 80 | 81 | * 将配置文件 `config/auth.php` 中 将授权提供者的驱动修改为 `cache` 即可 82 | 83 | ```php 84 | 'providers' => [ 85 | 'users' => [ 86 | 'driver' => 'cache', 87 | 'model' => App\User::class, 88 | ], 89 | ] 90 | ``` 91 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yangjisen/laravel-cache-provider", 3 | "license": "MIT", 4 | "keywords": ["laravel", "cache", "auth", "guard"], 5 | "authors": [ 6 | { 7 | "name": "Yang Ji Sen", 8 | "email": "381722452@qq.com" 9 | } 10 | ], 11 | "require": { 12 | "laravel/framework": ">=6.0" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "YangJiSen\\CacheUserProvider\\": "src/" 17 | } 18 | }, 19 | "extra": { 20 | "laravel": { 21 | "providers": [ 22 | "YangJiSen\\CacheUserProvider\\ServiceProvider" 23 | ] 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/CacheForget.php: -------------------------------------------------------------------------------- 1 | getAuthIdentifierName() ?? 'every'; 21 | 22 | $key = (config('cache-user.cache_channel') === 'every') 23 | ? "CacheUserProvider:{$model}:{$second}:{$user->{$user->getAuthIdentifierName()}}" 24 | : "CacheUserProvider:{$model}:single"; 25 | 26 | 27 | return Cache::forget($key); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/CacheUserProvider.php: -------------------------------------------------------------------------------- 1 | _ttl = $ttl; 24 | parent::__construct($hasher, $model); 25 | } 26 | 27 | /** 28 | * Get a new query builder for the model instance. 29 | * 30 | * @param string|null $identifier 31 | * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null 32 | * @throws \Exception 33 | */ 34 | protected function newModelQuery($identifier = null) 35 | { 36 | 37 | if (is_null($identifier)) return parent::newModelQuery(); 38 | 39 | $model = $this->createModel(); 40 | if(!($model instanceof Authenticatable)) { 41 | throw new \Exception('model unrealized authenticatable'); 42 | } 43 | 44 | $second = $model->getAuthIdentifierName() ?? 'every'; 45 | 46 | $key = (config('cache-user.cache_channel') === 'every') 47 | ? "CacheUserProvider:".class_basename($model).":{$second}:{$identifier}" 48 | : 'CacheUserProvider:'.class_basename($model).':single'; 49 | 50 | return Cache::remember($key, $this->_ttl, 51 | function () use ($model, $identifier) { 52 | 53 | $with = collect(explode(',', config('cache-user.model_with'))) 54 | ->filter()->toArray(); 55 | 56 | $query = $model->when($with, function ($query) use ($with) { 57 | return $query->with($with); 58 | }); 59 | 60 | return (config('cache-user.cache_channel') === 'every' && $identifier) 61 | ? $query->where($model->getAuthIdentifierName(), $identifier)->first() 62 | : $query->all(); 63 | }); 64 | 65 | } 66 | 67 | /** 68 | * Retrieve a user by their unique identifier. 69 | * 70 | * @param mixed $identifier 71 | * @return \Illuminate\Database\Eloquent\Model|null 72 | */ 73 | public function retrieveById($identifier) 74 | { 75 | return (config('cache-user.cache_channel') === 'every') 76 | ? $this->newModelQuery($identifier) 77 | : $this->newModelQuery($identifier) 78 | ->firstWhere($this->createModel()->getAuthIdentifierName(), $identifier); 79 | } 80 | 81 | /** 82 | * Retrieve a user by their unique identifier and 'remember me" token. 83 | * 84 | * @param mixed $identifier 85 | * @param string $token 86 | * @return \Illuminate\Database\Eloquent\Model|null 87 | */ 88 | public function retrieveByToken($identifier, $token) 89 | { 90 | $retrievedModel = (config('cache-user.cache_channel') === 'every') 91 | ? $this->newModelQuery($identifier) 92 | : $this->newModelQuery($identifier) 93 | ->firstWhere($this->createModel()->getAuthIdentifierName(), $identifier); 94 | 95 | if (!$retrievedModel) return null; 96 | 97 | $rememberToken = $retrievedModel->getRememberToken(); 98 | 99 | return $rememberToken && hash_equals($rememberToken, $token) 100 | ? $retrievedModel : null; 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | mergeConfigFrom(__DIR__.'/config.php','cache-user'); 19 | } 20 | 21 | /** 22 | * Bootstrap services. 23 | * 24 | * @return void 25 | */ 26 | public function boot() 27 | { 28 | if ($this->app->runningInConsole()) { 29 | $this->registerPublishing(); 30 | } 31 | 32 | Auth::provider('cache',function($app, $config) { 33 | $config['model']::observe(UserObserver::class); 34 | return new CacheUserProvider( 35 | $app['hash'], 36 | $config['model'], 37 | config('cache-user.cache_ttl', 3600) 38 | ); 39 | }); 40 | } 41 | 42 | /** 43 | * Register the package's publishable resources. 44 | * 45 | * @return void 46 | */ 47 | protected function registerPublishing() 48 | { 49 | $this->publishes([ 50 | __DIR__.'/config.php' => config_path('cache-user.php'), 51 | ], 'cache-user'); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/UserObserver.php: -------------------------------------------------------------------------------- 1 | env('CACHE_USER_TTL', 3600), 15 | 16 | /* 17 | |-------------------------------------------------------------------------- 18 | | 缓存的保存方式 19 | |-------------------------------------------------------------------------- 20 | | 21 | | single: 所有的保存为一个缓存键值 22 | | every: 按照单个用户进行缓存 23 | | 24 | */ 25 | 'cache_channel' => env('CACHE_USER_CHANNEL', 'every'), 26 | 27 | /* 28 | |-------------------------------------------------------------------------- 29 | | 渴望加载的关联模型 30 | |-------------------------------------------------------------------------- 31 | | 32 | | String: a,b,c 33 | | 34 | | 使用关联加载时注意: 关联的数据仅在第一次查询时加载, 关联数据更新后需要自行删除缓存数据 35 | | 36 | | @param \Illuminate\Database\Eloquent\Model $user 37 | | @method YangJiSen\CacheUserProvider\CacheForget::CacheForget($user); 38 | | 39 | */ 40 | 'model_with' => env('CACHE_USER_MODEL_WITH'), 41 | ]; 42 | --------------------------------------------------------------------------------