Use memcache caching technology in PHP to improve response speed

Source: Internet
Author: User

Memcache can be used either under Linux or in Windows systems, and of course the Linux system.

As for how to install Memcache,google, then everything comes out.

Here is an example of a memcache use:

The code is as follows Copy Code

<?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:

  code is as follows copy code

<?php
//using 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);//Set never Expires
Memcache_close ($MC)
?

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.