-
Notifications
You must be signed in to change notification settings - Fork 0
Cache
lpachecob edited this page Mar 24, 2026
·
1 revision
BOUNDLY v0.9.0 includes automatic API response caching for improved performance.
Infrastructure\FrameworkCore\Http\Middleware\ResponseCacheMiddleware.phpEnable caching in configuration:
// config/boundly.php
'cache' => [
'response' => [
'enabled' => true,
'store' => 'file', // or 'redis', 'memcached'
'ttl' => 60, // Cache TTL in minutes
'exclude_paths' => ['api/health', 'api/*/health'],
],
],- First Request (MISS): Response is cached and returned
- Subsequent Requests (HIT): Cached response is returned immediately
- Cache Key: Based on path + query parameters + Accept-Language
X-Cache: HIT
X-Cache: MISS
#[Action(resource: 'products', method: 'GET', middleware: ['cache.response:30'])]
class ListProductsAction
{
public function execute(): JsonResponse
{
// Response will be cached for 30 minutes
return response()->json(Product::all());
}
}use Illuminate\Support\Facades\Cache;
class ProductController
{
public function show($id)
{
$product = Cache::remember("product:{$id}", 60, function () use ($id) {
return Product::findOrFail($id);
});
return response()->json($product);
}
public function update(Request $request, $id)
{
$product = Product::findOrFail($id);
$product->update($request->validated());
// Invalidate cache
Cache::forget("product:{$id}");
return response()->json($product);
}
}BOUNDLY can automatically invalidate cache when entities are modified:
// config/boundly.php
'cache' => [
'response' => [
'enabled' => true,
'auto_invalidate' => true,
'invalidate_on' => ['created', 'updated', 'deleted'],
],
],use Infrastructure\FrameworkCore\Http\Middleware\ResponseCacheMiddleware;
$middleware = app(ResponseCacheMiddleware::class);
// Invalidate specific path
$middleware->invalidate('api/products', ['page' => 1]);
// Invalidate all cached responses
$middleware->invalidateAll();// Tag-based invalidation with Redis
Cache::tags(['products', 'category_5'])->flush();// config/boundly.php
'cache' => [
'response' => [
'enabled' => true,
'store' => env('CACHE_DRIVER', 'file'),
'ttl' => 60, // Default TTL in minutes
'exclude_paths' => [
'api/health',
'api/*/health',
'api/auth/*',
'api/admin/*',
],
'vary_by' => [
'accept_language',
'accept_encoding',
],
],
],| Option | Default | Description |
|---|---|---|
enabled |
false |
Enable/disable response caching |
store |
'file' |
Cache store driver |
ttl |
60 |
Default TTL in minutes |
exclude_paths |
[] |
Paths to never cache |
vary_by |
[] |
Headers that affect cache key |
Excluded paths are automatically bypassed:
'exclude_paths' => [
'api/health', // Health checks
'api/auth/*', // Authentication endpoints
'api/users/me', // User-specific data
'api/*/sensitive', // Sensitive resources
],Responses from cache include appropriate headers:
Cache-Control: public, max-age=3600
X-Cache: HIT
| Scenario | Without Cache | With Cache |
|---|---|---|
| GET /api/products | ~150ms | ~5ms |
| GET /api/products/123 | ~80ms | ~2ms |
| GET /api/users?page=1 | ~200ms | ~10ms |
public function test_products_are_cached()
{
$response = $this->getJson('/api/products');
$response->assertHeader('X-Cache', 'MISS');
// Second request should be cached
$response = $this->getJson('/api/products');
$response->assertHeader('X-Cache', 'HIT');
}'store' => 'file',Simple, no external dependencies. Good for single-server deployments.
'store' => 'redis',CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379Best for multi-server deployments with high traffic.
'store' => 'memcached',CACHE_DRIVER=memcached
MEMCACHED_HOST=127.0.0.1
MEMCACHED_PORT=11211High-performance option for large-scale applications.
- Verify
cache.response.enabledistrue - Check cache store is configured correctly
- Ensure path is not in
exclude_paths - Verify cache directory is writable
- Reduce TTL for frequently updated resources
- Implement cache invalidation on updates
- Use cache tags for selective invalidation
Next Step: Exception-Handling