memcached persistent connection

59 views
Skip to first unread message

siva

unread,
Feb 24, 2014, 10:01:02 PM2/24/14
to memc...@googlegroups.com
The following is the code snippet i am using to create a memcahed object with persistantId.
First time when I invoke the script it is going to "if" block adds a server, but for all requests thereafter it should go to "else" block. but it is going to "if" block as the getServerList() is returning zero for subsequent requests.

can someone correct if I am doing something wrong

$memcache = new Memcached('mempool');
if(!count($memcache->getServerList()))
{
              //echo ("new pool created has been added");
$memcache->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
return $memcache->addServer('127.0.0.1', 11211);
 
}
else{
return true;
}

Ryan McElroy

unread,
Mar 3, 2014, 5:27:23 PM3/3/14
to memc...@googlegroups.com
What do you mean by invoking the script in this case? Are you invoking it from the command line, or from a web server?

I just did some testing and it looks like PHP Memcached objects with persistence IDs are stored per-PHP-thread.

So if you have 40 PHP threads in your web server, you will have 40 PHP objects that you need to assign servers to before you're guaranteed that the persistence will grab you one that already has the servers assigned. If, on the other hand, you are running from the command line, when the script finishes, the PHP thread will terminate and the Memcached object will be destroyed.

Otherwise, your code looks to me like it will work, but you can just drop the count() call to make it look cleaner, since an empty array evaluates to "false" in an if statement in PHP.

~Ryan


--
 
---
You received this message because you are subscribed to the Google Groups "memcached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to memcached+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages