Download the PHP package diegobvasco/laravel-auth-cache without Composer
On this page you can find all versions of the php package diegobvasco/laravel-auth-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download diegobvasco/laravel-auth-cache
More information about diegobvasco/laravel-auth-cache
Files in diegobvasco/laravel-auth-cache
Package laravel-auth-cache
Short Description This is my package laravel-auth-cache
License MIT
Homepage https://github.com/diegobvasco/laravel-auth-cache
Informations about the package laravel-auth-cache
Laravel Auth Cache
A Laravel authentication caching package that provides optimized user retrieval with automatic cache invalidation. Built with SOLID principles, dependency injection, and event-driven architecture for maintainable and testable code.
Installation
You can install the package via composer:
You can publish the config file with:
This is the contents of the published config file:
Usage
1. Configure the Auth Provider
Update your config/auth.php file to use the cachedEloquent provider:
2. Configure Cache Settings
Publish the config file and adjust the cache settings in config/auth-cache.php:
Configuration Options
- enabled: Enable or disable caching (default:
true) - ttl: Cache time-to-live in minutes (default:
60) - store: Specific cache store to use (default:
null- uses default cache store) - prefix: Cache key prefix (default:
'auth')
3. Clearing cache
Both the trait and the observer clear the cache only listen Eloquent model events updated and deleted.
Using trait
Add the HasCachedAuthProvider trait to your user model:
Using observer
Register the observer CachedAuthObserver to your user model:
The example use ObservedBy to register the observer but you can use traditional way in AppServiceProvider.
Per-Guard Configuration
You can override cache settings per-provider in config/auth.php by adding a cache key to the provider definition. These values are merged on top of the global config/auth-cache.php settings:
Custom Cache Store
Use a specific cache store for auth caching by setting the AUTH_CACHE_STORE environment variable:
How It Works
- Caching: When a user is retrieved via
retrieveById(), the provider caches the user record with the configured TTL. - Cache Key: Cache keys are generated as
{prefix}.{model_basename}.{id}(e.g.,auth.user.123). - Automatic Invalidation: When a model is updated or deleted, a
CacheInvalidationRequestedevent is dispatched. The registered listener automatically clears the corresponding cache entry. - Interface-Based Design: All components use interfaces and dependency injection, making them testable and extensible.
- Configurable: You can enable/disable caching, adjust TTL, use different cache stores, and customize the cache key prefix.
Architecture
This package is built using SOLID principles and Laravel best practices:
Interface-Based Design
All cache operations are defined by interfaces:
- CacheInterface - Core cache operations (remember, forget, isEnabled)
- CacheKeyGeneratorInterface - Strategy for generating cache keys
- CacheInvalidatorInterface - Handles cache invalidation
- CacheConfigurationInterface - Configuration access
Dependency Injection
All components are resolved through Laravel's service container, providing:
- Easy testing with mocks
- Ability to replace implementations
- Loose coupling between components
Event-Driven Cache Invalidation
- Events decouple cache clearing from models
- Listeners handle invalidation logic
- Multiple listeners can respond to cache events
Events
CacheInvalidationRequested
Dispatched when cache should be invalidated (on model update, delete, or manual request).
Event Properties:
model- The model instance being invalidatedidentifier- The model's identifier (ID)reason- Why invalidation occurred:updated,deleted, ormanual
Example: Listen to Invalidation Events
Extending
Custom Cache Key Generator
Create your own key generation strategy:
Custom Cache Invalidation
Implement your own invalidation logic:
Custom Configuration
Modify configuration at runtime:
Testing
The package includes comprehensive tests:
- Unit tests for all components
- Integration tests for provider, trait, and observer
- Architecture tests ensuring code quality
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Diego Vasconcelos
- All Contributors
License
The MIT License (MIT). Please see License File for more information.