We can know the specific working principle of memcached through the introduction of previous articles. So, today we will demonstrate to you how to implement There are two ways to use PHP as a memcached client and call the memcached service to perform object access operations.
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>// 包含 memcached 类文件 </SPAN><LI class=alt><SPAN>require_once('memcached-client.php'); </SPAN><LI class=""><SPAN>// 选项设置 </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>options</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>( </SPAN></SPAN><LI class=""><SPAN> 'servers' =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span> array('192.168.1.1:11211′), //memcached 服务的地址、端口,可用多个数组元素表示多个 memcached 服务 </span> </li> <li class="alt"> <span> 'debug' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> true, //是否打开 debug </span> </li> <li class=""> <span> 'compress_threshold' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 10240, //超过多少字节的数据时进行压缩 </span> </li> <li class="alt"> <span> 'persistant' =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> false //是否使用持久连接 </span> </li> <li class=""><span> ); </span></li> <li class="alt"><span>// 创建 memcached 对象实例 </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">mc</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> memcached($options); </span> </li> <li class="alt"><span>// 设置此脚本使用的唯一标识符 </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">key</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'mykey'</font></span><span>; </span> </li> <li class="alt"><span>// 往 memcached 中写入对象 </span></li> <li class=""> <span>$mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>add($key, 'some random strings'); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">val</font></span><span> = $mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>get($key); </span> </li> <li class=""> <span>echo "n".str_pad('$mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>add() ', 60, '_')."n"; </span> </li> <li class="alt"><span>var_dump($val); </span></li> <li class=""><span>// 替换已写入的对象数据值 </span></li> <li class="alt"> <span>$mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>replace($key, array('some'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'haha', 'array'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'xxx')); </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">val</font></span><span> = $mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>get($key); </span> </li> <li class="alt"> <span>echo "n".str_pad('$mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>replace() ', 60, '_')."n"; </span> </li> <li class=""><span>var_dump($val); </span></li> <li class="alt"><span>// 删除 memcached 中的对象 </span></li> <li class=""> <span>$mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>delete($key); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">val</font></span><span> = $mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>get($key); </span> </li> <li class=""> <span>echo "n".str_pad('$mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>delete() ', 60, '_')."n"; </span> </li> <li class="alt"><span>var_dump($val); </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
The first method of using PHP as a memcached client, PHP has an extension called memcache. When compiling under Linux, you need to bring the –enable-memcache[=DIR] option. Under Windows, remove the comment in front of php_memcache.dll in php.ini to make it available.
In addition, there is another way to avoid the trouble caused by expansion and recompilation, and that is to use php-memcached-client directly.
This article uses the second method. Although the efficiency is slightly worse than the extension library, it is not a big problem.
First download memcached-client.php. After downloading memcached-client.php, you can operate the memcached service through the class "memcached" in this file. In fact, the code call is very simple. The main methods used are add(), get(), replace() and delete(). The method description is as follows:
add ($key, $val, $exp = 0)
Write objects into memcached. $key is the unique identifier of the object, $val is the written object data, $exp is the expiration time in seconds, and the default is unlimited time;
get ($key)
Get object data from memcached through the object’s unique identifier $key;
replace ($key, $value, $exp=0)
Use $value replaces the content of the object with the identifier $key in memcached. The parameters are the same as the add() method. It will only work if the $key object exists;
delete ($key, $time = 0 )
Delete the object with the identifier $key in memcached. $time is an optional parameter, indicating how long to wait before deleting.
The following is a simple test code, which performs access operations on object data with the identifier 'mykey':
Is it very simple to implement PHP as a memcached client? In actual applications, the result set of the database query is usually saved in memcached, and is obtained directly from memcached the next time it is accessed, instead of performing database query operations. This can reduce the burden on the database to a great extent. Usually the value after the SQL statement md5() is used as the unique identifier key. Below is an example of using memcached to cache a database query result set (this code snippet follows the example code above):
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>sql</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>'SELECT * FROM users'</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>key</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>md5</FONT></SPAN><SPAN>($sql); //memcached 对象标识符 </SPAN></SPAN><LI class=""><SPAN>if ( !($</SPAN><SPAN class=attribute><FONT color=#ff0000>datas</FONT></SPAN><SPAN> = $mc-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span>get($key)) ) { </span> </li> <li class="alt"><span> // 在 memcached 中未获取到缓存数据,则使用数据库查询获取记录集。 </span></li> <li class=""><span> echo "n".str_pad('Read datas from MySQL.', 60, '_')."n"; </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">conn</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_connect</font></span><span>('localhost', 'test', 'test'); </span> </li> <li class=""><span> mysql_select_db('test'); </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">result</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_query</font></span><span>($sql); </span> </li> <li class=""> <span> while ($</span><span class="attribute"><font color="#ff0000">row</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">mysql_fetch_object</font></span><span>($result)) </span> </li> <li class="alt"><span> $datas[] = $row; </span></li> <li class=""><span> // 将数据库中获取到的结果集数据保存到 memcached 中,以供下次访问时使用。 </span></li> <li class="alt"> <span> $mc-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>add($key, $datas); </span> </li> <li class=""><span>} else { </span></li> <li class="alt"><span> echo "n".str_pad('Read datas from memcached.', 60, '_')."n"; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>var_dump($datas); </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
It can be seen that after using PHP as a memcached client, database connections and query operations can be reduced, the database load is reduced, and the script runs faster. Also improved.