bitandblack/request-cache
Composer 安装命令:
composer require bitandblack/request-cache
包简介
Smooth caching of HTTP requested data. It runs non-blocking and uses background processes to request the data.
README 文档
README
Bit&Black Request Cache
Smooth caching of HTTP requested data. It runs non-blocking and uses background processes to request the data.
Installation
This library is available for the use with Composer. Add it to your project by running $ composer require bitandblack/request-cache.
Usage
At first initialize a cache type and a process object, and use them to initialize the request handler object. In this example we're going to use the FileSystemCache:
<?php
use BitAndBlack\RequestCache\CacheType\FileSystemCache;
use BitAndBlack\RequestCache\Process;
use BitAndBlack\RequestCache\RequestHandler;
$cache = new FileSystemCache(__DIR__);
$process = new Process();
$requestHandler = new RequestHandler($cache, $process);
The request handler is now ready to be used. Now create a request object with the URL you want to request and the time to live (ttl). This example requests the URL https://www.bitandblack.com and allows the data to be stored for 1 hour:
<?php
use BitAndBlack\RequestCache\Request;
$request = new Request(
'https://www.bitandblack.com',
3600
);
The data can be requested now:
<?php
$response = $requestHandler->getResponse($request);
The request handler will return the response immediately and without blocking the script. If the data has been expired, the last response will be returned from cache, while the new data will be requested in the background.
When successful, the response will be a GuzzleHttp\Psr7\Response object, that implements the Psr\Http\Message\ResponseInterface.
Options
Custom request client
Per default, this library uses Guzzle to make requests. You can use the request callback to set up a custom request client or to modify the configuration.
<?php
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
$cache->setRequestCallback(
function (string $url): ResponseInterface
{
$client = new Client();
return $client->request(
'GET',
$url,
[
'allow_redirects' => false
]
);
}
);
TTL
The ttl value may be easier to set with the help of the bitandblack/duration library. For example instead of writing 86400 for the duration of a whole day, you can write Duration::createFromDays(1)->getSeconds() then.
Help
If you have any questions, feel free to contact us under hello@bitandblack.com.
Further information about Bit&Black can be found under www.bitandblack.com.
bitandblack/request-cache 适用场景与选型建议
bitandblack/request-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.55k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bitandblack/request-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bitandblack/request-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-18