This article provides a detailed analysis of the application of memcache in php. For more information, see
Required environment:
Php 5.3.3
Apache 2.2.7
Mysql 5.5.8
Download related documents:
Click to download
Decompress the memcached_1.2.5file and execute memcached.exe-d-install
Decompress the php5.3 _ vc6_memcachedll file and copy the php_memcache.dll file to the ext file directory of the php installation directory.
Then fill in this sentence in php. ini: extension = "php_memcache.dll"
Check whether the memcache extension is referenced in phpinfo.
Test code:
The code is as follows:
// Connection
$ Mem = new Memcache;
$ Mem-> connect ("127.0.0.1", 11211 );
// 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 ();
$ Memcachehost = '192. 168.10.1 ';
$ Memcacheport = 11211;
$ Memcachelife = 60;
$ Memcache = new Memcache;
$ Memcache-> connect ($ memcachehost, $ memcacheport) or die ("cocould not connect ");
$ Query = "select * from user limit 10 ";
$ Key = md5 ($ query );
If (! $ Memcache-> get ($ key ))
{
$ Conn = mysql_connect ("192.168.30.1", "root", "passwd ");
Mysql_select_db (users );
$ Result = mysql_query ($ query );
While ($ row = mysql_fetch_assoc ($ result ))
{
$ Arr [] = $ row;
}
$ F = 'DB ';
$ Memcache-> add ($ key, serialize ($ arr), 0, 30 );
$ Data = $ arr;
}
Else {
$ F = 'mem ';
$ Data_mem = $ memcache-> get ($ key );
$ Data = unserialize ($ data_mem );
}
Echo $ f;
Echo "";
// Print_r ($ data );
Foreach ($ data as $)
{
Echo $ a [user_id]. _. $ a [email];
Echo "";
}
?>
News system application:
The code is as follows:
// =================== Memcache
$ Memcachehost = '2017. 0.0.1 ';
$ Memcacheport = 11211;
$ Memcachelife = 60;
$ Memcache = new Memcache;
$ Memcache-> connect ($ memcachehost, $ memcacheport) or die ("cocould not connect ");
// ==================== News
$ SQL = "SELECT id, title, left (title, 16) as biaoti, date_time FROM 'p _ newsbase 'where shenhe = '1' order by id DESC limit 7 ";
$ Query = $ db-> query ($ SQL );
$ Key = md5 ($ query );
While ($ row_news = $ db-> fetch_array ($ query )){
$ Str = $ row_news ['biaoti']. $ db-> time_out ($ row_news ['date _ time']);
$ Sm_news [] = array ("name" => $ str, "title" => $ row_news ['title'], "id" => $ row_news ['id'], "date_time" => $ row_news ['date _ time']);
}
If (! $ Memcache-> get ($ key )){
$ Memcache-> add ($ key, serialize ($ sm_news), 0, $ memcachelife );
} Else {
$ Data_mem = $ memcache-> get ($ key );
$ Sm_news = unserialize ($ data_mem );
}
$ Smarty-> assign ("sm_news", $ sm_news );