PHP queue framework WanQueue/file/redis queue-PHP source code

Source: Internet
Author: User
PHP queue framework WanQueue file redis queue PHP queue framework WanQueue/file queue, you can quickly switch to any queue such as reids/mysql. Happy/convenient/Simple.

Run php WanQueue/console/QueueServer. php to enable the Queue Service.

// Add a task to the queue
$ Que = new WanQueue \ Queue ();
$ Que-> push (new WanQueue \ Jobs \ Email ('2017 @ 163.com ',' I love wanwan ',' I love you! '));

Usage

 Push (new WanQueue \ Jobs \ Email ('2017 @ 163.com ',' I love wanwan ',' I love you! '));

Job example

 Email = $ email; $ this-> title = $ title; $ this-> content = $ content;} function handle () {// TODO: Implement handle () method. echo 'email to: ', $ this-> email, 'theme:', $ this-> title, 'Send success! ', "\ N ";}}

Redis driver queue

 QueueName = $ queueName; $ this-> getRedisQueue ();}/*** get queue --- redis */private function getRedisQueue () {if ($ this-> queue = null) {try {$ this-> queue = new \ Redis (); $ this-> queue-> pconnect ('123. 0.0.1 ', 6379);} catch (Exception $ e) {file_put_contents (date ('Y-m-D '). '-redis. log', date ('Y-m-d H: I: s '). ':'. $ e-> getMessage (), FILE_APPEND) ;}}/ *** get queue ----- file version */private function getQueue () {$ queue = _ DIR _. "/Queue/{$ this-> queueName}. queue"; if (! Is_file ($ queue) {return [];} return json_decode (file_get_contents ($ queue), true );} /*** queue persistence ---- file version */private function setQueue () {if (! Is_dir (_ DIR __. "/Queue") {mkdir (_ DIR __. "/Queue");} file_put_contents (_ DIR __. "/Queue/{$ this-> queueName }. queue ", json_encode ($ this-> queue);}/*** queue ** @ param $ value * @ return int */function push ($ value) {return $ this-> queue-> rpush ($ this-> queueName, serialize ($ value);}/*** queue ** @ return mixed */function pop () {return unserialize ($ this-> queue-> lpop ($ this-> queueName ));} /*** start to join the queue * @ param $ value * @ return int */function prePush ($ value) {return $ this-> queue-> lpush ($ this-> queueName, serialize ($ value ));} /*** start from the end * @ return mixed */function popL () {return unserialize ($ this-> queue-> rpop ($ this-> queueName ));} /*** save the queue */function _ destruct () {// TODO: Implement _ destruct () method. // $ this-> setQueue ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.