ow=$result->fetch_array();
$i++;
}
}
}
if($store)
{
$result=$mysqli->query("select `id` from store_new where `member`=".$this->data['id']);
if($result){
$row=$result->fetch_array();
$i=0;
while($row)
{
$this->store[$i]=new Store($row[0]);
$row=$result->fetch_array();
$i++;
}
}
}
$mysqli->close();
}
(部分敏感函数已隐藏)
?
另外,在对前端的AJAX响应的php脚本中,加入缓存的支持,由于有赞的api调用速度较慢,然而这种分销商城的数据对实时性的要求并不是很高,故采用服务器缓存的方式来减少与有赞通讯这部分导致的速度减缓.另外,本地化的对象代替从远程数据库读取信息,也减少了查询数据库的开支.Cache对象的实现如下,将对象的序列化代码存于服务器本地:
?
3600*24)return false;
return $array['data'];
}
static public function updateCache($string,$data)
{
$array=array();
$array['time']=time();
$array['data']=$data;
$file=fopen($string.".ser","w");
fwrite($file,serialize($array));
fclose($file);
}
}
后端AJAX响应页面根据Cache类的反馈选择获取本地数据,或是请求服务器更新本地数据:
?
?
fetch_array();
echo $row[0];
exit(0);
case 'get':
if($array=Cache::readCache("member"))
{
echo json_encode($array);
}
else
{
$db=new Database();
$db->init(false,true);
$arr=array();
for($i=0;$i
member);$i++)
{
$arr[$i]=array();
$arr[$i]=$db->member[$i]->data;
$arr[$i]['password']=null;
$arr[$i]['name']=iconv("GBK","utf-8",$arr[$i]['name']);
$arr[$i]['nickname']=iconv("GBK","utf-8",$arr[$i]['nickname']);
$arr[$i]['sell']=$db->member[$i]->getSell();
}
Cache::updateCache("member",$arr);
echo json_encode($arr);
}
exit(0);
经过以上优化,原本响应时间在15s左右的页面,现在响应时间为0.5s左右.
?