如何在PHP和MYSQL中使用memcached

时间:2015-05-14 13:47:11

标签: php mysql memcached

有人可以帮我解决如何通过PHP连接mySQL以使用memcached服务器吗?

我有以下脚本,需要使用memcached:

$dbserver="server";
$dbuser="user";
$dbpass="pass";
$dbname="db";
function dbcon(){
  @$spojenie=mysql_connect($GLOBALS["dbserver"],$GLOBALS["dbuser"],$GLOBALS["dbpass"]);
  mysql_query("SET NAMES utf8");
  if (!$spojenie){
    die('error');
  }
  mysql_select_db($GLOBALS["dbname"],$spojenie);
  return $spojenie;
}
$dbspojenie=dbcon();
$GLOBALS["dbspojenie"]=&$dbspojenie; 

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

阅读手册memcache

 <?php
    $mysqli = new mysqli("host", "user", "passwd", "database");
    $memc = new Memcached();
    $memc->addServer("host", 11211);
    mysqlnd_memcache_set($mysqli, $memc, NULL, 'var_dump');

    /* This query will be intercepted and executed via Memcache protocol */
    echo "Sending query for id via Memcache: ";
    $mysqli->query("SELECT f1, f2, f3 FROM test WHERE id = 1");

    /* f1 is not configured as valid key field, this won't be sent via Memcache */
    echo "Sending query for f1 via Memcache: ";
    $mysqli->query("SELECT id FROM test WHERE f1 = 1");

    mysqlnd_memcache_set($mysqli);

    /* Now the regular MySQL protocol will be used */
    echo "var_dump won't be invoked: ";
    $mysqli->query("SELECT f1, f2, f3 WHERE id = 1");

    ?>