Analyze the application of memcache in php_PHP tutorial

WBOY
Release: 2016-07-21 15:06:00
Original
1116 people have browsed it

Required environment:
php 5.3.3
apache 2.2.7
mysql 5.5.8
Related document download: Click to download
Extract the Memcached_1.2.5 document, execute memcached.exe -d -install under cmd
Extract 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 php.ini: extension="php_memcache.dll"
Check under phpinfo() to see if the memcache extension is referenced.

Test code:

Copy code The code is as follows:

//Connect
$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 the array
$arr = array('aaa ', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get( 'key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//Delete data
$mem ->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
" ;
//Clear all data
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
";
//Close the 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))
                                                                                                                        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 "";
}
?>



News system application:


Copy code
The 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"=>$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);


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327650.htmlTechArticleRequired environment: php 5.3.3 apache 2.2.7 mysql 5.5.8 Related document download: Click to download and decompress Memcached_1 .2.5 document, execute memcached.exe -d -install under cmd to install php5.3_vc6_memcachedll file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template