php - 为什么 memcache->get() 只从数据库中检索第一行?

标签 php mysql memcached

第一次接触内存缓存;这是我的代码:

    $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/

相关文章:

php - 执行触发器时出现 SQL DELIMITER 错误

ruby-on-rails - 为 memcached 和 Rails 组合片段和对象缓存的最佳方法

Python memcache 在 get_multi() 中保留顺序

php - ajax 类似结构

php - 从函数或方法返回 html 代码是个坏主意吗?

php - 在php中匹配不同的字符串

ruby-on-rails - 需要 LRU 缓存时,Memcached 与 Redis 作为 Rails.cache

php - 如何使用 Codeigniter mymodel 类在多个 where 条件下进行更新?

php - Symfony2 FOSRestBundle handleView() 不起作用

php - 我可以从字符串中删除随机数字和字母吗?但只保留重要的