Download the PHP package methorz/http-cache-middleware without Composer
On this page you can find all versions of the php package methorz/http-cache-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download methorz/http-cache-middleware
More information about methorz/http-cache-middleware
Files in methorz/http-cache-middleware
Package http-cache-middleware
Short Description PSR-15 HTTP caching middleware with ETag support and RFC 7234 compliance
License MIT
Informations about the package http-cache-middleware
MethorZ HTTP Cache Middleware
PSR-15 HTTP caching middleware with ETag support and RFC 7234 compliance
Automatic HTTP caching for PSR-15 applications with ETag generation, 304 Not Modified responses, and Cache-Control header management. Zero configuration, production-ready.
โจ Features
- ๐ท๏ธ Automatic ETag Generation - MD5/SHA256/custom algorithm support
- ๐ 304 Not Modified - Automatic conditional request handling
- ๐ Cache-Control Builder - Fluent interface for RFC 7234 directives
- โ RFC Compliant - RFC 7234 (caching) & RFC 7232 (conditional requests)
- ๐ฏ Conditional Requests -
If-None-Match, wildcard support - ๐ช Strong & Weak ETags - Full support for both ETag types
- ๐ง Zero Configuration - Sensible defaults, works out-of-the-box
- ๐จ Highly Customizable - Control caching behavior per-route
- ๐ฆ Framework Agnostic - Works with any PSR-15 application
๐ฆ Installation
๐ Quick Start
Basic Usage
That's it! All GET and HEAD requests will now have:
- Automatic ETag generation
- 304 Not Modified responses
- Proper caching headers
๐ Detailed Usage
With Cache-Control Directives
Generated Headers:
Configuration Options
Development Mode (Disable Caching)
For development, you want fresh data on every request. Simply disable the middleware:
Recommended approach: Only add CacheMiddleware to your production pipeline configuration, not in development.
๐ฏ Cache-Control Directive Builder
Fluent interface for building Cache-Control headers:
Common Patterns
Public, cacheable for 1 hour:
Private, no caching:
Immutable assets (images, CSS, JS):
API responses with shared cache:
All Directives
| Method | Description | Example |
|---|---|---|
public() |
Cache may be stored by any cache | public |
private() |
Cache only for single user | private |
noCache() |
Must revalidate before use | no-cache |
noStore() |
Must not be stored anywhere | no-store |
maxAge(int) |
Maximum freshness time | max-age=3600 |
sMaxAge(int) |
Shared cache max age | s-maxage=7200 |
mustRevalidate() |
Must revalidate when stale | must-revalidate |
proxyRevalidate() |
Proxy must revalidate | proxy-revalidate |
noTransform() |
Cache must not transform response | no-transform |
staleWhileRevalidate(int) |
Serve stale while fetching fresh | stale-while-revalidate=60 |
staleIfError(int) |
Serve stale if origin errors | stale-if-error=120 |
immutable() |
Response will never change | immutable |
๐ท๏ธ ETag Generation
Automatic ETag Generation
ETag Utilities
๐ How It Works
1. First Request (Cache Miss)
Client caches response with ETag
2. Subsequent Request (Cache Validation)
Benefits:
- โก Faster: No body transmission (~95% bandwidth reduction)
- ๐ฐ Cheaper: Reduced server CPU & network costs
- ๐ Better UX: Instant responses for unchanged resources
๐ฏ Use Cases
1. Static Asset Caching
2. API Response Caching
3. Dynamic Content with Validation
4. Private User Data
5. CDN Integration
๐ง Configuration Examples
Mezzio / Laminas
Per-Route Configuration
๐ HTTP Headers Reference
Request Headers (Client โ Server)
| Header | Description | Example |
|---|---|---|
If-None-Match |
Conditional request with ETag | "abc123" or W/"abc123" or * |
If-Modified-Since |
Conditional request with date | Wed, 21 Oct 2015 07:28:00 GMT |
Response Headers (Server โ Client)
| Header | Description | Example |
|---|---|---|
ETag |
Entity tag for resource version | "abc123" or W/"abc123" |
Cache-Control |
Caching directives | public, max-age=3600 |
Expires |
Absolute expiration time | Wed, 21 Oct 2025 07:28:00 GMT |
Last-Modified |
Resource modification time | Wed, 21 Oct 2024 07:28:00 GMT |
๐งช Testing
Test Coverage: 37 tests, 57 assertions, 100% passing
โก Performance Impact
Bandwidth Savings
Server Load Reduction
- โ 304 responses skip expensive body serialization
- โ ETag comparison is instant (simple hash check)
- โ Reduces database queries when responses haven't changed
- โ Lower CPU usage for repeated identical requests
๐ Security Considerations
Private vs Public
Cache Invalidation
This middleware handles validation (304 responses), not invalidation. For cache invalidation:
- Change resource content โ new ETag โ cache miss โ fresh response
- Use
no-cachedirective โ always revalidate with server - Use CDN purge APIs for immediate invalidation
๐ Resources
๐ Related Packages
This package is part of the MethorZ HTTP middleware ecosystem:
| Package | Description |
|---|---|
| methorz/http-dto | Automatic HTTP โ DTO conversion with validation |
| methorz/http-problem-details | RFC 7807 error handling middleware |
| methorz/http-cache-middleware | HTTP caching with ETag support (this package) |
| methorz/http-request-logger | Structured logging with request tracking |
| methorz/openapi-generator | Automatic OpenAPI spec generation |
These packages work together seamlessly in PSR-15 applications.
๐ License
MIT License. See LICENSE for details.
๐ค Contributing
Contributions welcome! See CONTRIBUTING.md for guidelines.
๐ Links
- Changelog
- Contributing
- Security
- Issues
All versions of http-cache-middleware with dependencies
psr/http-factory Version ^1.0
psr/http-message Version ^1.0 || ^2.0
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0