How PHP uses Memcache caching technology to improve response speed _php tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.