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 (accordingtimestamp
Sort 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.