- How do I debounce a Laravel notification to prevent spamming users?
- Extend the `DebounceNotification` class or use the `debounce()` facade method. For example, wrap your notification dispatch in `debounce(Notification::class, 10, $userId)` to delay execution for 10 seconds if triggered repeatedly by the same user or IP. Ensure your notification uses `Queueable` for async processing.
- Does this package work with Laravel 10 for debouncing jobs?
- Yes, but CLI (Artisan command) debouncing requires Laravel 11+. For Laravel 10, focus on jobs and notifications. Verify your cache driver supports atomic locks (Redis is recommended). Jobs must implement `ShouldQueue` to work with debouncing.
- Can I use this for rate-limiting API requests or cron jobs?
- This package is optimized for jobs, notifications, and CLI commands—not direct API rate-limiting. For cron jobs, debounce the job dispatching logic (e.g., wrap `YourJob::dispatch()` in `debounce()`). API rate-limiting may require middleware or dedicated packages like `spatie/rate-limiter`.
- How do I configure the cache driver for debouncing?
- Publish the config with `php artisan vendor:publish --tag=laravel-debounce-config` and set `driver` in `.env` (e.g., `CACHE_DRIVER=redis`). Redis is recommended for atomic locks. Avoid file cache, as it doesn’t support lock contention. Test cache persistence under load.
- What happens if Redis goes down during debouncing?
- Debounce locks rely on cache, so Redis downtime may cause lost locks or duplicate executions. Mitigate this by monitoring cache health and implementing a fallback (e.g., disable debouncing via `LARAVEL_DEBOUNCE_ENABLED=false` in `.env` during outages).
- How can I track debounced events for analytics or debugging?
- Use the built-in report tracking by enabling `report_tracking` in config. Access reports via `Debounce::report()` or monitor via Telescope. Note that reports reset on cache flushes; for persistence, log to a database or external service in `after()` hooks.
- Is there a way to debounce Artisan commands in Laravel 10?
- No, CLI debouncing requires Laravel 11+. In Laravel 10, create a custom command that dispatches a debounced job instead. For example, wrap `Artisan::call()` in a job that uses `debounce()` logic. Alternatively, upgrade to Laravel 11+ for native support.
- How do I test debounced jobs without flaky timing issues?
- Disable debouncing in tests by setting `config(['debounce.enabled' => false])` or mock the cache driver. Use `Debounce::shouldSkip()` to bypass debounce logic conditionally. Avoid relying on timing in tests; focus on verifying the debounced behavior via hooks or report tracking.
- What are the performance implications of high-volume debouncing?
- High concurrency may cause cache lock contention, slowing execution. Test under load with your cache driver (Redis recommended). Optimize by increasing debounce intervals or using shorter TTLs for less critical tasks. Monitor queue backlogs and cache latency.
- Are there alternatives to this package for debouncing in Laravel?
- For jobs/notifications, consider `spatie/laravel-activitylog` (with custom debounce logic) or `beberlei/assert` for manual lock-based debouncing. For CLI tasks, use `spatie/laravel-schedule` with custom logic. This package stands out for its Laravel-native integration (UniqueJobs, cache) and reporting features.