定制 smartondev/httpcache-middleware 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2024-11-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固