Detailed explanation of using memcache caching technology in PHP to improve response speed_PHP tutorial

WBOY
Release: 2016-07-13 17:06:17
Original
1035 people have browsed it

Although PHP is already very good and fast, it will still get stuck if there is a large amount of data. Now I will introduce to you how to use memcache caching technology in PHP to improve the response speed. Friends who need to know more can refer to.

Memcache can be used under both Linux and Windows systems. Of course, Linux systems are preferred.

As for how to install memcache, just google and everything will come out.

Post an example of memcache usage below:

//Show version
$version = $mem->getVersion();
echo "Memcached Server version: ".$version."
";
The code is as follows
 代码如下 复制代码

//连接
$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();
?>

Copy code

 代码如下 复制代码

//使用memcache缓存
$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);
?>

//Connect
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211) or die ("Could not connect");
//Save data
$mem->set('key1', 'This is first value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val ."
";

//Replace data
$mem->replace('key1', 'This is replace value', 0, 60);
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
";//Save the array
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$mem->set('key2', $arr, 0, 60);
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
"; //Delete data
$mem->delete('key1');
$val = $mem->get('key1');
echo "Get key1 value: " . $val . "
"; //Clear all data
$mem->flush();
$val2 = $mem->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "
"; //Close the connection
$mem->close();
?>
Example 2:
The code is as follows Copy code
//Use memcache cache
$mc = memcache_connect('localhost', 11211);
$pn = $mc->get('pid');
echo $pn; if($pn<1) $pn = 1;
else $pn++;<🎜> <🎜>$mc->set('pid',$pn,0,0); //Set never expires
memcache_close($mc);
?> http://www.bkjia.com/PHPjc/630708.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630708.htmlTechArticleAlthough php has been done very well and quickly, it will still get stuck if there is a large amount of data. Now let me introduce to you how to use memcache caching technology in PHP to improve response speed. If necessary...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template