Giter Club home page Giter Club logo

litatd's Introduction

延时队列 atd

变量解释

$redisHandler;  //redis链接句柄, Task与listen 使用时需长连接
$topic; //话题
$topics; //话题数组
$message; //消息体

启动服务

//连接redis
$redisHandler = new \Redis();
$redisHandler->pconnect("192.168.1.163"); //需使用长连接

//延时队列服务
\Lit\Atd\Task::init($redisHandler)->run();

发布一条延时消息

use \Lit\Atd\Mapper\Message;

//连接redis
$redisHandler = new \Redis();
$redisHandler->connect("192.168.1.163");

//$message 消息体, 如果消息体重复会重置消息时间, 使用 $message->uniq_id 避免此问题
$message = new Message();
$message->topic = "topic1";
$message->body = "消息体!" ;;//. uniqid();
$message->uniq_id = ""; //唯一ID, 如果需要延期某条消息或者删除某条消息,需记录此ID
//$time 要执行的10位时间戳
$time = time() + 1;

var_dump(\Lit\Atd\Client::init($redisHandler)->at($time, $message));

消费延时队列

//连接redis
$redisHandler = new \Redis();
$redisHandler->connect("192.168.1.163");

$topic = "topic1";
$msg = \Lit\Atd\Client::init($redisHandler)->pop($topic);
if (null !== $msg) {
    var_dump($msg->uniq_id, $msg->body, $msg->topic);
}

监听延时队列

监听延时队列, 并使用回调函数执行相应操作

//连接redis
$redisHandler = new \Redis();
$redisHandler->pconnect("192.168.1.163"); //需使用长连接

//$topics 要监听的topic 必须为数组, 可同时监听多个 topic
$topics[] = "topic1";
$topics[] = "topic2";
\Lit\Atd\Client::init($redisHandler)->listen($topics, function (Message $message) {
    var_dump($message->topic, $message->body, $message->uniq_id);
});

删除未发布的消息

use \Lit\Atd\Mapper\Message;

//连接redis
$redisHandler = new \Redis();
$redisHandler->connect("192.168.1.163");

//$message 消息体
$message = new Message();
$message->topic = "topic1";
$message->body = "消息体!";// . uniqid();
$message->uniq_id = ""; //唯一ID, 需和写入时的值相同

var_dump(\Lit\Atd\Client::init($redisHandler)->remove($message));

litatd's People

Contributors

ghostong avatar

Stargazers

 avatar

Watchers

James Cloos avatar  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.