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('key1', 'This is first value', 0, 60);
- $val = $mem->get('key1');
- echo "Get key1 value: " . $val ."<br>";
-
- $mem->replace('key1', 'This is replace value', 0, 60);
- $val = $mem->get('key1');
- echo "Get key1 value: " . $val . "<br>";
-
- $arr = array('aaa', 'bbb', 'ccc', 'ddd');
- $mem->set('key2', $arr, 0, 60);
- $val2 = $mem->get('key2');
- echo "Get key2 value: ";
- print_r($val2);
- echo "<br>";
-
- $mem->delete('key1');
- $val = $mem->get('key1');
- echo "Get key1 value: " . $val . "<br>";
-
-
- $mem->flush();
- $val2 = $mem->get('key2');
- echo "Get key2 value: ";
- print_r($val2);
- echo "<br>";
-
- $mem->close();
- ?>
例子2,代码如下:
- <?php
-
- $mc = memcache_connect('localhost', 11211);
- $pn = $mc->get('pid');
- echo $pn;
- if($pn<1) $pn = 1;
- else $pn++;
- $mc->set('pid',$pn,0,0);
- memcache_close($mc);
- ?>
|