Code Monkey home page Code Monkey logo

dq's Introduction

dq

PHP delay queue based on Redis. Just For Study.

Install

composer require outman/dq

Usage

Client

require __DIR__ . '/vendor/autoload.php';

$redisParams = [
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
];

$config = [
    'params' => $redisParams // Predis/Client $params
    // 'options' => [] // Predis/Client $options
    // 'bucketCount' => DQ::DEFAULT_BUCKET_THREAD
    // 'queueName' => DQ::DEFAULT_QUEUE,
    // 'bucketPrefix' => DQ::DEFAULT_QUEUE_BUCKET,
    // 'partialCount' => DQ::HASH_PARTIAL,
];

$queue = new DQ\Client\Queue($config);
$queue->enqueue(mixed $value [, int $delaySeconds = 0]); // $value will be serialize.

//// enqueue delay
for ($i = 0; $i <= 10000; $i ++) {
    mt_srand($i);
    $dt = mt_rand(0, 100);// delay time seconds.
    $ret = $queue->enqueue([
        'dq' => sprintf('delay value - %07d - %d', $i, $dt),], $dt);

    // if ($i % 10000) {
    //     echo "TEST-", $i, PHP_EOL;
    // }
}

//// dequeue
while (true) {
    $v = $queue->dequeue();
    if ($v) {
        $v = json_encode($v) . PHP_EOL;
        file_put_contents('deq_1.txt', $v, FILE_APPEND);
        echo $v;
    }
}

Server

<?php
require __DIR__ . '/vendor/autoload.php';

$redisParams = [
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
];

$config = [
    'params' => $redisParams, // Predis/Client $params
    // 'options' => [] // Predis/Client $options
    // 'bucketCount' => DQ::DEFAULT_BUCKET_THREAD
    // 'queueName' => DQ::DEFAULT_QUEUE,
    // 'bucketPrefix' => DQ::DEFAULT_QUEUE_BUCKET,
    // 'partialCount' => DQ::HASH_PARTIAL,
];


$scanner = new DQ\Server\Scanner($config);
$scanner->run();

dq's People

Contributors

outman avatar

Watchers

James Cloos avatar djx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.