Skip to content

Commit

Permalink
Merge pull request #1934 from HDInnovations/Cache-Auth-User
Browse files Browse the repository at this point in the history
(Add) Auth User Caching
  • Loading branch information
HDVinnie authored Oct 5, 2021
2 parents 521c6b3 + 20cf51f commit 49162f1
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 21 deletions.
30 changes: 30 additions & 0 deletions app/Helpers/CacheUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @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);
});
}
}
7 changes: 5 additions & 2 deletions app/Observers/UserObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Auth;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -34,7 +35,8 @@ class AuthServiceProvider extends ServiceProvider
public function boot()
{
$this->registerPolicies();

//
Auth::provider('cache-user', function () {
return resolve(CacheUserProvider::class);
});
}
}
45 changes: 45 additions & 0 deletions app/Providers/CacheUserProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @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;
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 9 additions & 16 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

'providers' => [
'users' => [
'driver' => 'eloquent',
'driver' => 'cache-user',
'model' => App\Models\User::class,
],

Expand Down Expand Up @@ -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,

];

0 comments on commit 49162f1

Please sign in to comment.