实例
1、根据id取模后更新分库里的分表数据
2、根据不同的取模选择不同的分库,更新对应的表数据
3、关闭分库,切换总库
4、插入redis消息队列,通过异步定时任务更新总表数据
编辑edit.php
<?php
require './RunDbPdo.php';
require './RedisQ.php';
$model = new RunDbPdo();
$model->configFile = './config/user.config.php';
$redis = new RedisQ();
if (isset($_GET) && !empty($_GET)) {
extract($_GET);
$user_id = isset($user_id) ? $user_id : '';
$sql = "select * from mm_user where user_id='{$user_id}'";
$data = $model->getRow($sql);
}
if (isset($_POST) && !empty($_POST)) {
extract($_POST);
$user_id = isset($user_id) ? $user_id : 0;
$username = isset($username) ? $username : 0;
$age = isset($age) ? $age : 0;
//1、根据id取模后更新分表数据
$d = $user_id % 2;
//2、根据不同的取模选择不同的数据库
$model->configFile = "./config/user{$d}.config.php";
$sql2 = "update mm_user{$d} set username='{$username}',age='{$age}' where user_id='{$user_id}'";
$res2 = $model->query($sql2);
$model->close();//关闭分库,切换总库
//3、这一步应该插入redis消息队列,通过异步定时任务更新总表数据
if($res2) {
$sql = "update mm_user set username='{$username}',age='{$age}' where user_id='{$user_id}'";
//4、压入队列
$redis->lpush('sqls',$sql);
header('location:findAll.php');
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>用户管理系统</title>
<link href="./css/property-system.css" rel="stylesheet">
<link href="./css/operate-system.css" rel="stylesheet">
</head>
<body>
<main>
<section class="content">
<fieldset class="form-list-32">
<h3 class="operate-title-1"><i></i>编辑用户信息</h3>
<form action="edit.php" id="editFormId" method="post" autocomplete="off">
<ul>
<input type="hidden" name="user_id" value="<?= $data['user_id'] ?>">
<li><h6>用户名:</h6>
<aside><input type="text" name="username" value="<?= $data['username'] ?>"
class="tbox30 tbox30-6"/></aside>
</li>
<li><h6>年龄:</h6>
<aside><input name="age" value="<?= $data['age'] ?>" maxlength="11" class="tbox30 tbox30-6"
type="text"></aside>
</li>
<li class="agent-subbtn-wrap mt30px">
<h6> </h6>
<aside><input class="btn-2" type="submit" value="提交"></aside>
</li>
</ul>
</form>
</fieldset>
</section>
</main>
</body>
</html>
执行异步消息队列脚本文件queque.php
<?php
$file_dir = dirname(__FILE__);
chdir($file_dir);
require "./RedisQ.php";
require './RunDbPdo.php';
$redis_boj = new RedisQ();
$model = new RunDbPdo();
$model->configFile = './config/user.config.php';
//4、循环出队列 当sql为空时break结束前循环,sql执行失败的写入文件日志,后期重新执行
$do = true;
while ($do) {
$sql = $redis_boj->rpop('sqls');
if(empty($sql)){
break;
}
$res = $model->query($sql);
if(!$res){
file_put_contents('sql_push',$sql);
}
var_dump($res);
}