diff --git a/app/Helpers/CacheUser.php b/app/Helpers/CacheUser.php new file mode 100644 index 0000000000..657c9c46aa --- /dev/null +++ b/app/Helpers/CacheUser.php @@ -0,0 +1,30 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +namespace App\Helpers; + +use App\Models\User; + +class CacheUser +{ + public static function user($id) + { + if (! $id || $id <= 0 || ! is_numeric($id)) { + return; + } + + return \cache()->remember('cachedUser.'.$id, 30, function () use ($id) { + return User::find($id); + }); + } +} diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index beb591fbc6..54be4756cf 100644 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -29,14 +29,17 @@ public function created(User $user) } /** - * Handle the User "updated" event. + * Handle the User "saved" event. * * + * @throws \Exception + * * @return void */ - public function updated(User $user) + public function saved(User $user) { //\cache()->put(\sprintf('user:%s', $user->passkey), $user); + \cache()->forget('cachedUser.'.$user->id); } /** diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 02f46d84ff..8fd5a69378 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -18,6 +18,8 @@ use App\Interfaces\ByteUnitsInterface; use App\Interfaces\WishInterface; use App\Models\Page; +use App\Models\User; +use App\Observers\UserObserver; use App\Repositories\WishRepository; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; @@ -53,7 +55,7 @@ public function register() public function boot() { // User Observer For Cache - //User::observe(UserObserver::class); + User::observe(UserObserver::class); // Torrent Observer For Cache //Torrent::observe(TorrentObserver::class); diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 6457d4eeb1..3bd18bada2 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -14,6 +14,7 @@ namespace App\Providers; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; +use Illuminate\Support\Facades\Auth; class AuthServiceProvider extends ServiceProvider { @@ -34,7 +35,8 @@ class AuthServiceProvider extends ServiceProvider public function boot() { $this->registerPolicies(); - - // + Auth::provider('cache-user', function () { + return resolve(CacheUserProvider::class); + }); } } diff --git a/app/Providers/CacheUserProvider.php b/app/Providers/CacheUserProvider.php new file mode 100644 index 0000000000..95fe16da25 --- /dev/null +++ b/app/Providers/CacheUserProvider.php @@ -0,0 +1,45 @@ + + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 + */ + +namespace App\Providers; + +use App\Helpers\CacheUser; +use App\Models\User; +use Illuminate\Auth\EloquentUserProvider; +use Illuminate\Contracts\Hashing\Hasher as HasherContract; + +class CacheUserProvider extends EloquentUserProvider +{ + public function __construct(HasherContract $hasher) + { + parent::__construct($hasher, User::class); + } + + public function retrieveById($identifier) + { + return CacheUser::user($identifier); + } + + public function retrieveByToken($identifier, $token) + { + $model = CacheUser::user($identifier); + + if (! $model) { + return null; + } + + $rememberToken = $model->getRememberToken(); + + return $rememberToken && hash_equals($rememberToken, $token) ? $model : null; + } +} diff --git a/config/app.php b/config/app.php index eb9bf85ddb..5b82868892 100644 --- a/config/app.php +++ b/config/app.php @@ -215,6 +215,7 @@ 'Broadcast' => Illuminate\Support\Facades\Broadcast::class, 'Bus' => Illuminate\Support\Facades\Bus::class, 'Cache' => Illuminate\Support\Facades\Cache::class, + 'CacheUser' => App\Helpers\CacheUser::class, 'Config' => Illuminate\Support\Facades\Config::class, 'Cookie' => Illuminate\Support\Facades\Cookie::class, 'CookieConsent' => BrianFaust\CookieConsent\Facades\CookieConsent::class, diff --git a/config/auth.php b/config/auth.php index 2e91324a8e..d4946f9961 100644 --- a/config/auth.php +++ b/config/auth.php @@ -67,7 +67,7 @@ 'providers' => [ 'users' => [ - 'driver' => 'eloquent', + 'driver' => 'cache-user', 'model' => App\Models\User::class, ], @@ -112,20 +112,13 @@ | */ - 'password_timeout' => 10800, - - 'TwoStepEnabled' => true, - - 'verificationEmailFrom' => env('MAIL_FROM_ADDRESS', env('MAIL_FROM_NAME')), - - 'verificationEmailFromName' => ' 2-Step Verification', - - 'TwoStepExceededCount' => 3, - - 'TwoStepExceededCountdownMinutes' => 60 * 24, - - 'TwoStepVerifiedLifetimeMinutes' => 6 * 60, - - 'TwoStepTimeResetBufferSeconds' => 6 * 60, + 'password_timeout' => 10800, + 'TwoStepEnabled' => true, + 'verificationEmailFrom' => env('MAIL_FROM_ADDRESS', env('MAIL_FROM_NAME')), + 'verificationEmailFromName' => ' 2-Step Verification', + 'TwoStepExceededCount' => 3, + 'TwoStepExceededCountdownMinutes' => 60 * 24, + 'TwoStepVerifiedLifetimeMinutes' => 6 * 60, + 'TwoStepTimeResetBufferSeconds' => 6 * 60, ];