承接 angelleger/laravel-response-cache 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

angelleger/laravel-response-cache

Composer 安装命令:

composer require angelleger/laravel-response-cache

包简介

Production-grade response caching for Laravel APIs with ETag, Redis tags, per-route TTLs, and safe auth-variation

README 文档

README

Key-based full response caching for Laravel 10+ applications. The package stores complete GET/HEAD responses in the configured cache store using deterministic keys. It offers a small helper API, middleware, and artisan commands for managing cached responses without relying on cache tags.

Requirements

  • PHP 8.2 or higher
  • Laravel 10.x

Installation

composer require angelleger/laravel-response-cache
php artisan vendor:publish --tag=response-cache-config

Configuration

config/response_cache.php

return [
    'ttl' => 300, // default lifetime in seconds
    'store' => env('RESPONSE_CACHE_STORE'), // cache store to use
    'key_prefix' => 'resp_cache:', // prefix for all keys and indexes
    'guest_only' => true, // only cache guests unless auth=true is passed to middleware
    'vary_on_headers' => ['Accept', 'Accept-Language'], // headers included in the key
    'vary_on_cookies' => [], // cookies included in the key
    'include_query_params' => [], // when set, only these query parameters are used
    'ignore_query_params' => ['_', 'utm_*'], // query parameters to ignore (supports * suffix)
    'status_whitelist' => [200], // only cache these response codes
    'max_payload_kb' => null, // skip responses larger than this (in kilobytes)
    'etag' => true, // automatically add ETag header to cached responses
    'lock_seconds' => 0, // hold an atomic lock for this many seconds while generating
    'lock_wait' => 10, // seconds to wait for a lock to be released
    'debug' => env('RESPONSE_CACHE_DEBUG', false), // enable debug helpers
];

Configuration Notes

  • ttl: default time-to-live for cached responses when no explicit value is supplied.
  • store: cache store to use; null uses the application's default store.
  • key_prefix: string prepended to every cache key and route index entry.
  • guest_only: when true, authenticated users are skipped unless the middleware parameter auth=true is provided.
  • vary_on_headers / vary_on_cookies: header and cookie names that should contribute to the cache key.
  • include_query_params: if non-empty, only these query parameters are considered when building the key.
  • ignore_query_params: query parameters to discard; supports a trailing * wildcard.
  • status_whitelist: response status codes eligible for caching.
  • max_payload_kb: maximum size of the response body; larger responses are bypassed.
  • etag: toggle automatic generation of an ETag header when caching.
  • lock_seconds / lock_wait: when lock_seconds > 0, generation is wrapped in an atomic lock to prevent cache stampedes.
  • debug: when enabled, exposes additional debug headers.

Cache Key Strategy

Keys follow the pattern:

{method}:{path-or-route}:{normalized-query}:{locale}:{guard-or-guest}

The query segment is created by sorting parameters, removing ignored keys, and injecting vary_on_headers/vary_on_cookies values as pseudo parameters (h_header / c_cookie). The active authentication guard name (or guest) and the request locale are appended to avoid collisions.

Middleware

Apply the resp.cache middleware to any GET/HEAD route that should be cached.

Route::get('/posts', fn () => Post::all())
    ->middleware('resp.cache:ttl=120');

// Allow authenticated users to be cached
Route::get('/dashboard', fn () => view('dashboard'))
    ->middleware('auth', 'resp.cache:ttl=60,auth=true');

Middleware Parameters

  • ttl=<seconds> – override the default TTL for this route.
  • auth=true – cache authenticated responses even if guest_only is enabled.

Responses are cached only when:

  • The HTTP method is GET or HEAD.
  • The response status code appears in status_whitelist.
  • The response size does not exceed max_payload_kb (when set).
  • The request does not contain Cache-Control: no-store.

Cached responses include an X-Cache: HIT header for debugging. You can disable caching on a per-request basis by sending Cache-Control: no-store from the client or controller.

Helper API

use AngelLeger\ResponseCache\Facades\ResponseCache;

$key = ResponseCache::makeKey(request());
$response = ResponseCache::rememberResponse(request(), fn () => response('ok'), 60);
ResponseCache::forgetByKey($key);
ResponseCache::forgetRoute('posts.index');
  • makeKey(Request $request, array $overrides = []) – build the cache key used for the request.
  • rememberResponse(Request $request, Closure $callback, DateTimeInterface|int $ttl) – return the cached response or execute the callback and store the result.
  • forgetByKey(string $key) – remove a cached response.
  • forgetRoute(string $routeName) – remove all cached responses for the route (uses a bounded key index, max 1000 entries).

Artisan Commands

php artisan response-cache:clear --key="get:posts::en:guest"
php artisan response-cache:clear --route=posts.index
php artisan response-cache:stats
  • response-cache:clear – clear by exact key or by route name (uses the internal index).
  • response-cache:stats – show basic cache information such as the underlying driver.

Caveats

  • The package uses key-based invalidation only; cache flush() is intentionally avoided because it clears unrelated data.
  • Streamed responses or those not meeting cache rules (status, size, etc.) are skipped automatically.
  • Route indexes are capped to the most recent 1000 keys to remain memory safe.
  • Use caution when changing configuration in production; mismatched prefixes or stores can orphan keys.

License

MIT

angelleger/laravel-response-cache 适用场景与选型建议

angelleger/laravel-response-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 763 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「redis」 「performance」 「api」 「cache」 「response」 「middleware」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 angelleger/laravel-response-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 angelleger/laravel-response-cache 我们能提供哪些服务?
定制开发 / 二次开发

基于 angelleger/laravel-response-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 763
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-03