Home  >  Article  >  Backend Development  >  Simple usage example code of memcache

Simple usage example code of memcache

零下一度
零下一度Original
2017-07-27 15:42:491032browse

In actual applications, we will cache the result set found from the database, using md5($sql) as the $key and the result set as the value.

To just simply apply the code in 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 = &#39;127.0.0.1&#39;;
$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(&#39;name1&#39; => &#39;jiajiam1&#39;,
&#39;age1&#39;=>12,
&#39;country&#39;=>&#39;china&#39;);
$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>

The above is the detailed content of Simple usage example code of memcache. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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