codewiser/http-cache-control
Composer 安装命令:
composer require codewiser/http-cache-control
包简介
Http Cache Control layer for Laravel
关键字:
README 文档
README
This package provides solution to work with HTTP Cache-Control headers. Resources are cached and then invalidates on Eloquent events. Server may respond without making database requests, just using cache values.
Prepare models
CacheControl uses cache to keep headers values. Then model is changed the
associated cache must be invalidated.
The models must implement \Codewiser\HttpCacheControl\Contracts\Cacheable.
Here is example of implementation. Classes share cache tag, so changing any Model invalidates shared cache.
use Codewiser\HttpCacheControl\Contracts\Cacheable; use Codewiser\HttpCacheControl\Observers\InvalidatesCache; use Illuminate\Database\Eloquent\Attributes\ObservedBy; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Psr\SimpleCache\CacheInterface; #[ObservedBy(InvalidatesCache::class)] class User extends Model implements Cacheable { public function cache(): CacheInterface { return Cache::tags(['user', 'order']); } } #[ObservedBy(InvalidatesCache::class)] class Order extends Model implements Cacheable { public function cache(): CacheInterface { return Cache::tags(['order', 'user']); } }
Usage
CacheControl class analyzes request headers and creates response with proper
headers.
First argument must be a \Psr\SimpleCache\CacheInterface,
or \Codewiser\HttpCacheControl\Contracts\Cacheable, or classname of a Model,
that implements that interface.
The second argument is a callback, that should return response content. This callback would be called only if necessary.
Cache-Control header
You may set any Cache-Control directives you like:
use Codewiser\HttpCacheControl\CacheControl; use Codewiser\HttpCacheControl\CacheControlHeader; public function index(Request $request) { return CacheControl::make(Order::class, fn(Request $request) => OrderResource::collection(Order::all()) ) ->cacheControl(fn(Request $request) => new CacheControlHeader( public: true, max_age: 1800, must_revalidate: true, )); }
If no
publicorprivatedirectives was not set, it would beprivateby default.
Nothing would be cached on server in this case.
Expires header
Or you may set only Expires header:
use Codewiser\HttpCacheControl\CacheControl; use Codewiser\HttpCacheControl\CacheControlHeader; public function index(Request $request) { return CacheControl::make(Order::class, fn(Request $request) => OrderResource::collection(Order::all()) ) ->expires(now()->addHour()); }
Nothing would be cached on server in this case.
Caching entire response
You may want to cache the entire response:
use Codewiser\HttpCacheControl\CacheControl; use Codewiser\HttpCacheControl\CacheControlHeader; public function index(Request $request) { return CacheControl::make(Order::class, fn(Request $request) => OrderResource::collection(Order::all()) ) ->remember() ->cacheControl(new CacheControlHeader( public: true, max_age: now()->addHour(), must_revalidate: true, )); }
Use with care. Note, that cache may become huge.
Private Cache
If controller response must not be shared across users, you should
set Cache-Control: private directive.
use Codewiser\HttpCacheControl\CacheControl; use Codewiser\HttpCacheControl\CacheControlHeader; public function index(Request $request) { return CacheControl::make(Order::class, fn(Request $request) => OrderResource::collection( Order::query()->whereBelongsTo($request->user())->get() ) ) ->cacheControl(new CacheControlHeader( private: true, max_age: new DateInterval('PT1H'), must_revalidate: true, )); }
Vary header
Vary headers describes a list of request headers, that matters for caching.
For example, your application supports different languages, so cache depends
on Accept-Language request header:
use Codewiser\HttpCacheControl\CacheControl; use Codewiser\HttpCacheControl\CacheControlHeader; public function index(Request $request) { return CacheControl::make(Order::class, fn(Request $request) => OrderResource::collection(Order::all()) ) ->vary('Accept-Language') ->cacheControl(new CacheControlHeader( public: true, max_age: 1800, must_revalidate: true, )); }
Note, that web-server may append more Vary headers values. It is
Accept-Encodingas usual.
Conditional headers
Controller may respond with ETag and/or Last-Modified header, so the next
request may bring If-None-Match or If-Modified-Since headers, that makes it
conditional.
use Codewiser\HttpCacheControl\CacheControl; use Codewiser\HttpCacheControl\CacheControlHeader; public function index(Request $request) { return CacheControl::make(Order::class, fn(Request $request) => OrderResource::collection(Order::all()) ) ->cacheControl(['public' => true]) // Implicit ->etag() // Or explicit ->etag(fn() => custom_etag_calculation(Order::all())); }
In this example local cache stores only ETag value. That would be enough to
check future requests with If-None-Match.
This way is the most cache careful.
use Codewiser\HttpCacheControl\CacheControl; use Codewiser\HttpCacheControl\CacheControlHeader; public function index(Request $request) { return CacheControl::make(Order::class, fn(Request $request) => OrderResource::collection(Order::all()) ) ->cacheControl(['public' => true]) // Return timestamp or DateTimeInterface ->lastModified(fn() => Order::all()->max('updated_at')); }
codewiser/http-cache-control 适用场景与选型建议
codewiser/http-cache-control 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 854 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 02 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「laravel」 「cache-control」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 codewiser/http-cache-control 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 codewiser/http-cache-control 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 codewiser/http-cache-control 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Add HTTP caching to the response
Adds several features to help make ZF2 applications cacheable.
Simple, fast, standalone PHP lib, that helps You to define, if any of your files were modified since last time You check - Is this file FRESH?
Modify cache headers via configuration
A standalone ESI (Eve Swagger Interface) Client Library using kevinrob/guzzle-cache-middleware
统计信息
- 总下载量: 854
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-10