- How do I integrate php-http/cache-plugin with Laravel’s built-in HTTP client (Guzzle or Symfony) for caching API responses?
- First, wrap your Laravel HTTP client (e.g., Guzzle 7+) in an HTTPlug-compatible client using `php-http/guzzle7-adapter`. Then, inject the `CachePlugin` into a `PluginStack` with a PSR-6 cache pool (e.g., Redis via `predis/predis`). Laravel’s `Cache` facade can be adapted to PSR-6 using `cache/adapter-redis` or similar. Example: `$client = new PluginClient(new PluginStack(ClientDiscovery::find()), [new CachePlugin(new CachePool(new PredisAdapter()))]);`
- Does php-http/cache-plugin work with Laravel’s Cache facade directly, or do I need a PSR-6 adapter?
- You’ll need a PSR-6 adapter to bridge Laravel’s `Cache` facade. Use packages like `cache/adapter-redis` or `cache/array-adapter` to wrap Laravel’s cache manager (e.g., `Cache::store('redis')`) into a PSR-6 pool. This avoids reinventing caching logic while leveraging Laravel’s existing backends.
- Can I exclude specific API endpoints (e.g., /auth) from caching with this plugin?
- Yes, use the `blacklisted_paths` option in the `CachePlugin` configuration. Pass an array of regex patterns (e.g., `['/auth', '/webhooks']`) to skip caching for those routes. This is ideal for dynamic or sensitive endpoints.
- What Laravel versions and PHP-HTTP ecosystem packages does php-http/cache-plugin support?
- The plugin supports PHP 8.1–8.5 and works with Laravel projects using HTTPlug-compatible clients (e.g., `php-http/guzzle7-adapter` or `symfony/http-client`). It’s PSR-6 compliant, so any Laravel cache backend (Redis, Memcached, file) can be used via adapters like `predis/predis` or `cache/file-adapter`.
- How does the plugin handle conditional requests (e.g., If-None-Match headers) for caching immutable resources like CDN assets?
- The plugin respects `Cache-Control` and `ETag` headers by default (via `respect_response_cache_directives`). For immutable resources, it validates responses against cached `ETag` or `Last-Modified` headers, reducing redundant network calls. Enable `ETagCachePlugin` for stricter ETag-only caching if needed.
- Will php-http/cache-plugin work with Laravel’s middleware (e.g., HandleCors) or conflict with existing HTTP stack?
- The plugin operates at the HTTPlug layer and doesn’t interfere with Laravel’s middleware. However, ensure your middleware processes responses *after* the `CachePlugin` in the stack. For observability, log `X-Cache` headers via Laravel’s `LogResponse` middleware or a custom listener.
- What’s the performance overhead of caching large responses (e.g., >1MB files) with this plugin?
- Stream detachment (v1.8.0+) avoids serialization issues but may increase memory usage for large payloads. For files >1MB, consider customizing `CacheKeyGenerator` or using a dedicated file cache backend. Benchmark with your workload—most use cases see latency improvements despite minor overhead.
- How do I monitor cache hit/miss rates or integrate with Laravel’s logging system?
- The plugin emits `X-Cache` headers (e.g., `X-Cache: HIT`). Log these via Laravel’s `LogResponse` middleware or a custom `CacheListener`. For metrics, use a package like `spatie/laravel-monitoring` or Prometheus client libraries to track headers. No built-in Prometheus support exists yet.
- Can I preload (warm) the cache with common API responses at Laravel startup?
- Yes, manually trigger requests during bootstrapping (e.g., in `AppServiceProvider@boot`) or use Laravel’s `booted` event. For dynamic warming, schedule a command with `Artisan::call('cache:warm')`. Ensure your cache backend supports bulk operations (e.g., Redis pipelines).
- What alternatives exist for HTTP caching in Laravel, and when should I choose this plugin over them?
- Alternatives include Laravel’s `Cache::remember()` (for simple in-memory caching) or `spatie/laravel-cache-control` (for response-based caching). Choose `php-http/cache-plugin` if you need **HTTPlug integration**, **PSR-6 compatibility**, or **fine-grained control** (e.g., ETag validation, path blacklisting). It’s ideal for API clients, not Laravel’s internal HTTP layer.