mombol/php-file-queue

本包最新版本(v1.2.2)没有提供许可证信息。

PHP 文件队列

v1.2.2 2016-10-24 06:44 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:58:31 UTC


README

支持php文件队列

  • 通过queueNamespace共享队列数据的多队列任务。
  • 通过设置consumeSpan来消费队列数据(为了避免队列数据文件过大)。
  • 追踪队列游标位置以恢复最后游标位置(需要手动调用track方法)。

使用方法

实例

**NOTE**
* All construct parameters are optional for your custom settings.
* set config `role` to `customer` or orthers when process as a customer to consume queue data which shared by multi queue.

文件队列实例

$FileQueue = new FileQueue(array(
    'silent' => false, // if it is set and is equal to true, just return only one object handler without mount any files
    'role' => 'generator', // queue role, default generator, it must be setted not generator when process as a customer
    'queueNamespace' => 'nsp', // queue namespace to support one queue data shared by multi queue
    'queueDir' => '/var/run/php-file-queue',
    'queueFileName' => 'default',
    'queueFileSuffix' => 'mq',
    'cursorFileSuffix' => 'cursor',
    'initialReadLineNumber' => 0 //  the number of initial read line number, default value set 0 mean that will read from the queue header, the orthers you can set to 'end' which make it read from the queue tail
));

文件队列数据消费实例

$QueueDataConsume = new QueueDataConsume(array(
    'consumeSpan' => 5, // queue consumption span
    'doConsumeBackup' => true, // whether or not to backup the queue consumption data
    'queueDir' => '/var/run/php-file-queue',
    'queueFileName' => 'default'
));

向队列中推送数据

$data = 'test'; // anything you want to push
$GerneratorFileQueue->push($data);

从队列中弹出数据

$num = 1;
$currentPostion = array();
$data = $CustomerFileQueue->pop($num, $currentPostion); // $num : pop number, $currentPostion : current postion after pop a data
// print_r($currentPostion);

获取当前位置

$pos = $FileQueue->position();

将位置重置到队列头部

$FileQueue->rewind();

将队列游标指向末尾

$FileQueue->end();

追踪队列游标并恢复最后游标

$FileQueue->track();
$FileQueue->rewind();
// do anything ...
$FileQueue->recover();

获取队列长度

$length = $FileQueue->length();

文件队列结束的测试

while (!$FileQueue->eof()) {
    // do anything ...
}

卸载队列文件

$FileQueue->unmount();

消费队列数据

$QueueDataConsume->consume();

清理队列数据

$QueueDataConsume->clean();