问题:将几十万条的PHP缓存文件更新到新的目录下,写了一个自动执行的程序,由于服务器设置了php脚本执行最大时间,所以有时候自动执行的程序会出错,以下是使用ajax的解决方法。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="__JS__/jquery-1.7.2.min.js"></script>
<script language="javascript">let URL = '__URL__/';let ROOT_PATH = '__ROOT__/';let APP = '__APP__/';</script>
<meta name="renderer" content="webkit">
<title>自动更新切换缓存</title>
</head>
<body>
<p><input type="button" value="自动更新切换缓存" id="update_status" onclick="change_cache(1)" /></p>
<input type="hidden" name="ajax_is_ing" value="0" />
</body>
<script type="text/javascript">
/**
* 切换缓存
*/
let add_use_time_trip = window.setInterval(change_cache(0),120000);//120秒
function change_cache(init){
$("#update_status").removeAttr('onclick');
let ajax_is_ing = $("input[name='ajax_is_ing']").val();
if(ajax_is_ing == 1) return false;
$("input[name='ajax_is_ing']").val(1);//开始请求
console.log(123);
let query = new Object();
query.init = init;
$.ajax({
url : APP + "home/Test/change_cache_ajax",
timeout : 120000, //超时时间设置,单位毫秒
data : query,
type : "POST",
success:function(data){
if(data.status == 1) {
console.log(data.data);
$("input[name='ajax_is_ing']").val(0);//结束请求
change_cache(0);
}else if(data.status == 2){
console.log('end');
clearInterval(add_use_time_trip);
$("input[name='ajax_is_ing']").val(0);//结束请求
$("#update_status").attr('onclick',"change_cache(1)");
alert('更新完毕');
}else{
console.log('error');
}
},
error:function(xhr,state,errorThrown){
//程序出错之后 success 里的change_cache()将失效 ,这时候计时器的请求 将会被用到
$("input[name='ajax_is_ing']").val(0);//结束请求
$("#update_status").attr('onclick',"change_cache(1)");
}
});
}
</script>
</html>
/**
* 视频观看记录缓存切换
*/
public function change_cache_ajax(){
$param = I('param.');
if($param['init'] == 1){cookie('start_cache',null);}
$start = cookie('start_cache') ? cookie('start_cache') : 0;
//缓存切换
$start_time = microtime(true);
$order_goods_ids = M('order_goods')
->where(['goods_type'=>1,'status'=>['in',[2,3]]])
->order(['id'=>'asc'])->limit($start,10)
->field('id,uid')->select();
if(empty($order_goods_ids)){
cookie('start_cache',null);
$return['status'] = 2;
$this->ajaxReturn($return);
}
foreach ($order_goods_ids as $ke=>$vo){
$CACHE_PATH = DATA_PATH.'user_'.$vo['uid'].'/';
$course_video_ids = M('my_course_study')->where(['order_goods_id'=>$vo['id']])->getField('id',true);
foreach ($course_video_ids as $k=>$v){
$see_info = S('my_course_study_see_info_'.$v);
if($see_info){
F('my_course_study_id_'.$v,$see_info,$CACHE_PATH);
}
}
}
cookie('start_cache',$start+10);
$cha_time = microtime(true) - $start_time;
$data['start'] = $start;
$data['cha_time'] = $cha_time;
$return['status'] = 1;
$return['data'] = $data;
$this->ajaxReturn($return);
}
执行效果如图: