在實際應用程式中我們會快取從資料庫查出來的結果集,以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的簡單使用實例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!