波纹/聚氯乙烯

通过创建数据管道来简化批量处理

v0.1.0 2019-12-09 22:26 UTC

This package is not auto-updated.

Last update: 2024-09-10 20:50:26 UTC


README

这个PHP库应该用于优化批量数据处理。它是在减少数据库或Web API往返次数的思路下创建的。

收集

PVC在设计时就考虑到了批量处理。如果你需要减少往返次数,那么你需要增大负载。PVC通过简化为负载分组对象,使一次性查询API中的记录变得更加容易。

# Batch operations of 40

$pipeline = new \Pvc\Pipeline;

$pipeline->collect(40, function($batch) {
	echo "Batch size: ".count($batch)."\n";
});

for ($i = 0; $i < 100; $i++) {
	$pipeline->push($i);
}
$pipeline->flush();

// Batch size: 40
// Batch size: 40
// Batch size: 20

分支

如果你需要将数据分割到多个路径中,PVC可以帮助你。

# Branched batches

$pipeline = new \Pvc\Pipeline;
$pipeline
	->branch('switch', function($datum) {
		return $datum['type'];
	})
	// the second argument is always the path this data took
	->collect(30, function($data, $path) {
		echo "For type ".$path['switch'].": ".count($data)." records\n";
	});

$type = 0;
for ($i = 0; $i < 100; $i++) {
	$pipeline->push(['type' => ($type++) % 3, 'id' => $i ]);
};
$pipeline->flush();

// For type 0: 30
// For type 1: 30
// For type 2: 30
// For type 0: 4
// For type 1: 3
// For type 2: 3

转换

在数据传输过程中转换数据可能对你的应用程序很有用,因此PVC提供了一个转换操作,允许你在数据流中更改数据。

$pipeline = new \Pvc\Pipeline;
$pipeline->transform(function($data) use (&$lookupTable) {
	return $lookupTable[$data->getId()];
});

过滤

你可以使用过滤操作来过滤掉不需要或不相关的数据。

$pipeline = new \Pvc\Pipeline;
$pipeline->filter(function($data) use (&$lookupTable) {
	return $data instanceof GoodData;
});

展开

与收集相反。当你的数据流是一个数组时,展开将遍历该数组,并使后续操作将数据视为数组。