PHP利用memcache缓存技术提高响应速度

  1. PHP下memcache模块是一个高效的守护进程,提供用于内存缓存的过程式程序和面向对象的方便的接口,特别是对于设计动态web程序时减少对数据库的访问。memcache也提供用于通信对话(session_handler)的处理。 
  2.     memcache既可以在linux下使用,也可以在windows系统下使用。
<?php
header("content-type:text/html;charset=utf-8");
$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>"; 
//关闭连接 www.bcty365.com 
$mem->close(); 
?>

 

posted @ 2016-02-01 15:35  侠岚之弋痕夕  阅读(261)  评论(0编辑  收藏  举报
Where is the starting point, we don't have a choice, but the destination where we can pursue!