This example describes how PHP uses the Memcache caching technique to improve response speed. Share to everyone for your reference. The specific analysis is as follows:
Although the PHP has been done very quickly, but if the large amount of data will still have some cards, here is a introduction to PHP using Memcache caching technology to improve response speed, there is a need to know friends can refer to.
Memcache can be used under Linux, but also in Windows system, of course, the Linux system, as to how to install the Memcache,google will come out.
Here's an example of a memcache, with the following code:
Copy Code code as follows:
<?php
Connection
$mem = new Memcache;
$mem->connect ("127.0.0.1", 11211) or die ("could not Connect");
Display version
$version = $mem->getversion ();
echo "Memcached Server version:". $version. " <br> ";
Save data
$mem->set (' key1 ', ' This is ', ' 0, 60 ');
$val = $mem->get (' Key1 ');
echo "Get Key1 Value:". $val. " <br> ";
Replace data
$mem->replace (' key1 ', ' This is replace value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 Value:". $val. "<br>";
Save Array
$arr = Array (' AAA ', ' BBB ', ' CCC ', ' ddd ');
$mem->set (' Key2 ', $arr, 0, 60);
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
Delete data
$mem->delete (' Key1 ');
$val = $mem->get (' Key1 ');
echo "Get Key1 Value:". $val. "<br>";
Clear All data
$mem->flush ();
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "<br>";
Close connection
$mem->close ();
?>
Example 2, the code is as follows:
Copy Code code as follows:
<?php
Using the Memcache cache
$MC = Memcache_connect (' localhost ', 11211);
$PN = $MC->get (' pid ');
Echo $PN;
if ($pn <1) $PN = 1;
else $PN + +;
$MC->set (' pid ', $PN, 0,0); Setting never Expires
Memcache_close ($MC);
?>
I hope this article will help you with your PHP program design.