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/ */
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
{
// ...
}
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.
});