- How do I debounce a Laravel notification to avoid spamming users?
- Use the `Debounce::notification()` method with your notification class and a delay (e.g., `Debounce::notification(new FileUploaded($file), 10)`). The package will ensure only one notification executes per debounce window, even if triggered multiple times.
- Does this package work with Laravel 10 or older versions?
- Yes, but CLI debouncing (Artisan commands) requires Laravel 11+. For older versions, focus on debouncing jobs and notifications. Check the [README](https://github.com/codeartbtw/laravel-debounce) for version-specific setup.
- What cache driver should I use for atomic locks?
- Redis is recommended for performance and reliability. Database caching works but may introduce latency. Ensure your cache driver supports atomic operations (e.g., `cache()->lock()`). Avoid file-based caching for production.
- How do I generate unique keys for debouncing?
- Use dynamic keys tied to the user or request context, like `user()->id` or `request()->ip()`. Static keys (e.g., `'send_notification'`) will debounce all occurrences globally. The package defaults to `user_id + request_ip` for notifications.
- Can I test debounce behavior without affecting production?
- Yes, disable debouncing entirely in testing by setting `LARAVEL_DEBOUNCE_ENABLED=false` in your `.env`. This bypasses all debounce logic while keeping your code unchanged. Useful for CI/CD pipelines or local testing.
- What happens if the cache is cleared while a job is debounced?
- Debounce state is lost, potentially causing duplicate executions. Mitigate this by using a persistent cache (e.g., Redis) or implementing a fallback (e.g., database-backed locks). The package doesn’t auto-recover, so plan for cache resilience.
- How do I debounce an Artisan command in Laravel 11+?
- Wrap your command logic in `Debounce::command()` with a delay, e.g., `Debounce::command(fn() => Artisan::call('queue:work'), 30)`. This works for CLI tasks like `php artisan your:command` and prevents rapid re-execution.
- Does this package integrate with Laravel Telescope?
- Yes, enable reporting via `config(['laravel-debounce.reporting' => true])` to log debounce occurrences (IP, user, timestamp). These appear in Telescope under the `laravel-debounce` tab, helping debug or audit debounced tasks.
- What are the performance implications of tracking occurrences?
- Minimal overhead—each occurrence adds a cache write. For high-frequency tasks (e.g., >1000/min), disable reporting (`'reporting' => false`) or use a faster cache driver like Redis. Benchmark under load if critical.
- Are there alternatives if I’m not using Laravel’s queue system?
- For non-queue systems, use Redis’s `SETNX` or a custom lock service. The debounce logic relies on Laravel’s `UniqueJobs` middleware, so alternatives require rebuilding atomic locks. The package’s core (cache + delays) is adaptable to other PHP frameworks.