PHP code example of diegobvasco / laravel-auth-cache

1. Go to this page and download the library: Download diegobvasco/laravel-auth-cache library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

diegobvasco / laravel-auth-cache example snippets


return [
    'cache' => [
        'enabled' => (bool) env('AUTH_CACHE_ENABLED', true),
        'ttl' => (int) env('AUTH_CACHE_TTL', 60),
        'store' => env('AUTH_CACHE_STORE'),
        'prefix' => env('AUTH_CACHE_PREFIX', 'auth'),
    ],
];

return [
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'cachedEloquent',
            'model' => App\Models\User::class,
        ],
    ],
];

return [
    'cache' => [
        'enabled' => env('AUTH_CACHE_ENABLED', true),
        'ttl' => env('AUTH_CACHE_TTL', 60),
        'store' => env('AUTH_CACHE_STORE', null),
        'prefix' => env('AUTH_CACHE_PREFIX', 'auth'),
    ],
];

use DiegoVasconcelos\AuthCache\Concerns\HasCachedAuthProvider;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasCachedAuthProvider;

    // ...
}

use DiegoVasconcelos\AuthCache\Observers\CachedAuthObserver;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;

#[ObservedBy(CachedAuthObserver::class)]
class User extends Authenticatable
{
    // ...
}

'providers' => [
    'users' => [
        'driver' => 'cachedEloquent',
        'model' => App\Models\User::class,
        'cache' => [
            'ttl' => 120, // Override to 120 minutes for this provider
        ],
    ],
    'api_users' => [
        'driver' => 'cachedEloquent',
        'model' => App\Models\ApiUser::class,
        'cache' => [
            'enabled' => false, // Disable cache for this provider
        ],
    ],
],

use DiegoVasconcelos\AuthCache\Events\CacheInvalidationRequested;

Event::listen(CacheInvalidationRequested::class, function ($event) {
    Log::info("Cache cleared for {$event->reason}: {$event->identifier}");
});

use DiegoVasconcelos\AuthCache\Cache\Contracts\CacheKeyGeneratorInterface;

class CustomKeyGenerator implements CacheKeyGeneratorInterface
{
    public function generate(string $modelClass, mixed $identifier): string
    {
        return "custom.{$modelClass}.{$identifier}";
    }
}

// Register in AppServiceProvider
app()->bind(
    CacheKeyGeneratorInterface::class,
    CustomKeyGenerator::class
);

use DiegoVasconcelos\AuthCache\Cache\Contracts\CacheInvalidatorInterface;

class CustomInvalidator implements CacheInvalidatorInterface
{
    public function invalidate(object $model, mixed $identifier): void
    {
        // Your custom logic (e.g. log, broadcast, etc.)
        Cache::forget("custom.key.{$identifier}");
    }
}

app()->bind(
    CacheInvalidatorInterface::class,
    CustomInvalidator::class
);

use DiegoVasconcelos\AuthCache\Cache\Contracts\CacheConfigurationInterface;

app()->afterResolving(CacheConfigurationInterface::class, function ($config) {
    // Customize based on environment, user roles, etc.
});
bash
php artisan vendor:publish --tag="laravel-auth-cache-config"