有没有办法手动将mysql查询结果缓存到txt文件中?
Ex:
$a=1;
$b=9;
$c=0;
$cache_filename = 'cached_results/'.md5("$a,$b,$c").'.txt';
if(!file_exists($cache_filename)){
$result = mysql_query("SELECT * FROM abc,def WHERE a=$a AND b=$b AND c=$c");
while($row = mysql_fetch_array($result)){
echo $row['name'];
}
// Write results on $row to the txt file for re-use
}else{
// Load results just like $row = mysql_fetch_array($result); from the txt file
}
原始查询包含更多使用多个表的 WHERE 和联接。
那么,这可能吗?如果是这样,请解释一下。
谢谢你,
PN123
如果您确定数据的生存时间很长,那么您当然可以通过将数据临时保存到文本文件来缓存数据。
if (!file_exists($cachefile)) {
// Save to cache
$query=mysql_query('SELECT * FROM ...');
while ($row=mysql_fetch_array($query))
$result[]=$row;
file_put_contents($cachefile,serialize($result),LOCK_EX);
}
else
// Retrieve from cache
$result=unserialize(file_get_contents($cachefile));
foreach ($result as $row)
echo $row['name'];
尽管如果考虑性能的话,使用 APC、MemCache 或 XCache 会是更好的选择。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)