Php Memcache&;Mysqli准备语句问题
检查下面的代码,我有以下问题:SQL语句中的最后两个参数是动态的,我如何使memcache获得正确的参数,而不仅仅是?,哪个只给我看?添加第二个变量$sql1=“从tpost ORDER按id desc LIMIT$var1,$var2选择id title vtext”?还是给出了更好的解决方案Php Memcache&;Mysqli准备语句问题,php,memcached,prepared-statement,Php,Memcached,Prepared Statement,检查下面的代码,我有以下问题:SQL语句中的最后两个参数是动态的,我如何使memcache获得正确的参数,而不仅仅是?,哪个只给我看?添加第二个变量$sql1=“从tpost ORDER按id desc LIMIT$var1,$var2选择id title vtext”?还是给出了更好的解决方案 $sql = "SELECT id, title, vtext FROM tpost ORDER BY id desc LIMIT ?, ?"; $content = $memcache->get
$sql = "SELECT id, title, vtext FROM tpost ORDER BY id desc LIMIT ?, ?";
$content = $memcache->get($sql);
if($content == null) {
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('ii', $offset, $rowsperpage);
$stmt->execute();
$stmt->bind_result($r_id, $r_title, $r_vtext);
while ($stmt->fetch()) {
$data[] = array( 'id' => $r_id, 'title' => $r_title, 'vtext' => $r_vtext);
}
$stmt->close();
$memcache->set($sql,$data,0,$cache_time);
}
感谢您的帮助使用完整的SQL查询作为密钥是不好的做法。创建唯一标识符或至少对其进行哈希运算。原因是,当您的密钥越大,匹配速度越慢,数据传输速度越慢(使用小密钥到memcache服务器的1000r/s比使用大密钥的1000r/s快:)
$sql = "SELECT id, title, vtext FROM tpost ORDER BY id desc LIMIT ?, ?";
$key = "SELECT id, title, vtext FROM tpost ORDER BY id desc LIMIT $r_title, $r_vtext";
$content = $memcache->get($sql);
if($content == null) {
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('ii', $offset, $rowsperpage);
$stmt->execute();
$stmt->bind_result($r_id, $r_title, $r_vtext);
while ($stmt->fetch()) {
$data[] = array( 'id' => $r_id, 'title' => $r_title, 'vtext' => $r_vtext);
}
$stmt->close();
$memcache->set($key,$data,0,$cache_time);
}
数据也可以是空的,如果用户请求超出范围,只检查它并再次进入SQL查询是不明智的
// Generate key
$key = 'recent:'. $offset .':'. $rowsperpage;
// If nothing found within cache instance, retrieve and set it
if(!$data = $memcache->get($key)) {
$sql = "SELECT `id`, `title`, `vtext`
FROM `tpost`
ORDER BY `id` DESC LIMIT ?, ?";
$stmt = $this->$mysqli->prepare($sql);
$stmt->bind_param('ii', $offset, $rowsperpage);
// Retrieve result set
if($stmt->execute()) {
$data = array();
$stmt->bind_result($r_id, $r_title, $r_vtext);
while ($stmt->fetch()) {
$data[] = array(
'id' => $r_id,
'title' => $r_title,
'vtext' => $r_vtext);
}
}
$stmt->close();
// Set cache entry
$memcache->set($key, $data, 0, $cache_time);