在实际应用中我们会缓存从数据库中查出来的结果集,以md5($sql)为$key,结果集为值。

以只是在php简单应用代码:

<!DOCTYPE html>
<html>
<head>
<title>memcache demo</title>
<meta http-equiv="content-type"content="text/html;chatset=utf-8">
</head>
<body>
<?php
$server_ip = '127.0.0.1';
$server_port = 11211;
 
$memcache = new Memcache();
$memcache->connect($server_ip,$server_port);
 
$memcache->add("name1","user_name1",MEMCACHE_COMPRESSED,0);
$memcache->add("name2","user_name2",MEMCACHE_COMPRESSED,0);
$array1 = array('name1' => 'jiajiam1',
'age1'=>12,
'country'=>'china');
$memcache->add("other",$array1,MEMCACHE_COMPRESSED,20);
$memcache->set("name3","user_name3",MEMCACHE_COMPRESSED,0);
$memcache->replace("name1","user_name_relpace",MEMCACHE_COMPRESSED,0);
$memcache->replace("123","12345");
 
echo"name1:".$memcache->get("name1")."<br/>";
$memcache->delete("name1");
echo"name1:".$memcache->get("name1")."<br/>";
 
$array_get = array("name1","name2","name3");
 
$result_get = $memcache->get($array_get);
foreach ($result_get as $key => $value) {
echo"$key:--->$value<br/>";
}
foreach ($memcache->getStats() as $key => $value) {
echo"$key:--->$value<br/>";
};
 
echo"<br/>";
 
foreach($memcache->getExtendedStats() as $key => $value) {
echo"$key:--->$value<br/>";
}
$memcache->close();
?>
</body>
</html>
登录后复制

以上就是memcache的简单使用实例代码的详细内容,更多请关注Work网其它相关文章!

09-16 03:24