Last active
March 16, 2023 18:10
-
-
Save paulund/9b13d504fb5fd05af99ecb0378418d98 to your computer and use it in GitHub Desktop.
Laravel Cache Auth::user(). Learn how to cache the logged in user at https://paulund.co.uk/laravel-cache-authuser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Models\User; | |
use App\Observers\UserObserver; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
User::observe(UserObserver::class); | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Config/auth.php | |
'providers' => [ | |
'users' => [ | |
'driver' => 'cache-user', | |
'model' => App\Models\User::class, | |
] | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Auth\CacheUserProvider; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; | |
class AuthServiceProvider extends ServiceProvider | |
{ | |
/** | |
* The policy mappings for the application. | |
* | |
* @var array | |
*/ | |
protected $policies = [ | |
]; | |
/** | |
* Register any authentication / authorization services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// Caching user | |
Auth::provider('cache-user', function() { | |
return resolve(CacheUserProvider::class); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Auth; | |
use App\Models\User; | |
use Illuminate\Auth\EloquentUserProvider; | |
use Illuminate\Contracts\Hashing\Hasher as HasherContract; | |
use Illuminate\Support\Facades\Cache; | |
/** | |
* Class CacheUserProvider | |
* @package App\Auth | |
*/ | |
class CacheUserProvider extends EloquentUserProvider | |
{ | |
/** | |
* CacheUserProvider constructor. | |
* @param HasherContract $hasher | |
*/ | |
public function __construct(HasherContract $hasher) | |
{ | |
parent::__construct($hasher, User::class); | |
} | |
/** | |
* @param mixed $identifier | |
* @return \Illuminate\Contracts\Auth\Authenticatable|null | |
*/ | |
public function retrieveById($identifier) | |
{ | |
return Cache::get("user.$identifier") ?? parent::retrieveById($identifier); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Observers; | |
use Illuminate\Foundation\Auth\User; | |
use Illuminate\Support\Facades\Cache; | |
/** | |
* User observer | |
*/ | |
class UserObserver | |
{ | |
/** | |
* @param User $user | |
*/ | |
public function saved(User $user) | |
{ | |
Cache::put("user.{$user->id}", $user, 60); | |
} | |
/** | |
* @param User $user | |
*/ | |
public function deleted(User $user) | |
{ | |
Cache::forget("user.{$user->id}"); | |
} | |
/** | |
* @param User $user | |
*/ | |
public function restored(User $user) | |
{ | |
Cache::put("user.{$user->id}", $user, 60); | |
} | |
/** | |
* @param User $user | |
*/ | |
public function retrieved(User $user) | |
{ | |
Cache::add("user.{$user->id}", $user, 60); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CacheUserProvider
Line number 31 should be.