Skip to content

Instantly share code, notes, and snippets.

@tanthammar
Forked from paulund/AppServiceProvider.php
Created March 16, 2023 18:10
Show Gist options
  • Select an option

  • Save tanthammar/888a2cdee62220ee29a211556b87726c to your computer and use it in GitHub Desktop.

Select an option

Save tanthammar/888a2cdee62220ee29a211556b87726c to your computer and use it in GitHub Desktop.

Revisions

  1. @paulund paulund revised this gist Sep 12, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CacheUserProvider.php
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,6 @@ public function __construct(HasherContract $hasher)
    */
    public function retrieveById($identifier)
    {
    return Cache::get("user.$identifier") ?? parent::retrieveById($identifier;
    return Cache::get("user.$identifier") ?? parent::retrieveById($identifier);
    }
    }
  2. @paulund paulund revised this gist Sep 23, 2019. No changes.
  3. @paulund paulund revised this gist Jun 9, 2018. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions UserObserver.php
    Original file line number Diff line number Diff line change
    @@ -34,9 +34,6 @@ public function restored(User $user)
    }

    /**
    * Listen to the User retrieved event. If this happens, the
    * user isn't cached, so we will cache it for next time.
    *
    * @param User $user
    */
    public function retrieved(User $user)
  4. @paulund paulund revised this gist Jun 3, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions auth.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // Config/auth.php
    'providers' => [
    'users' => [
    'driver' => 'cache-user',
  5. @paulund paulund revised this gist Jun 3, 2018. 4 changed files with 114 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions AppServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?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()
    {
    //
    }
    }
    32 changes: 32 additions & 0 deletions AuthServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <?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);
    });
    }
    }
    46 changes: 46 additions & 0 deletions UserObserver.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    <?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);
    }

    /**
    * Listen to the User retrieved event. If this happens, the
    * user isn't cached, so we will cache it for next time.
    *
    * @param User $user
    */
    public function retrieved(User $user)
    {
    Cache::add("user.{$user->id}", $user, 60);
    }
    }
    6 changes: 6 additions & 0 deletions auth.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    'providers' => [
    'users' => [
    'driver' => 'cache-user',
    'model' => App\Models\User::class,
    ]
    ]
  6. @paulund paulund created this gist Jun 3, 2018.
    33 changes: 33 additions & 0 deletions CacheUserProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?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;
    }
    }