Data is first written into redis using a queue, and then scheduled tasks are used to write mysql. How can this problem be achieved?

Source: Internet
Author: User
Tags php cli
I have never touched on redis before. Can I explain in detail how to use it? Baidu hasn't found a typical example. I have never touched redis before. Can you explain how to use it in detail? Baidu has not found a typical example.

Reply content:

I have never touched on redis before. Can I explain in detail how to use it? Baidu has not found a typical example.

Use the php-worker mode. This should be roughly the case

$cache_data = $redis->get_all();$redis->flush_all();$sqlArr = [];for($item in $cache_data){    array_push($sqlArr,sql_gen($item));}execute_sqls($sqlArr);sleep($sleep_time);

Background running:

Chmod a + x/png/php_daemon # Add the execution permission nohup/png/php_daemon>/png/php_daemon.log 2> & 1 &

You can also add it to/etc/rc. local to enable it to run automatically upon startup:

/png/php_daemon >/png/php_daemon.log

Php_daemon content:

#! /Png/php/7.0.0/bin/php
  

Supplement:
PHP provides two predefined variables for receiving command line parameters in PHP scripts:
Http://php.net/manual/zh/reserved.variables.php
$ Number of parameters passed to the script by argc
$ Argv [] parameter array passed to the script

Getopt can be used to obtain the command line parameter options and return an associated array of parameter names and values.
Http://php.net/manual/zh/function.getopt.php
Separate characters (not allowed)
Followed by a colon (this option requires a value)
Followed by two colons (optional)

#!/png/php/5.4.39NTS/bin/php 
Chmod + x cli. php./cli. php-a1024-b2048 or php cli. php-a1024-b2048

Output:

Array( [a] => 1024 [b] => 2048)

Comment out$argv = getopt("a:b:")Output:

Array( [0] => ./cli.php [1] => -a1024 [2] => -b2048)

Write a daemon and keep reading redis.

1. Program startup
2. Read new data (accordingtimestampSort To get the oldest)
3. No data, sleep for 1 second
3. If there is data, operate mysql, and update the timestamp of the last checktimestamp

Do you know that redis has the Message Queue function, so that other threads can use the blpop blocking read mode to read messages from the queue.

Use this: https://github.com/Footstones/Plumber

Two methods can be considered:
(1) The list is used as the cache and the producer/consumer mode is used.
(2) Pub/Sub, subscribe to messages, write MySQL

In the past, I also asked this question in stackoverflow. Many answers are that redis uses list storage and then sets a cron task that runs every minute to read this list. Why not?while (true)This is because long-running php processes may cause memory leakage.

But now I am trying to use rabbitmq to implement the queue, and the effect is quite good.

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.