Home > php教程 > PHP源码 > PHP使用memcache缓存技术提高响应速度的方法_php技巧

PHP使用memcache缓存技术提高响应速度的方法_php技巧

PHP中文网
Release: 2016-05-25 17:09:54
Original
1201 people have browsed it

本文实例讲述了PHP使用memcache缓存技术提高响应速度的方法。分享给大家供大家参考。具体分析如下:

php虽然己经做到很好很快了,但是如果大数据量时还是会有些卡了,这里介绍一下PHP中使用memcache缓存技术提高响应速度方法,有需要了解的朋友可参考.

memcache既可以在linux下使用,也可以在windows系统下使用,当然首推linux系统,至于如何安装memcache,google一下就什么都出来了.

下面贴一段memcache的使用例子,代码如下:

<?php 
//连接 
$mem = new Memcache; 
$mem->connect("127.0.0.1", 11211) or die ("Could not connect"); 
//显示版本 
$version = $mem->getVersion(); 
echo "Memcached Server version:  ".$version."<br>"; 
//保存数据 
$mem->set(&#39;key1&#39;, &#39;This is first value&#39;, 0, 60); 
$val = $mem->get(&#39;key1&#39;); 
echo "Get key1 value: " . $val ."<br>"; 
//替换数据 
$mem->replace(&#39;key1&#39;, &#39;This is replace value&#39;, 0, 60); 
$val = $mem->get(&#39;key1&#39;); 
echo "Get key1 value: " . $val . "<br>"; 
//保存数组 
$arr = array(&#39;aaa&#39;, &#39;bbb&#39;, &#39;ccc&#39;, &#39;ddd&#39;); 
$mem->set(&#39;key2&#39;, $arr, 0, 60); 
$val2 = $mem->get(&#39;key2&#39;); 
echo "Get key2 value: "; 
print_r($val2); 
echo "<br>"; 
//删除数据 
$mem->delete(&#39;key1&#39;); 
$val = $mem->get(&#39;key1&#39;); 
echo "Get key1 value: " . $val . "<br>"; 
//清除所有数据 
$mem->flush(); 
$val2 = $mem->get(&#39;key2&#39;); 
echo "Get key2 value: "; 
print_r($val2); 
echo "<br>"; 
//关闭连接 
$mem->close(); 
?>
Copy after login


例子2,代码如下:

<?php 
//使用memcache缓存 
$mc = memcache_connect(&#39;localhost&#39;, 11211); 
$pn = $mc->get(&#39;pid&#39;); 
echo $pn; 
if($pn<1) $pn = 1; 
else $pn++; 
$mc->set(&#39;pid&#39;,$pn,0,0); //设置永不过期 
memcache_close($mc); 
?>
Copy after login


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template