Home > php教程 > php手册 > PHP中使用memcache缓存技术提高响应速度详解

PHP中使用memcache缓存技术提高响应速度详解

WBOY
Release: 2016-06-13 10:09:10
Original
885 people have browsed it

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

memcache既可以在linux下使用,也可以在windows系统下使用,当然首推linux系统。

至于如何安装memcache,google一下就什么都出来了。

下面贴一段memcache的使用例子:

 代码如下 复制代码

//连接
$mem = new Memcache;
$mem->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', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."
";

//替换数据
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";

//保存数组
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 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:

 代码如下 复制代码

//使用memcache缓存
$mc = memcache_connect('localhost', 11211);
$pn = $mc->get('pid');
echo $pn;

if($pn else $pn++;

$mc->set('pid',$pn,0,0); //设置永不过期
memcache_close($mc);
?>

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