Required Environment:
PHP 5.3.3
Apache 2.2.7
MySQL 5.5.8
Download Related documents:
Click to download
Extract memcached_1.2.5 document, cmd execute memcached.exe-d-install
Unzip the Php5.3_vc6_memcachedll document and copy the Php_memcache.dll file to the Ext file directory in the PHP installation directory.
Then fill in the php.ini with this sentence: extension= "Php_memcache.dll"
Under Phpinfo (), see if the memcache extension is referenced.
Test Code:
Copy Code code as follows:
<?php
Connection
$mem = new Memcache;
$mem->connect ("127.0.0.1", 11211);
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 ();
$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 "";
}
?>
the application of the news system:
Copy Code code 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" => $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);