本文实例讲述了PHP使用memcache缓存技术提高响应速度的方法。分享给大家供大家参考。具体分析如下:
PHP虽然己经做到很好很快了,但是如果大数据量时还是会有些卡了,这里介绍一下PHP中使用memcache缓存技术提高响应速度方法,有需要了解的朋友可参考.
memcache既可以在linux下使用,也可以在windows系统下使用,当然首推linux系统,至于如何安装memcache,google一下就什么都出来了.
下面贴一段memcache的使用例子,代码如下:
代码如下:
connect("127.0.0.1",11211) or die ("Could not connect");
//显示版本
$version = $mem->getVersion();
echo "Memcached Server version: ".$version."
"; //保存数据 $mem->set('key1','This is first value',60); $val = $mem->get('key1'); echo "Get key1 value: " . $val ."
"; //替换数据 $mem->replace('key1','This is replace value',60); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "
"; //保存数组 $arr = array('aaa','bbb','ccc','ddd'); $mem->set('key2',$arr,60); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "
"; //删除数据 $mem->delete('key1'); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "
"; //清除所有数据 $mem->flush(); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "
"; //关闭连接 $mem->close(); ?>
例子2,代码如下:
"; //保存数据 $mem->set('key1','This is first value',60); $val = $mem->get('key1'); echo "Get key1 value: " . $val ."
"; //替换数据 $mem->replace('key1','This is replace value',60); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "
"; //保存数组 $arr = array('aaa','bbb','ccc','ddd'); $mem->set('key2',$arr,60); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "
"; //删除数据 $mem->delete('key1'); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "
"; //清除所有数据 $mem->flush(); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "
"; //关闭连接 $mem->close(); ?>
代码如下:
get('pid');
echo $pn;
if($pn<1) $pn = 1;
else $pn++;
$mc->set('pid',$pn,0); //设置永不过期
memcache_close($mc);
?>
希望本文所述对大家的PHP程序设计有所帮助。