Home > Article > Backend Development > Simple usage example code of memcache
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 = '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>
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!