第一次接触内存缓存;这是我的代码:
$memcache = new Memcache();
$memcache->connect('127.0.0.1', 11211) or die('Memcache connection error');
// set the key then check the cache
$key = md5(' SELECT * FROM `users` ');
$get_result = $memcache->get($key);
if($get_result) {
echo "Data Pulled From Cache";
var_dump($get_result);
}
else {
$query = ' SELECT * FROM `users` ';
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$memcache->set($key, $row, TRUE, 20); // stored for 20 seconds
echo "Data Pulled from the Database";
var_dump($row);
}
我的用户表中有 50,000 个虚拟用户,为什么 var dump 只显示第一个用户?如您所见,查询中没有限制子句。
最佳答案
您只从 $result
资源中获取一行。如果你想要一个完整的行集,你需要在 while
循环中获取每一行并将它们附加到一个数组中:
$rowset = array();
while($row = mysql_fetch_assoc($result)) {
// Fetch all rows in loop, appending each onto $rowset
$rowset[] = $row;
}
// Store $rowset into memcache
$memcache->set($key, $rowset, TRUE, 20); // stored for 20 seconds
关于php - 为什么 memcache->get() 只从数据库中检索第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098817/