【问题标题】:PDO Memcached PHPPDO 内存缓存 PHP
【发布时间】:2014-03-16 08:06:29
【问题描述】:

由于数据库变得如此之快,我正在尝试在我的服务器中实现 memcached。我想知道如何在这段代码中实现它:

    function getWorkers($db)
{
    $meminstance = new Memcache();
    $meminstance->pconnect('localhost', 11211);

    $del = $db->prepare('SELECT DISTINCT(address) from works');
    $del->execute();
    $arr = $del->fetchAll();
    $works = getMaxWork($db);

    foreach($arr as $a)
    {   
        $del = $db->prepare('SELECT * FROM works WHERE address = \'' . $a[0] . '\'');
        $del->execute();
        $work = $del->rowCount();

        echo '<tr>';
        echo '<td>' . htmlspecialchars($a[0], ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td>' . $work . '</td>';
        echo '<td>' . round(intval($work)/intval($works)*100, 2) . '</td>';
        echo '</tr>';
    }
}

【问题讨论】:

  • 您不需要再次查询每一行,因为您已经收到了初始 sql 查询中的所有行
  • 只需将您通常echo 的 HTML 缓存为字符串,除非您真正受益于在其他位置准备好查询结果。
  • 这里只需要de2.php.net/manual/de/memcache.get.phpde2.php.net/manual/de/memcache.set.php,但你真的应该考虑一个好的缓存机制,因为缓存在应用程序中并不是一件小事
  • 缓存很容易...缓存失效,这太糟糕了;)

标签: php mysql pdo memcached


【解决方案1】:

给你

  function getData($db)
{
    $meminstance = new Memcache();
    $meminstance->pconnect('localhost', 11211);

    $sqlQuery = 'SELECT DISTINCT(address) from works';
    $memcacheKey = md5($sqlQuery);

    if ( $arr = $meminstance->get(memcacheKey) )
    {
        // its in cache do nothing

    }
    else
    { 
        // its not in cache, so we came here, lets now get it from db and cache it
        $del = $db->prepare($sqlQuery);
        $del->execute();
        $arr = $del->fetchAll();
        $works = getMaxWork($db);

        // lets now cache it
           $meminstance->set(memcacheKey,$arr);
      }



    foreach($arr as $a)
    {   
        $sqlQuery = 'SELECT * FROM works WHERE address = \'' . $a[0] . '\'';
         $memcacheKey = md5($sqlQuery);
         if ( $del =  $meminstance->get($memcacheKey))
           {
                 //its in cache yaaaaaaaa :D
             }
           else
           {
             $del = $db->prepare('SELECT * FROM works WHERE address = \'' . $a[0] . '\'');
            $del->execute();
             $work = $del->rowCount();
             // lets cache it here
             $meminstance->set($memcacheKey,$del);

          }


        echo '<tr>';
        echo '<td>' . htmlspecialchars($a[0], ENT_QUOTES, 'UTF-8') . '</td>';
        echo '<td>' . $work . '</td>';
        echo '<td>' . round(intval($work)/intval($works)*100, 2) . '</td>';
        echo '</tr>';
    }
}

在您的网站中使用 memcache 时的逻辑。是 md5 的 sql 查询,所以它们将是唯一的。您首先尝试从 memcached 中获取(您 md5 sql 查询),因此它将成为关键。如果你没有得到任何东西,你从数据库中获取数据,然后将其保存到 memcache 中。这意味着您 md5 sql Query 使其成为键,因此它将是唯一的,并且您将从数据库返回的结果作为其值传递

key ===> md5(Sql 查询) value ==> 对象,从数据库中获取的结果集

//伪代码

if ( Get data from memcache passed )
  {
    return result;
  }
  else
  {
    get the data from database
    save it into memcache
   }

注意:如果您在单个服务器上运行,最好查看 APC 而不是 memcached

【讨论】:

  • 我收到一个 PHP 致命错误:未捕获的异常 'PDOException' 并带有消息“您无法序列化或反序列化 PDOStatement 实例”
猜你喜欢
  • 1970-01-01
  • 2010-12-13
  • 2014-03-12
  • 2015-08-13
  • 1970-01-01
  • 2013-03-12
  • 2011-11-22
  • 2012-05-24
  • 2012-07-26
相关资源
最近更新 更多