I’ve this code in php using memcache :
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 1) or die ("Failed to save data at t\
he server");
echo "Store data in the cache (data will expire in 1 second)<br/>\n";
$i=0;
do{
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
$i++;
}while($get_result);
echo $i;
var_dump($get_result);
the value of $i is different every time.. Sometimes it is 140, sometimes it is 900+, etc… Why does it change ?
edit –
btw, that is an example program for memcached from php website with minor alterations.
edit
I guess I wasn’t clear enough with question :
I meant to ask, why does the $memcache->get('key') execution takes more cpu times in some cases while it takes less cpu time in other cases.
=> more loops means $memcache->get('key') happened to take less time, less loops means it took more cpu time.
Keep in mind that cache is set to expire within 1 second and do - while executes until it expires.
Your loop keeps running and is reading the same key over and over again, until it is removed from memcache.
The expire time for that key is set to 1 second, so the number of times the loop runs is the number of times the value can be read in 1 second. The expire time is just an indication. It doen’t necessarily have to be 1 second exact to the millisecond. And the speed of your script can depend on various other conditions as well, so this counter isn’t very reliable.