dongsu3138
dongsu3138
2012-07-27 22:37

PHP memcache连接

已采纳

I have a page where few thousands of users can hit a method at the same time . I do have following code where I connect every time . Since this will go to a seperate memcache server will this cause slowdowns is there a way to connect just once and reuse that connection ? Do I have to close connection after every request ?

$primary_connected = $memcache_primary->connect($primary_memcache_server, 11211);
if($primary_connected){
        $data = $memcache_primary->get($key);
        if ($data != NULL) {
            return data;
        }
 }
else{
/////Get data from database 
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongmi6102 dongmi6102 9年前

    If you are using the PHP memcached class (the one with the d on the end, not memcache) then yes, you can open a persistent connection.

    You can pass a persistent ID to the constructor which will open a persistent connection and subsequent instances that use the same persistent ID will use that connection.

    $memcached = new Memcached('method_name_or_persistent_identifier');
    $memcached->addServer(...);
    // use it
    

    Hope that helps.

    See Memcached::__construct() for more details.

    点赞 评论 复制链接分享