i use memecached on my website (php, mysql, apache, ubuntu..) it work very fast but on "peak hours" i get a lot of "could not connect" error message, sometime users need to refresh 5 time for see the content.
I have 24 gb ram on my server and "top" command give me this for the memcached process:
4135 nobody 20 0 161m 37m 836 S 7 (%CPU) 0.2 (%MEM) 0:07.06 memcached
I launch memcached server like this:
memcached -d -u nobody -m 8192 -p 12000 -c 11212
And this is my PHP function:
<?
function cache_sql($query,$update,$time=0)
{
$m = new Memcache;
$m->connect('localhost', 11211) or die ("Could not connect");
$file = $m->get(md5($query));
if($update == 1)
{
$results = mysql_query($query);
while($data = mysql_fetch_array($results)){$records[] = $data;}
if(!$file)
{
$m->set(md5($query), $records, NULL, $time);
}
else
{
$m->replace(md5($query), $records, NULL, $time);
}
}
else
{
if(!$file)
{
$results = mysql_query($query);
while($data = mysql_fetch_array($results)){$records[] = $data;}
$m->set(md5($query), $records, NULL, $time);
return $records;
}
else
{
return $file;
}
}
}
?>
I am doing it right?
I think you accidentally flipped the -c and -p parameters when starting memcached:
memcached -d -u nobody -m 8192 -p 11212 -c 12000
replace
$m->connect('localhost', 11211) or die ("Could not connect");
with
$m->addServer('localhost', 11211) or die ("Could not connect");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With