Home > Article > Backend Development > PHP script regularly updates product list
The main content of this article is about the PHP script regularly updating the product list, which has a certain reference value. Now I share it with you. Friends in need can refer to it.
In order to improve the anti-resistance in e-commerce projects Concurrency capability requires caching of the product list. The following is the script used to update the cache:
//PRODUCTION_为缓存商品列表的临时key//PRODUCTION为缓存商品列表key $con = mysqli_connect($db_config['host'],$db_config['user'],$db_config['passwd'],$db_config['database']); $redis = new Redis();$redis->connect($redis_config['host']);$redis->del(PRODUCTION_); $sql = "select * from production ORDER BY created_at"; $result = mysqli_query($con,$sql); $artimages = [];while($row = mysqli_fetch_assoc($result)){ $redis->lPush(PRODUCTION_,json_encode($row)); }/* *之所以不直接更新到商品列表是因为当商品列表数据量比较大的时候,脚本执行的时间长, *在del PRODUCTION之后 lPush PRODUCTION之前 用户访问网站的时候会出现读取不到 *商品列表的情况。 *先存入临时列表,再最后rename为实际被查询的key */$redis->rename(PRODUCTION_,PRODUCTION);
The appeal script is just a demo and needs to be locked during actual application. Otherwise, the crontab will be reached again before the script is executed. When executing automatically, errors will occur.
The following is the crontab script:
* * * * * root /usr/local/php/bin/php /xxx/xxxx/xxx/production.php >/dev/null 2>&1 root 是执行脚本的用户 /usr/local/php/bin/php php脚本解释器,如果不写全路径的话会报错 >/dev/null 2>&1 忽略日志输出
Related recommendations:
PHP script to read local database data
php Script daemon principle and implementation code
Detailed explanation of php script timeout mechanism
The above is the detailed content of PHP script regularly updates product list. For more information, please follow other related articles on the PHP Chinese website!