Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP memcache connect

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 
}
like image 520
Pit Digger Avatar asked Jul 27 '12 22:07

Pit Digger


1 Answers

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.

like image 62
drew010 Avatar answered Sep 29 '22 20:09

drew010