smartondev/httpcache-middleware
Composer 安装命令:
composer require smartondev/httpcache-middleware
包简介
PSR-7, PSR-11 compatible middlewares for HTTP cache handling
README 文档
README
This package is under development, and not ready for production use.
This package provides a set (eg. Cache-Control, ETag) of HTTP cache handling PSR-7, PSR-11 compatible middlewares.
Installation
composer require smartondev/httpcache-middleware
Usage
Cache-Control
For use Cache-Control, ETag, Last-Modified and more http "caching" headers, you need to add HttpCacheMiddleware
to your route.
use SmartonDev\HttpCacheMiddleware\Http\Middleware\HttpCacheMiddleware; use SmartonDev\HttpCacheMiddleware\Providers\Constants\ProviderConstants; // add HttpCacheContextService to container with ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID as singleton per request Route::get('/api/user/{id}', function ($id) { $user = User::find($id); app()->get(ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID)->getCacheHeaderBuilder()->maxAge(days: 30)->public(); return response()->json($user); })->middleware(HttpCacheMiddleware::class);
ETag
For use ETag, you need to add ETagMatchMiddleware to your route. You need to implement ETagResolverInterface and add
it to the container with ProviderConstants::ETAG_RESOLVER_SERVICE_ID. For use ETag you need to
add HttpCacheContextService andHttpCacheMiddleware to the container
with ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID
and ProviderConstants::HTTP_CACHE_MIDDLEWARE_SERVICE_ID.
use SmartonDev\HttpCacheMiddleware\Http\Middleware\ETagMatchMiddleware; use SmartonDev\HttpCacheMiddleware\Http\Middleware\HttpCacheMiddleware; use SmartonDev\HttpCacheMiddleware\Providers\Constants\ProviderConstants; use SmartonDev\HttpCacheMiddleware\Contracts\ETagResolverInterface; class ETagResolver implements ETagResolverInterface { public function resolve($request): string { // use it own logic to generate ETag $id = $request->route('id'); $etagFromCache = Cache::get('etag_' . $id); return $etagFromCache; } } // add ETagResolver to container with ProviderConstants::ETAG_RESOLVER_SERVICE_ID // add HttpCacheContextService to container with ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID as singleton pre request Route::get('/api/user/{id}', function ($id) { $user = User::find($id); app()->get(ProviderConstants::HTTP_CACHE_CONTEXT_SERVICE_ID)->getCacheHeaderBuilder()->maxAge(days: 30)->public(); // etag added automatically, or you can set it manually return response()->json($user); })->middleware(HttpCacheMiddleware::class)->middleware(ETagMatchMiddleware::class);
Modified since
Author
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2024-11-02