Home > Article > Backend Development > PHP operates the singleton mode of memcache and tests its mechanism
& lt;? PHPtClass Memcacheopr Extends Memcached
{
PRIVATE Static $ host = '127.0.0.1';
$ Port = 11211;
Public Static Function Getinstance () {
if (!(self::$instance instanceof Memcached)) {
Return self::$instance; ('max_execution_time',60); //The maximum execution time of the php page. The default value here is 30s. If it exceeds, it needs to be reset.
$mcache = MemcacheOpr::getInstance(); // Singleton mode call
$mcache- >flush(); //Clear them all first
$mcache->delete('string1m');
$mcache->delete('string2m');
$mcache->delete('string3m');
$mcache->delete('string4m');
$mcache->delete('string5m');
for($i=0;$i $mcache-> ;delete('string'.$i);
}
/*$mcache->set('somewords','have you learned some thing?');
$output = $mcache->get('somewords ');
echo "output=[$output]
";*/
$begintime = microtime(TRUE);
$input = str_repeat('what',1024*1024*16*2*1.5 ); //192M
$input = substr($input,0,-512);
$mcache->set('string1m',$input);//192M Why can the five contents of 192M be saved successfully? Woolen cloth? It’s because memcached has enabled compression for things stored in memory
$mcache->set('string2m',$input.'areyou2');
$mcache->set('string3m',$input.'areyou3' );
$mcache->set('string4m',$input.'areyou4');
$mcache->set('string5m',$input.'areyou5');
/*for($i= 0;$i $mcache->set('string'.$i, $input);
}*/
$input = str_repeat('what',1048576); / / 4M of stuff is actually stored here. Because memcached has compression turned on, there will be no problem if only a small amount of content is actually stored.
for($i=0;$i $mcache->set('string'.$i,$input);
}
$mcache->delete('somewords ');
$during = microtime(TRUE) - $begintime;
echo 'total time:'.$during.'s
';
file_put_contents('/tmp/string5m.log',$mcache- >get('string1m'));
//echo 'length of $output is:'.strlen($output).",output=[$output]
";
/*$allKeys = $mcache->getAllKeys();
var_dump($allKeys);
print_r($mcache->getStats());*/
In fact, from phpinfo() we can see that for saving If the content exceeds 2000 bytes, memcached will enable compression
If you want to see the memcache usage status on the server, you can check it through the telnet 127.0.0.1 11211 command
Get the actual stored value: get string1m
Use the stats command to display some of the status, such as memory hits
Use stats slabs to display the actual situation of different slab blocksIn fact, each page saves 1m of content by default, and then the 1m content can be divided into many blocks according to the size of the chunksize. These blocks are slab blocks
When the length of the saved content is the same, blocks of the same size will generally be used. When the length exceeds 1.25 times (the default increment multiple), a new slab block will be enabled.
The larger the increment multiple, the greater the waste of memory. If the 8k slab block has been used up and you want to continue to save 8k data, then 10k data will be used (by default), and 2k has actually been wasted. memory data;
If the increment multiple is 2, it will waste 8k of memory.
The above introduces the singleton mode of PHP operating memcache and testing its mechanism, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.