mehr-it/buffer
Composer 安装命令:
composer require mehr-it/buffer
包简介
Library implementing buffer utilities
关键字:
README 文档
README
This is a small library implementing simple buffer utilities for dealing with large data sets.
Flushing buffers
Flushing buffers implement buffers with a given size which are automatically flushed using a given handler function when they are full.
You may create an instance as follows. It Takes up to three arguments:
new FlushingBuffer(10, function(data) { /* send data */ });
new FlushingBuffer(10, function(data) { /* send data */ }, function() { return collect(); });
The first argument specifies the buffer size, the second one a handler function which is called each time the buffer is full. It receives the buffer data as argument. The third one is optional and acts as resolver for the underlying data structure to use. If omitted a simple array is used.
New items are added to the buffer using the add()-method. Usually you want the buffer to be
flushed a last time, after all data was added, even if it's not full. To achieve this, simply
call the flush()-method to manually flush the buffer:
$b = new FlushingBuffer(2, function(data) { /* send data */ });
$b->add(1);
$b->add(2);
$b->add(3);
$b->flush();
You may also specify a key, if you want to replace elements in the buffer at given key.
$b = new FlushingBuffer(2, function(data) { /* send data */ });
$b->add(1, 'key1');
$b->add(2, 'key1');
Of course replacing and existing element does not increase buffer size and therefore does not cause a buffer flush.
Adding multiple items
To add multiple items to the buffer at once, the addMultiple() method can be used. By default
keys are not preserved. You can change this by passing true as second parameter.
$b = new FlushingBuffer(2, function(data) { /* send data */ });
$b->addMultiple([1, 2, 3]);
// with preserved keys
$b->addMultiple(['a' => 1, 'b' => 2, 'c' => 3], true);
Filling array keys
To fill multiple keys with the same value, the fillKeys() method can be used:
$b = new FlushingBuffer(2, function(data) { /* send data */ });
$b->fillKeys(['a', 'b', 'c'], 1);
This will add/replace the keys "a", "b" and "c" with the value 1.
Setting values by path
The setPath() method sets a value for the given array path. The path can be specified as string
using "dot notation" or as array:
$b = new FlushingBuffer(2, function(data) { /* send data */ });
$b->setPath(['this', 'is', 'a' 'path'], 1);
$b->setPath('this.is.another.path'], 2);
This creates new array levels as needed but only root level items affect the buffer count.
Chunk processors
Chunk processors allow to process an iterable in chunks and return the processed items as
generator. They act pretty much like PHP's array_chunk() function except they working with
any kind of iterable and returning a generator. Thus they are predestined for processing large
data sets.
You may use them like this:
$in = function() { /* input generator code */ };
$generator = (new ChunkProcessor($in, 500, function($chunk) {
yield /* ... */
}))->consume();
To maintain the keys, the keepKeys argument may be set to true:
$generator = (new ChunkProcessor($in, 500, $processor, null, true))->consume();
Als static method exists, which creates the processor and calls consume() in one step:
$generator = ChunkProcessor::process($in, 500, $processor);
mehr-it/buffer 适用场景与选型建议
mehr-it/buffer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.79k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「Buffer」 「chunked」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mehr-it/buffer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mehr-it/buffer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mehr-it/buffer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
A php buffer to work with binary data.
Library to communicate with CRMBuffer
This library allows you to use the Buffer API using PHP
Response type for the bulletphp framework implementing chunked encoding
Php implementation of the file chunked upload for the angular directive ng-file-upload
统计信息
- 总下载量: 5.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-22