- How do I install the saloonphp/cache-plugin in a Laravel project?
- Run `composer require saloonphp/cache-plugin` and ensure you have a PSR-16 cache driver (e.g., Redis or Memcached) configured in Laravel. The plugin integrates seamlessly with Saloon’s existing middleware pipeline.
- Which Laravel versions and Saloon versions does this plugin support?
- The plugin requires **Saloon v4+** and **PHP 8.1+**. While Laravel compatibility depends on Saloon’s Laravel integration, it works with Laravel 9+ and 10+ by default, as Saloon supports these versions.
- Can I cache responses globally or only per-request?
- You can configure caching **globally** via Saloon’s middleware stack or **per-request** using the `withCache()` method. Global caching applies to all requests, while per-request lets you fine-tune TTL and keys dynamically.
- What cache backends are supported (Redis, database, etc.)?
- The plugin supports **any PSR-16 compliant cache** (Redis, Memcached, APCu, or Laravel’s filesystem cache). Database-based caches require a custom adapter, as they aren’t natively supported.
- How do I handle cache invalidation for dynamic API responses?
- Use **TTL-based expiration** or implement **event-driven invalidation** (e.g., webhooks or manual cache clearing). For mutable data, combine the plugin with Laravel’s cache tags or a custom invalidation strategy.
- What happens if the cache fails during a request?
- By default, the plugin **falls back to the original HTTP request** if the cache fails. You can customize this behavior by extending Saloon’s middleware or using a circuit breaker (e.g., `saloonphp/circuit-breaker`).
- Is this plugin suitable for real-time APIs like WebSocket push notifications?
- No. This plugin is **not recommended** for real-time systems where stale data is unacceptable. It’s optimized for **read-heavy, idempotent APIs** (e.g., product catalogs, weather data) where latency or rate limits are concerns.
- How do I test the caching behavior in PHPUnit?
- Mock the PSR-16 cache pool using Laravel’s `Mockery` or PHPUnit’s `getMockBuilder()`. Verify cache hits/misses by checking the `CacheItemPoolInterface` interactions. Test edge cases like TTL expiration and cache key collisions.
- Are there alternatives to this plugin for caching Saloon responses?
- Yes. For simple cases, **Laravel’s built-in HTTP client caching** or **OPcache** (for PHP bytecode) might suffice. For advanced use cases, consider **dedicated reverse proxies (Varnish, Nginx)** or Saloon’s **retry middleware** for rate-limited APIs.
- How do I monitor cache performance (hit/miss rates) in production?
- Instrument the cache with **Laravel’s cache events** or use **OpenTelemetry** to track cache interactions. Log hit/miss rates via middleware or a dedicated monitoring tool like Datadog or Prometheus.