PDO Memcached PHP

PDO Memcached PHP,php,mysql,pdo,memcached,Php,Mysql,Pdo,Memcached,我正试图将memcached实现到我的服务器中,因为数据库变得如此之快。我想知道如何将其实现到以下代码中: function getWorkers($db) { $meminstance = new Memcache(); $meminstance->pconnect('localhost', 11211); $del = $db->prepare('SELECT DISTINCT(address) from works'); $del-&g

我正试图将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>';
    }
}
函数getWorkers($db) { $meminstance=newmemcache(); $meminstance->pconnect('localhost',11211); $del=$db->prepare('SELECT DISTINCT(address)from works'); $del->execute(); $arr=$del->fetchAll(); $works=getMaxWork($db); foreach($arr作为$a) { $del=$db->prepare('SELECT*FROM works WHERE address=\'.$a[0].\''.); $del->execute(); $work=$del->rowCount(); 回声'; echo'.htmlspecialchars($a[0],ENT_引号,'UTF-8'); 回音“.$work.”; 回音''。圆形(整数($work)/整数($works)*100,2.''; 回声'; } } 给你

  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>';
    }
}

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

您不需要再次查询每一行,因为您已经收到初始sql查询中的所有行,只需将您通常
echo
的HTML缓存为字符串,除非您真的从在其他位置准备好查询结果中获益。这里您所需要的是,但您应该真正考虑一种好的缓存机制,因为缓存在应用程序中不是一件小事。缓存很容易。。。缓存失效,这是一件糟糕的事情;)我得到一个PHP致命错误:未捕获异常“PDOException”,消息为“您无法序列化或取消序列化PDOStatement实例”
if ( Get data from memcache passed )
  {
    return result;
  }
  else
  {
    get the data from database
    save it into memcache
   }