Analysis of the application of Memcache in PHP _php tutorial

Source: Internet
Author: User
Required Environment:
PHP 5.3.3
Apache 2.2.7
MySQL 5.5.8
Related Documents Download: Click to download
Unzip the memcached_1.2.5 document, execute memcached.exe-d under cmd-install
Unzip the Php5.3_vc6_memcachedll document and copy the Php_memcache.dll file to the Ext file directory of the PHP installation directory.
Then fill in this sentence in the php.ini: extension= "Php_memcache.dll"
Under Phpinfo (), see if the memcache extension is referenced.

Test Code:
Copy CodeThe code is as follows:
Connection
$mem = new Memcache;
$mem->connect ("127.0.0.1", 11211);
Save data
$mem->set (' key1 ', ' This is first value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "
";
Replace data
$mem->replace (' key1 ', ' This is replace value ', 0, 60);
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "
";
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 "
";
Delete data
$mem->delete (' Key1 ');
$val = $mem->get (' Key1 ');
echo "Get Key1 value:". $val. "
";
Clear All data
$mem->flush ();
$val 2 = $mem->get (' Key2 ');
echo "Get Key2 value:";
Print_r ($val 2);
echo "
";
Close connection
$mem->close ();
$memcachehost = ' 192.168.10.1 ';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect ($memcachehost, $memcacheport) or Die ("Could not Connect");
$query = "SELECT * from user limit 10";
$key =md5 ($query);
if (! $memcache->get ($key))
{
$conn =mysql_connect ("192.168.30.1", "root", "passwd");
mysql_select_db (users);
$result =mysql_query ($query);
while ($row =mysql_fetch_assoc ($result))
{
$arr []= $row;
}
$f = ' db ';
$memcache->add ($key, Serialize ($arr), 0,30);
$data = $arr;
}
else{
$f = ' mem ';
$data _mem= $memcache->get ($key);
$data = unserialize ($data _mem);
}
Echo $f;
echo "";
Print_r ($data);
foreach ($data as $a)
{
echo $a [user_id]._. $a [email];
echo "";
}
?>

application of the news system:
Copy CodeThe code is as follows:
==============memcache
$memcachehost = ' 127.0.0.1 ';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect ($memcachehost, $memcacheport) or Die ("Could not Connect");
============== News
$sql = "Select Id,title,left (title,16) as Biaoti,date_time from ' p_newsbase ' where shenhe= ' 1 ' ORDER by ID DESC limit 7";
$query = $db->query ($sql);
$key =md5 ($query);
while ($row _news= $db->fetch_array ($query)) {
$str = $row _news[' Biaoti ']. $db->time_out ($row _news[' date_time ');
$SM _news[]=array ("name" + $str, "title" + $row _news[' title '), "id" = = $row _news[' id '], "date_time" and "= $row _ news[' date_time ');
}
if (! $memcache->get ($key)) {
$memcache->add ($key, serialize ($sm _news), 0, $memcachelife);
}else{
$data _mem= $memcache->get ($key);
$sm _news = unserialize ($data _mem);
}
$smarty->assign ("Sm_news", $sm _news);

http://www.bkjia.com/PHPjc/327650.html www.bkjia.com true http://www.bkjia.com/PHPjc/327650.html techarticle Required Environment: PHP 5.3.3 Apache 2.2.7 mysql 5.5.8 related documents download: Click to download Unzip memcached_1.2.5 document, cmd execution memcached.exe-d-install will php5.3_vc6_ Memcachedll-Wen ...

  • 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.