kevinrob/guzzle-cache-middleware
Composer 安装命令:
composer require kevinrob/guzzle-cache-middleware
包简介
A HTTP/1.1 Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack. (RFC 7234)
关键字:
README 文档
README
A HTTP Cache for Guzzle 6+. It's a simple Middleware to be added in the HandlerStack.
Goals
- RFC 7234 compliance
- Performance and transparency
- Assured compatibility with PSR-7
Built-in storage interfaces
Installation
composer require kevinrob/guzzle-cache-middleware
or add it the your composer.json and run composer update kevinrob/guzzle-cache-middleware.
Why?
Performance. It's very common to do some HTTP calls to an API for rendering a page and it takes times to do it.
How?
With a simple Middleware added at the top of the HandlerStack of Guzzle.
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Kevinrob\GuzzleCache\CacheMiddleware; // Create default HandlerStack $stack = HandlerStack::create(); // Add this middleware to the top with `push` $stack->push(new CacheMiddleware(), 'cache'); // Initialize the client with the handler option $client = new Client(['handler' => $stack]);
Examples
Laravel cache
You can use a cache with Laravel, e.g. Redis, Memcache etc.:
[...] use Illuminate\Support\Facades\Cache; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Storage\LaravelCacheStorage; [...] $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new LaravelCacheStorage( Cache::store('redis') ) ) ), 'cache' );
Flysystem
[...] use League\Flysystem\Local\LocalFilesystemAdapter; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Storage\FlysystemStorage; [...] $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new FlysystemStorage( new LocalFilesystemAdapter('/path/to/cache') ) ) ), 'cache' );
WordPress Object Cache
[...] use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Storage\WordPressObjectCacheStorage; [...] $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new WordPressObjectCacheStorage() ) ), 'cache' );
Public and shared
It's possible to add a public shared cache to the stack:
[...] use Cache\Adapter\PHPArray\ArrayCachePool; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy; use Kevinrob\GuzzleCache\Storage\Psr6CacheStorage; use League\Flysystem\Local\LocalFilesystemAdapter; use Kevinrob\GuzzleCache\Storage\FlysystemStorage; [...] // Private caching $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new FlysystemStorage( new LocalFilesystemAdapter('/tmp/') ) ) ), 'private-cache' ); // Public caching $stack->push( new CacheMiddleware( new PublicCacheStrategy( new Psr6CacheStorage( new ArrayCachePool() ) ) ), 'shared-cache' );
Greedy caching
In some cases servers might send insufficient or no caching headers at all. Using the greedy caching strategy allows defining an expiry TTL on your own while disregarding any possibly present caching headers:
[...] use Kevinrob\GuzzleCache\KeyValueHttpHeader; use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy; use Kevinrob\GuzzleCache\Storage\FlysystemStorage; use League\Flysystem\Local\LocalFilesystemAdapter; [...] // Greedy caching $stack->push( new CacheMiddleware( new GreedyCacheStrategy( new FlysystemStorage( new LocalFilesystemAdapter('/tmp/') ), 1800, // the TTL in seconds new KeyValueHttpHeader(['Authorization']) // Optional - specify the headers that can change the cache key ) ), 'greedy-cache' );
Delegate caching
Because your client may call different apps, on different domains, you may need to define which strategy is suitable to your requests.
To solve this, all you have to do is to define a default cache strategy, and override it by implementing your own Request Matchers.
Here's an example:
namespace App\RequestMatcher; use Kevinrob\GuzzleCache\Strategy\Delegate\RequestMatcherInterface; use Psr\Http\Message\RequestInterface; class ExampleOrgRequestMatcher implements RequestMatcherInterface { /** * @inheritDoc */ public function matches(RequestInterface $request) { return false !== strpos($request->getUri()->getHost(), 'example.org'); } }
namespace App\RequestMatcher; use Kevinrob\GuzzleCache\Strategy\Delegate\RequestMatcherInterface; use Psr\Http\Message\RequestInterface; class TwitterRequestMatcher implements RequestMatcherInterface { /** * @inheritDoc */ public function matches(RequestInterface $request) { return false !== strpos($request->getUri()->getHost(), 'twitter.com'); } }
require_once __DIR__ . '/vendor/autoload.php'; use App\RequestMatcher\ExampleOrgRequestMatcher; use App\RequestMatcher\TwitterRequestMatcher; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Kevinrob\GuzzleCache\CacheMiddleware; use Kevinrob\GuzzleCache\Strategy; $strategy = new Strategy\Delegate\DelegatingCacheStrategy($defaultStrategy = new Strategy\NullCacheStrategy()); $strategy->registerRequestMatcher(new ExampleOrgRequestMatcher(), new Strategy\PublicCacheStrategy()); $strategy->registerRequestMatcher(new TwitterRequestMatcher(), new Strategy\PrivateCacheStrategy()); $stack = HandlerStack::create(); $stack->push(new CacheMiddleware($strategy)); $guzzle = new Client(['handler' => $stack]);
With this example:
- All requests to
example.orgwill be handled byPublicCacheStrategy - All requests to
twitter.comwill be handled byPrivateCacheStrategy - All other requests won't be cached.
Drupal
See Guzzle Cache module.
Links that talk about the project
Buy me a coffee
If you like this project, you can buy me a coffee! (or a beer 😉)

Development
Docker quick start
Initialization
make init
Running test
make test
Entering container shell
make shell
kevinrob/guzzle-cache-middleware 适用场景与选型建议
kevinrob/guzzle-cache-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.74M 次下载、GitHub Stars 达 435, 最近一次更新时间为 2015 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「performance」 「php」 「http」 「cache」 「Guzzle」 「validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kevinrob/guzzle-cache-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kevinrob/guzzle-cache-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kevinrob/guzzle-cache-middleware 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
g4 application profiler package
repository php library
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Create link to static resources with cache-breaking segment based on md5 of the file
Production-ready Laravel Prometheus metrics package with built-in collectors for HTTP, database, cache, queue, events, errors, filesystem, and mail monitoring
统计信息
- 总下载量: 18.74M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 440
- 点击次数: 34
- 依赖项目数: 118
- 推荐数: 5
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-07