<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>haohtml&#039;s blog &#187; 缓存</title>
	<atom:link href="http://blog.haohtml.com/tag/%e7%bc%93%e5%ad%98/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.haohtml.com</link>
	<description>haohtml&#039;s life and works</description>
	<lastBuildDate>Sun, 05 Feb 2012 12:34:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>smarty局部缓存</title>
		<link>http://blog.haohtml.com/archives/2777</link>
		<comments>http://blog.haohtml.com/archives/2777#comments</comments>
		<pubDate>Thu, 31 Dec 2009 07:01:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://blog.papake.net/?p=2777</guid>
		<description><![CDATA[Smarty提供了强大的缓存功能。但有时我们并不希望整篇文档都被缓存，而是有选择的缓存某一部分内容或某一部分内容不被缓存。例如你在页面上端使用一个带有广告条位置的模板，广告条可以包含任何HTML、图象、FLASH等混合信息. 因此这里不能使用一个静态的链接，同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定，同时需要一个函数取广告条的内容信息。smarty也提供了这种缓存控制能力。

我们可以使用$smarty->register_block($params,&#038;$smarty)使整篇页面中的某一块不被缓存。<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="smarty缓存使用技巧" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3342&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">smarty缓存使用技巧</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Smarty高级特性 – 用户自定义函数使用 SMARTY:: register_function;" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3586&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Smarty高级特性 – 用户自定义函数使用 SMARTY:: register_function;</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Smarty中的Insert方法的使用教程" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3042&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Smarty中的Insert方法的使用教程</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Smarty缩图函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3589&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Smarty缩图函数</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<div>
<div><span>Smarty提供了强大的 缓存功能。但有时我们并不希望整篇文档都被缓存，而是有选择的缓存某一部分内容或某一部分内容不被缓存。例如你在页面上端使用一个带有广告条位置的模板， 广告条可以包含任何HTML、图象、FLASH等混合信息. 因此这里不能使用一个静态的链接，同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定，同时需要一个函数取广告条的内容信息。smarty也提供了这种缓存控制能力。</span></p>
<p>我们可以使用$smarty-&gt;register_block($params,&amp;$smarty)使整篇页面中的某一块不被缓存。<br />
<span><br />
<strong>index.tpl:</strong></span></p>
<div>
<div>XML/HTML代码</div>
<ol>
<li><span><span>&lt;</span><span>div</span><span> </span><span>align</span><span>=</span><span>'center'</span><span>&gt;</span><span> </span></span></li>
<li><span>Page created: {"0"|date_format:"%D %H:%M:%S"} </span></li>
<li><span><span>&lt;</span><span>{dynamic}</span><span>&gt;</span><span> </span></span></li>
<li><span>Now is: {"0"|date_format:"%D %H:%M:%S"} </span></li>
<li><span>... do other stuff ... </span></li>
<li><span><span>&lt;</span><span>{/dynamic}</span><span>&gt;</span><span> </span></span></li>
<li><span><span>&lt;/</span><span>div</span><span>&gt;</span><span> </span></span></li>
</ol>
</div>
<p><strong>index.php:</strong></p>
<div>
<div>XML/HTML代码</div>
<ol>
<li><span><span>function smarty_block_dynamic($param, $content, &amp;$smarty) { </span></span></li>
<li><span>return $content; </span></li>
<li><span>} </span></li>
<li><span> </span></li>
<li><span>$<span>smarty</span><span> = </span><span>new</span><span> Smarty; </span></span></li>
<li><span>$smarty-<span>&gt;</span><span>caching</span><span> = </span><span>true</span><span>; </span></span></li>
<li><span>$smarty-<span>&gt;</span><span>register_block('dynamic', 'smarty_block_dynamic', false); </span></span></li>
<li><span> </span></li>
<li><span>if(!$smarty-<span>&gt;</span><span>is_cached()){ </span></span></li>
<li><span> ....... </span></li>
<li><span>} </span></li>
<li><span> </span></li>
<li><span>$smarty-<span>&gt;</span><span>display('index.tpl'); </span></span></li>
</ol>
</div>
<p>注解：<br />
定义一个函数，函数名格式为：smarty_type_name($params, &amp;$smarty)<br />
type为block<br />
name为用户自定义标签名称,在这里是{dynamic}<br />
两个参数是必须的，即使在函数中没有使用也要写上。两个参数的功能同上。</p>
<p><span>单独这样使用，可能你发现并没有实现您所需要的功能，这是因为还需要在smarty_compiler.class.php中修改几行代码：</span></p>
<p><span>在上面的这个文件里面搜索$smarty-&gt;register_function('current_time','smarty_function_current_time',true);这个字符串，大约在702行。</span></p>
<p><span>把这行代码修改为：</span></p>
<p><span>if($tag_command == 'dynamic') $this-&gt;_plugins['block'][$tag_command] = array($plugin_func, null, null, null, false);<br />
else $this-&gt;_plugins['block'][$tag_command] = array($plugin_func, null, null, null, true);</span></p>
<p>之后再打开您刚才写的页面就会发现部分缓存的功能已经实现了。</p></div>
</div>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="smarty缓存使用技巧" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3342&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">smarty缓存使用技巧</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Smarty高级特性 – 用户自定义函数使用 SMARTY:: register_function;" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3586&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Smarty高级特性 – 用户自定义函数使用 SMARTY:: register_function;</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Smarty中的Insert方法的使用教程" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3042&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Smarty中的Insert方法的使用教程</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Smarty缩图函数" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3589&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Smarty缩图函数</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/2777/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[教程]memcached for win32的安装</title>
		<link>http://blog.haohtml.com/archives/408</link>
		<comments>http://blog.haohtml.com/archives/408#comments</comments>
		<pubDate>Wed, 24 Sep 2008 15:56:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[服务器类]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[php_memcache.dll]]></category>

		<guid isPermaLink="false">http://haohtml.com/wp/?p=408</guid>
		<description><![CDATA[memcached是由livejournal团队(danga.com)制作的开源缓存软件，是缓存机制的一种实现，用它之所以高效，是因为它是利用了内存，使用好了能够大大加快页面或者是其它程序的执行速度。要注意的是一旦服务器停止，内存中的缓存数据会被清空。

win32下，需要启动memcached服务，首先下载相关的memcached文件（用于启动服务的windows.rar在附件中），解压后可以自己选择，这里我选择的是2.1版本的，将其中的memcached.exe和memcached.ini（里面也就这俩文件）拷贝到某路径下（如：E:\java\memcached2.1），然后通过cmd命令窗口，先转入到该路径，然后按如下步骤输入：<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="利用memcached构建高性能的Web应用程序" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026094.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用memcached构建高性能的Web应用程序</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026102.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached完全剖析</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached配置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached配置</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcache简要安装步骤" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F370&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcache简要安装步骤</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<div class="blog_content">
<p>memcached是由livejournal团队(danga.com)制作的开源缓存软件，是缓存机制的一种实现，用它之所以高效，是因为它是利用了内存，使用好了能够大大加快页面或者是其它程序的执行速度。要注意的是一旦服务器停止，内存中的缓存数据会被清空。</p>
<p>win32下，需要启动memcached服务，首先下载相关的memcached文件（用于启动服务的<span style="font-family: Arial;">windows.rar在附件中</span>），解压后可以自己选择，这里我选择的是2.1版本的，将其中的<span style="font-family: Arial;">memcached.exe和<span style="font-family: Arial;">memcached.ini（里面也就这俩文件）拷贝到某路径下（如：<span style="font-family: Arial;">E:\java\memcached2.1</span>），然后通过cmd命令窗口，先转入到该路径，然后按如下步骤输入：</span></span></p>
<p><span style="font-family: Arial;"><span style="font-family: Arial;">1、<span style="font-family: Arial;">memcached.exe -d install</span></span></span></p>
<p>2、<span style="font-family: Arial;">memcached.exe -d start</span></p>
<p>这里第一步是用于安装服务，第二步是用于启动服务，有些默认参数的值是通过<span style="font-family: Arial;">memcached.ini里的相关元素的设置值而定的。</span></p>
<p>如果要停止服务和卸载服务可以用入下命令：</p>
<p>3、<span style="font-family: Arial;">memcached.exe -d stop 或 <span style="font-family: Arial;">memcached.exe -d shutdown</span></span></p>
<p>4、<span style="font-family: Arial;">memcached.exe -d uninstall</span></p>
<p><strong>命令参数解释：</strong></p>
<p>-d 以守护程序（daemon）方式运行 memcached</p>
<p>-m 设置 memcached 可以使用的内存大小，单位为 M</p>
<p>-l 设置监听的 IP 地址，如果是本机的话，通常可以不设置此参数</p>
<p>-p 设置监听的端口，默认为 11211，所以也可以不设置此参数</p>
<p>-u 指定用户，如果当前为 root 的话，需要使用此参数指定用户<br />
-p 监听的端口<br />
-l 连接的IP地址, 默认是本机<br />
-d start 启动memcached服务<br />
-d restart 重起memcached服务<br />
-d stop|shutdown 关闭正在运行的memcached服务<br />
-d install 安装memcached服务<br />
-d uninstall 卸载memcached服务<br />
-u 以的身份运行 (仅在以root运行的时候有效)<br />
-m 最大内存使用，单位MB。默认64MB<br />
-M 内存耗尽时返回错误，而不是删除项<br />
-c 最大同时连接数，默认是1024<br />
-f 块大小增长因子，默认是1.25(增长因子:<a href="http://zhengjunwei2007-163-com.iteye.com/blog/963375">http://zhengjunwei2007-163-com.iteye.com/blog/963375</a>)<br />
-n 最小分配空间，key+value+flags默认是48<br />
-h 显示帮助</p>
<p><span style="font-family: Arial;">memcached.ini文件指定的各参数及值可以根据需要做更改，默认的如下：</span></p>
<p><span style="font-family: Arial;">bind_addr=127.0.0.1<br />
listener_port=11212<br />
memory=16<br />
max_conns=1024</span></p>
<p><span style="font-family: Arial;">evict_to_free = 0</span></p>
<p>说明：</p>
<p><span style="font-family: Arial;">bind_addr是指绑定的IP</span></p>
<p>listener_port是指监听的端口</p>
<p>memory内存大小</p>
<p>max_conns最大连接数</p>
<p>evict_to_free</p>
<p>在通过命令 <span style="font-family: Arial;">memcached.exe -d start 将服务启动后，可以通过一个demo来做测试，这个demo的下载地址如下：</span></p>
<p><span style="font-family: Arial;"><a href="http://www.whalin.com/memcached/#download">http://www.whalin.com/memcached/#download</a></span></p>
<p><span style="font-family: Arial;">运行类<span style="font-family: Arial;">com.danga.MemCached.test.<span style="font-family: Arial;">TestMemcached</span></span>前将服务IP和监听端口改为启动服务时所用的IP和监听端口，</span></p>
<p><span style="font-family: Arial;">如：<span style="font-family: Arial;">String[] servers = { "127.0.0.1:11212"};</span></span></p>
<p>或者可以用如下的Test类做测试</p>
<div class="code_title">java 代码</div>
<p>&nbsp;</p>
<div class="dp-highlighter">
<ol class="dp-j">
<li class="alt"><span><span class="keyword">package</span><span> com.danga.MemCached.test;       </span></span></li>
<li><span>      </span></li>
<li class="alt"><span class="keyword">import</span><span> java.util.Date;       </span></li>
<li><span>      </span></li>
<li class="alt"><span class="keyword">import</span><span> com.danga.MemCached.MemCachedClient;       </span></li>
<li><span class="keyword">import</span><span> com.danga.MemCached.SockIOPool;       </span></li>
<li class="alt"><span>      </span></li>
<li><span>      </span></li>
<li class="alt"><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Test {           </span></li>
<li><span>    </span><span class="keyword">protected</span><span> </span><span class="keyword">static</span><span> MemCachedClient mcc = </span><span class="keyword">new</span><span> MemCachedClient();          </span></li>
<li class="alt"><span>          </span></li>
<li><span>    </span><span class="keyword">static</span><span> {          </span></li>
<li class="alt"><span>        String[] servers ={</span><span class="string">"127.0.0.1:11212"</span><span>};          </span></li>
<li><span>          </span></li>
<li class="alt"><span>        Integer[] weights = { </span><span class="number">3</span><span> };          </span></li>
<li><span>          </span></li>
<li class="alt"><span>        </span><span class="comment">//创建一个实例对象SockIOPool      </span><span>  </span></li>
<li><span>        SockIOPool pool = SockIOPool.getInstance();          </span></li>
<li class="alt"><span>          </span></li>
<li><span>        </span><span class="comment">// set the servers and the weights     </span><span>  </span></li>
<li class="alt"><span>        </span><span class="comment">//设置Memcached Server     </span><span>  </span></li>
<li><span>        pool.setServers( servers );          </span></li>
<li class="alt"><span>        pool.setWeights( weights );          </span></li>
<li><span>          </span></li>
<li class="alt"><span>        </span><span class="comment">// set some basic pool settings        </span><span>  </span></li>
<li><span>        </span><span class="comment">// 5 initial, 5 min, and 250 max conns        </span><span>  </span></li>
<li class="alt"><span>        </span><span class="comment">// and set the max idle time for a conn        </span><span>  </span></li>
<li><span>        </span><span class="comment">// to 6 hours        </span><span>  </span></li>
<li class="alt"><span>        pool.setInitConn( </span><span class="number">5</span><span> );          </span></li>
<li><span>        pool.setMinConn( </span><span class="number">5</span><span> );          </span></li>
<li class="alt"><span>        pool.setMaxConn( </span><span class="number">250</span><span> );          </span></li>
<li><span>        pool.setMaxIdle( </span><span class="number">1000</span><span> * </span><span class="number">60</span><span> * </span><span class="number">60</span><span> * </span><span class="number">6</span><span> );          </span></li>
<li class="alt"><span>          </span></li>
<li><span>        </span><span class="comment">// set the sleep for the maint thread        </span><span>  </span></li>
<li class="alt"><span>        </span><span class="comment">// it will wake up every x seconds and        </span><span>  </span></li>
<li><span>        </span><span class="comment">// maintain the pool size        </span><span>  </span></li>
<li class="alt"><span>        pool.setMaintSleep( </span><span class="number">30</span><span> );          </span></li>
<li><span>          </span></li>
<li class="alt"><span class="comment">//        Tcp的规则就是在发送一个包之前，本地机器会等待远程主机     </span><span>  </span></li>
<li><span class="comment">//        对上一次发送的包的确认信息到来；这个方法就可以关闭套接字的缓存，     </span><span>  </span></li>
<li class="alt"><span class="comment">//        以至这个包准备好了就发；     </span><span>  </span></li>
<li><span>        pool.setNagle( </span><span class="keyword">false</span><span> );          </span></li>
<li class="alt"><span>        </span><span class="comment">//连接建立后对超时的控制     </span><span>  </span></li>
<li><span>        pool.setSocketTO( </span><span class="number">3000</span><span> );       </span></li>
<li class="alt"><span>        </span><span class="comment">//连接建立时对超时的控制     </span><span>  </span></li>
<li><span>        pool.setSocketConnectTO( </span><span class="number">0</span><span> );          </span></li>
<li class="alt"><span>          </span></li>
<li><span>        </span><span class="comment">// initialize the connection pool        </span><span>  </span></li>
<li class="alt"><span>        </span><span class="comment">//初始化一些值并与MemcachedServer段建立连接     </span><span>  </span></li>
<li><span>        pool.initialize();       </span></li>
<li class="alt"><span>                  </span></li>
<li><span>          </span></li>
<li class="alt"><span>        </span><span class="comment">// lets set some compression on for the client        </span><span>  </span></li>
<li><span>        </span><span class="comment">// compress anything larger than 64k        </span><span>  </span></li>
<li class="alt"><span>        mcc.setCompressEnable( </span><span class="keyword">true</span><span> );          </span></li>
<li><span>        mcc.setCompressThreshold( </span><span class="number">64</span><span> * </span><span class="number">1024</span><span> );          </span></li>
<li class="alt"><span>    }          </span></li>
<li><span>              </span></li>
<li class="alt"><span>    </span><span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> bulidCache(){          </span></li>
<li><span>        </span><span class="comment">//set(key,value,Date) ,Date是一个过期时间，如果想让这个过期时间生效的话，这里传递的new Date(long date) 中参数date，需要是个大于或等于1000的值。     </span><span>  </span></li>
<li class="alt"><span>        </span><span class="comment">//因为java client的实现源码里是这样实现的 expiry.getTime() / 1000 ，也就是说，如果 小于1000的值，除以1000以后都是0，即永不过期     </span><span>  </span></li>
<li><span>        mcc.set( </span><span class="string">"test"</span><span>, </span><span class="string">"This is a test String"</span><span> ,</span><span class="keyword">new</span><span> Date(</span><span class="number">10000</span><span>));   </span><span class="comment">//十秒后过期     </span><span>  </span></li>
<li class="alt"><span>                 </span></li>
<li><span>    }          </span></li>
<li class="alt"><span>         </span></li>
<li><span>    </span><span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> output() {          </span></li>
<li class="alt"><span>        </span><span class="comment">//从cache里取值     </span><span>  </span></li>
<li><span>        String value = (String) mcc.get( </span><span class="string">"test"</span><span> );          </span></li>
<li class="alt"><span>        System.out.println(value);           </span></li>
<li><span>    }          </span></li>
<li class="alt"><span>              </span></li>
<li><span>    </span><span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> main(String[] args){          </span></li>
<li class="alt"><span>        bulidCache();         </span></li>
<li><span>        output();              </span></li>
<li class="alt"><span>    }        </span></li>
<li><span>          </span></li>
<li class="alt"><span>}  </span></li>
</ol>
</div>
<p><strong> 应用memcached的一个心得：</strong></p>
<p>1、客户端在与 memcached 服务建立连接之后，进行存取对象的操作,每个被存取的对象都有一个唯一的标识符 key，存取操作均通过这个 key 进行，保存到 memcached 中的对象实际上是放置内存中的，并不是保存在 cache 文件中的，这也是为什么 memcached 能够如此高效快速的原因。注意，这些对象并不是持久的，服务停止之后，里边的数据就会丢失。</p>
<p>2、当存入cached的数据超过了cached的容量后会将最长时间没调用的对象挤出，这正好应征了cached的特征。</p>
<p>3、利用memcached常用的做法：在每取得一次cached对象后，重新设置这个对象的cache时间，这样能够使得经常被调用的对象可以长期滞留在缓存中，使得效率增倍。</p>
<p><a href="/wp-content/uploads/2011/10/memcached_for_windows.rar" target="_blank">windows.rar (</a>2.5 MB)</p>
<p><strong>相关教程:</strong></p>
<p>Linux下Memcached的安装及配置:<a href="http://blog.haohtml.com/archives/364" target="_blank">http://blog.haohtml.com/archives/364</a><br />
基于 PHP5 &amp; JQuery 的 Memcached 管理监控工具:<a href="http://www.junopen.com/memadmin/">http://www.junopen.com/memadmin/</a></p>
</div>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="利用memcached构建高性能的Web应用程序" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026094.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用memcached构建高性能的Web应用程序</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026102.png" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached完全剖析</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached配置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached配置</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcache简要安装步骤" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F370&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcache简要安装步骤</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/408/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[缓存加速]eAccelerator与memcached的区别与用途</title>
		<link>http://blog.haohtml.com/archives/399</link>
		<comments>http://blog.haohtml.com/archives/399#comments</comments>
		<pubDate>Wed, 24 Sep 2008 12:38:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[服务器类]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[eAccelerator]]></category>
		<category><![CDATA[memcached]]></category>

		<guid isPermaLink="false">http://haohtml.com/wp/?p=399</guid>
		<description><![CDATA[eAccelerator和memcached，是目前较为主流的两个可使用在PHP之中的缓存加速工具． eAccelerator专门为PHP开发，而memcached不仅仅用在PHP之中，其他所有的语言都可以使用． eAccelerator的主要功能： 1. 缓存PHP文件的执行代码：在被缓存的代码再次被调用时，将直接从内存读取，从而在很大程度了PHP运行的速度． 2. 提供了共享内存操作函数：用户可以将自己的常见非资源对像，保存到内存之中，并可以随时读取出来． memcached的主要功能： 提供共享内存操作函数，可以保存和读取数据 两者的共同点： 共同点：都提供了共享内存操作函数，可以用来保存和读取自己的数据 两者的区别： eAccelerator作为PHP的扩展库存在，那么仅在PHP运行时，可以操作和读写共享内存，一般情况，只能由操作共享内存的程序自己调用．同时，eAccelerator可以缓存PHP程序的执行代码，提升程序的调入和执行速度． memcached主要作为一个共享内存服务器，其PHP扩展库仅仅作为PHP到memcached的连接库存在，类似MySQL扩展库．因而，memcached可以完全脱离PHP，其共享的数据，可以被不同的程序调用． 根据两者的不同，我们将他们使用在真真需要的地方： eAccelerator主要用于单机PHP提速，缓存中间数据．对于实时性高，但数据操作量小的情况下，非常实用． memcached用于分布式或者集群系统，多台服务器可以共享数据．对于实时性高，同时数据操作量大的情况下，非常实用．<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="利用memcached构建高性能的Web应用程序" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026094.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用memcached构建高性能的Web应用程序</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached配置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached配置</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached for win32" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached for win32</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached与memcache的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F392&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached与memcache的区别</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>eAccelerator和<strong style="background-color: #ffff66; color: black;">memcached</strong>，是目前较为主流的两个可使用在PHP之中的缓存加速工具．</p>
<p>eAccelerator专门为PHP开发，而<strong style="background-color: #ffff66; color: black;">memcached</strong>不仅仅用在PHP之中，其他所有的语言都可以使用．</p>
<p>eAccelerator的主要功能：<br />
1. 缓存PHP文件的执行代码：在被缓存的代码再次被调用时，将直接从内存读取，从而在很大程度了PHP运行的速度．<br />
2. 提供了共享内存操作函数：用户可以将自己的常见非资源对像，保存到内存之中，并可以随时读取出来．</p>
<p><strong>memcached的主要功能：</strong><br />
提供共享内存操作函数，可以保存和读取数据</p>
<p><strong>两者的共同点：</strong><br />
共同点：都提供了共享内存操作函数，可以用来保存和读取自己的数据</p>
<p><strong> 两者的区别</strong>：<br />
eAccelerator作为PHP的扩展库存在，那么仅在PHP运行时，可以操作和读写共享内存，一般情况，只能由操作共享内存的程序自己调用．同时，eAccelerator可以缓存PHP程序的执行代码，提升程序的调入和执行速度．<br />
<strong style="background-color: #ffff66; color: black;">memcached</strong>主要作为一个共享内存服务器，其PHP扩展库仅仅作为PHP到<strong style="background-color: #ffff66; color: black;">memcached</strong>的连接库存在，类似MySQL扩展库．因而，<strong style="background-color: #ffff66; color: black;">memcached</strong>可以完全脱离PHP，其共享的数据，可以被不同的程序调用．</p>
<p><span style="color: #0000ff;"> 根据两者的不同，我们将他们使用在真真需要的地方：</span><br />
eAccelerator主要用于单机PHP提速，缓存中间数据．对于实时性高，但数据操作量小的情况下，非常实用．<br />
<strong style="background-color: #ffff66; color: black;">memcached</strong>用于分布式或者集群系统，多台服务器可以共享数据．对于实时性高，同时数据操作量大的情况下，非常实用．</p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="利用memcached构建高性能的Web应用程序" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026094.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用memcached构建高性能的Web应用程序</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached配置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached配置</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached for win32" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached for win32</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached与memcache的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F392&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached与memcache的区别</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/399/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>memcached安装+php使用手记</title>
		<link>http://blog.haohtml.com/archives/395</link>
		<comments>http://blog.haohtml.com/archives/395#comments</comments>
		<pubDate>Wed, 24 Sep 2008 12:33:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[服务器类]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://haohtml.com/wp/?p=395</guid>
		<description><![CDATA[本文简要介绍一下安装的情况,以及PHP模块memcache使用情况：

提要：
１。安装memcached服务器端
２。安装php对memcache支持模块
３。使用memcache情况，计数器、数据压缩
４。Memcache内存的更新清理(delete flush)
５。内存超量的测试(set)

１。安装memcached服务器端
memcached安装说明(北南南北的站)：
http://www.linuxsir.org/main/?q=node/184 
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="在安装php的make时候,出现错误&quot;chmod: cannot access `ext/phar/phar.phar': No such file or directory&quot;的解决办法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12482&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">在安装php的make时候,出现错误&quot;chmod: cannot access `ext/phar/phar.phar': No such file or directory&quot;的解决办法</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached for win32" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached for win32</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="利用memcached构建高性能的Web应用程序" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026094.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用memcached构建高性能的Web应用程序</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="freebsd+php+memcache、memcached安装和使用" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">freebsd+php+memcache、memcached安装和使用</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="color: #336699; font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><a href="http://blog.csdn.net/felio/archive/2006/09/29/1303647.aspx">http://blog.csdn.net/felio/archive/2006/09/29/1303647.aspx</a></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">本文简要介绍一下安装的情况</span><span lang="EN-US"><span style="font-family: Times New Roman;">,</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">以及</span><span lang="EN-US"><span style="font-family: Times New Roman;">PHP</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">模块</span><span lang="EN-US"><span style="font-family: Times New Roman;">memcache</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">使用情况：</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">提要：</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">１。安装</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">memcached</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">服务器端</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">２。安装</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">php</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">对</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">memcache</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">支持模块</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">３。使用</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">memcache</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">情况，计数器、数据压缩</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">４。</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">Memcache</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">内存的更新清理</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">(delete flush)<br />
</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">５。内存超量的测试</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">(set)</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;" align="left"><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">１。安装</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">memcached</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">服务器端</span></strong><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
memcached</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">安装说明</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">北南南北的站</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">)</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">：</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
<a href="http://www.linuxsir.org/main/?q=node/184"><span style="mso-bidi-font-size: 12.0pt;"><span style="color: #336699;">http://www.linuxsir.org/main/?q=node/184</span></span></a> </span></p>
<blockquote style="margin-right: 0px;" dir="ltr">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;" align="left">
<div style="padding-bottom: 4px; padding-left: 5.4pt; width: 95%; padding-right: 5.4pt; background: #e6e6e6; word-break: break-all; padding-top: 4px; border: windowtext 0.5pt solid;">
<div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><span style="color: #000000;">Linux下缓存服务器的应用<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />作者：tonyvicky<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />来自：LinuxSir.Org<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />摘要：由于数据库存储的数据量越来越大，查询速度也就变的越来越慢，因此就有了缓存服务器应用的必要，本文是介绍Memcached的安装以及简单的使用。<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />本文只介绍memcached的PHP的API，想查看其他关于Memcached的API文档案，请访问 http:</span><span style="color: #000000;">//</span><span style="color: #000000;">www.danga.com</span><span style="color: #000000;">/</span><span style="color: #000000;">memcached</span><span style="color: #000000;">/</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />目录<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />一、环境需求<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />二、下载相关软件<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />三、安装和配置<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">1</span><span style="color: #000000;">、安装Memcached<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">2</span><span style="color: #000000;">、安装memcache PHP模块<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">3</span><span style="color: #000000;">、测试脚本<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />四、关于本文<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">++++++++++++++++++++++++++++++++++++++++</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />正文<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">++++++++++++++++++++++++++++++++++++++++</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />一、环境需求<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />安装Memcached需要libevent库的支持，所以请在安装Memcached之前检查有没有安装libevent。测试环境还需要PHP的支持，本文假设PHP已经安装到</span><span style="color: #000000;">/</span><span style="color: #000000;">usr</span><span style="color: #000000;">/</span><span style="color: #000000;">local</span><span style="color: #000000;">/</span><span style="color: #000000;">php目录下，也就是在编译PHP的时候使用perfix参数指定目录(</span><span style="color: #000000;">--</span><span style="color: #000000;">prefix</span><span style="color: #000000;">=/</span><span style="color: #000000;">usr</span><span style="color: #000000;">/</span><span style="color: #000000;">local</span><span style="color: #000000;">/</span><span style="color: #000000;">php)<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />二、下载相关软件<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcached下载地址：http:</span><span style="color: #000000;">//</span><span style="color: #000000;">www.danga.com</span><span style="color: #000000;">/</span><span style="color: #000000;">memcached</span><span style="color: #000000;">/</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />memcache PHP模块下载地址： http:</span><span style="color: #000000;">//</span><span style="color: #000000;">pecl.php.net</span><span style="color: #000000;">/</span><span style="color: #000000;">package</span><span style="color: #000000;">/</span><span style="color: #000000;">memcache 推荐使用1.5版<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />libevent 下载地址： http:</span><span style="color: #000000;">//</span><span style="color: #000000;">www.monkey.org</span><span style="color: #000000;">/~</span><span style="color: #000000;">provos</span><span style="color: #000000;">/</span><span style="color: #000000;">libevent</span><span style="color: #000000;">/</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />本文不再讲述如何安装libevent<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />三、安装和配置<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">1</span><span style="color: #000000;">、安装Memcached<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> tar vxzf memcached-1.1.12.tar.gz</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> cd memcached-1.1.12</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> ./configure --prefix=/usr/local/memcached</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> make</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> make install</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />安装完之后要启动服务<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> cd /usr/local/memcached/bin</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> ./memcached -d -m 50 -p 11211 -u root</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />参数说明 </span><span style="color: #000000;">-</span><span style="color: #000000;">m 指定使用多少兆的缓存空间；</span><span style="color: #000000;">-</span><span style="color: #000000;">p 指定要监听的端口； </span><span style="color: #000000;">-</span><span style="color: #000000;">u 指定以哪个用户来运行<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">2</span><span style="color: #000000;">、安装memcache PHP模块<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> tar vxzf memcache-1.5.tgz</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> cd memcache-1.5</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> /usr/local/php/bin/phpize</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> make</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">root@tonyvicky:</span><span style="color: #008000;">#</span><span style="color: #008000;"> make install</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />安装完后会有类似这样的提示：<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Installing shared extensions: </span><span style="color: #000000;">/</span><span style="color: #000000;">usr</span><span style="color: #000000;">/</span><span style="color: #000000;">local</span><span style="color: #000000;">/</span><span style="color: #000000;">php</span><span style="color: #000000;">/</span><span style="color: #000000;">lib</span><span style="color: #000000;">/</span><span style="color: #000000;">php</span><span style="color: #000000;">/</span><span style="color: #000000;">extensions</span><span style="color: #000000;">/</span><span style="color: #000000;">no</span><span style="color: #000000;">-</span><span style="color: #000000;">debug</span><span style="color: #000000;">-</span><span style="color: #000000;">non</span><span style="color: #000000;">-</span><span style="color: #000000;">zts</span><span style="color: #000000;">-</span><span style="color: #000000;">20050922</span><span style="color: #000000;">/</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />把这个记住，然后修改php.ini，把<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />extension_dir </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">./</span><span style="color: #800000;">"</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />修改为<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />extension_dir </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/</span><span style="color: #800000;">"</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />并添加一行<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />extension</span><span style="color: #000000;">=</span><span style="color: #000000;">memcache.so<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">3</span><span style="color: #000000;">、测试脚本<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />自己写一个PHP程序测试一下吧<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">&lt;</span><span style="color: #000000;">?php<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />$memcache </span><span style="color: #000000;">=</span><span style="color: #000000;"> new Memcache; </span><span style="color: #000000;">//</span><span style="color: #000000;">创建一个memcache对象<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />$memcache</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">connect(</span><span style="color: #800000;">'</span><span style="color: #800000;">localhost</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #000000;">11211</span><span style="color: #000000;">) </span><span style="color: #0000ff;">or</span><span style="color: #000000;"> die (</span><span style="color: #800000;">"</span><span style="color: #800000;">Could not connect</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #000000;">//</span><span style="color: #000000;">连接Memcached服务器<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />$memcache</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">set(</span><span style="color: #800000;">'</span><span style="color: #800000;">key</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">test</span><span style="color: #800000;">'</span><span style="color: #000000;">); </span><span style="color: #000000;">//</span><span style="color: #000000;">设置一个变量到内存中，名称是key 值是test<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />$get_value </span><span style="color: #000000;">=</span><span style="color: #000000;"> $memcache</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">get(</span><span style="color: #800000;">'</span><span style="color: #800000;">key</span><span style="color: #800000;">'</span><span style="color: #000000;">); </span><span style="color: #000000;">//</span><span style="color: #000000;">从内存中取出key的值<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />echo $get_value;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />?</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span></div>
</div>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;" align="left">
</blockquote>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;" align="left">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;" align="left"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">注意的是：如果你安装过程中出现错误，请看看是不是有模块没装：</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
autoconf<br />
zlib (</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">压缩数据用</span></p>
<div class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">)</span></div>
<p><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">２。安装</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">php</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">对</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">memcache</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">支持模块</span></strong><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
PHP</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">老家：</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
<a href="http://cn.php.net/manual/zh/ref.memcache.php"><span style="mso-bidi-font-size: 12.0pt;"><span style="color: #336699;">http://cn.php.net/manual/zh/ref.memcache.php</span></span></a></span></span></p>
<p>&nbsp;</p>
<blockquote style="margin-right: 0px;" dir="ltr">
<div>
<div style="padding-bottom: 4px; padding-left: 5.4pt; width: 95%; padding-right: 5.4pt; background: #e6e6e6; word-break: break-all; padding-top: 4px; border: windowtext 0.5pt solid;">
<div><span style="color: #336699;"><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">Memcache Functions<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />简介<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache module provides handy procedural and </span><span style="color: #0000ff;">object</span><span style="color: #000000;"> oriented </span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> to memcached</span><span style="color: #000000;">,</span><span style="color: #000000;"> highly effective caching daemon</span><span style="color: #000000;">,</span><span style="color: #000000;"> which was especially designed to decrease database load in dynamic web applications</span><span style="color: #000000;">.</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />This module doesn</span><span style="color: #000000;">'</span><span style="color: #000000;">t have native support of multiple servers, but you still can implement it yourself in your application. Establish several memcached connections, set priority level for each server etc.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />More information about memcached can be found at http://www.danga.com/memcached/.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />需求<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />This module uses functions of zlib to support on-the-fly data compression. Zlib is required to install this module.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />PHP 4.3.3 or newer is required to use the memcache extension.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />安装<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />本 PECL 扩展未绑定于 PHP 中。 进一步信息例如新版本，下载，源程序，维护者信息以及更新日志可以在此找到： http://pecl.php.net/package/memcache.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />In order to use these functions you must compile PHP with Memcache support by using the --enable-memcache[=DIR] option.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Windows users will enable php_memcache.dll inside of php.ini in order to use these functions. 可以从 PHP 下载页面或者 http://snaps.php.net/ 下载此 PECL 扩展的 DLL 文件。<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />预定义常量<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />表格 1. MemCache Constants<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Name Description<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />MEMCACHE_COMPRESSED (integer)  Used to turn on-the-fly data compression on with Memcache::set(), Memcache::add() 和 Memcache::replace().<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />运行时配置<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />本扩展模块在 php.ini 中未定义任何配置选项。<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />资源类型<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />There is only one resource type used in memcache module - it</span><span style="color: #000000;">'</span><span style="color: #000000;">s the link identifier </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> a cache server connection</span><span style="color: #000000;">.</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />范例<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />例子 </span><span style="color: #000000;">1</span><span style="color: #000000;">.</span><span style="color: #000000;"> memcache extension overview example<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">&lt;?</span><span style="color: #000000;">php<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #800080;">$memcache</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Memcache;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">connect(</span><span style="color: #000000;">'</span><span style="color: #000000;">localhost</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">11211</span><span style="color: #000000;">) or </span><span style="color: #0000ff;">die</span><span style="color: #000000;"> (</span><span style="color: #000000;">"</span><span style="color: #000000;">Could not connect</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #800080;">$version</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">getVersion();<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">Server's version: </span><span style="color: #000000;">"</span><span style="color: #000000;">.</span><span style="color: #800080;">$version</span><span style="color: #000000;">.</span><span style="color: #000000;">"</span><span style="color: #000000;">&lt;br/&gt; </span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #800080;">$tmp_object</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> stdClass;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #800080;">$tmp_object</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">str_attr </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">'</span><span style="color: #000000;">test</span><span style="color: #000000;">'</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #800080;">$tmp_object</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">int_attr </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">123</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">set(</span><span style="color: #000000;">'</span><span style="color: #000000;">key</span><span style="color: #000000;">'</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$tmp_object</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #000000;">10</span><span style="color: #000000;">) or </span><span style="color: #0000ff;">die</span><span style="color: #000000;"> (</span><span style="color: #000000;">"</span><span style="color: #000000;">Failed to save data at the server</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">Store data in the cache (data will expire in 10 seconds)&lt;br/&gt; </span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #800080;">$get_result</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">get(</span><span style="color: #000000;">'</span><span style="color: #000000;">key</span><span style="color: #000000;">'</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #000000;">"</span><span style="color: #000000;">Data from the cache:&lt;br/&gt; </span><span style="color: #000000;">"</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #008080;">var_dump</span><span style="color: #000000;">(</span><span style="color: #800080;">$get_result</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">?&gt;</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />In the above example</span><span style="color: #000000;">,</span><span style="color: #000000;"> an </span><span style="color: #0000ff;">object</span><span style="color: #000000;"> is being saved in the cache and then retrieved back</span><span style="color: #000000;">.</span><span style="color: #000000;"> </span><span style="color: #0000ff;">Object</span><span style="color: #000000;"> and other non</span><span style="color: #000000;">-</span><span style="color: #000000;">scalar types are serialized before saving</span><span style="color: #000000;">,</span><span style="color: #000000;"> so it</span><span style="color: #000000;">'</span><span style="color: #000000;">s impossible to store resources (i.e. connection identifiers and others) in the cache.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />目录<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache::add -- Add an item to the server<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache::close -- Close memcached server connection<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache::connect -- Open memcached server connection<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />memcache_debug -- Turn debug output on/off<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache::decrement -- Decrement item</span><span style="color: #000000;">'</span><span style="color: #000000;">s value<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache</span><span style="color: #000000;">::</span><span style="color: #000000;">delete </span><span style="color: #000000;">--</span><span style="color: #000000;"> Delete item from the server<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache</span><span style="color: #000000;">::</span><span style="color: #008080;">flush</span><span style="color: #000000;"> </span><span style="color: #000000;">--</span><span style="color: #000000;"> </span><span style="color: #008080;">Flush</span><span style="color: #000000;"> all existing items at the server<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache</span><span style="color: #000000;">::</span><span style="color: #000000;">get </span><span style="color: #000000;">--</span><span style="color: #000000;"> Retrieve item from the server<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache</span><span style="color: #000000;">::</span><span style="color: #000000;">getStats </span><span style="color: #000000;">--</span><span style="color: #000000;"> Get statistics of the server<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache</span><span style="color: #000000;">::</span><span style="color: #000000;">getVersion </span><span style="color: #000000;">--</span><span style="color: #000000;"> </span><span style="color: #0000ff;">Return</span><span style="color: #000000;"> version of the server<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache</span><span style="color: #000000;">::</span><span style="color: #000000;">increment </span><span style="color: #000000;">--</span><span style="color: #000000;"> Increment item</span><span style="color: #000000;">'</span><span style="color: #000000;">s value<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache::pconnect -- Open memcached server persistent connection<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache::replace -- Replace value of the existing item<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache::set -- Store data at the server<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /> add a note User Contributed Notes<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Memcache Functions<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />ed at me3inc dot com<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />27-Oct-2006 05:47<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Re. Installing the memcache extension:<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />I had all kinds of troubles getting it hooked up, because in all of the instructions I read I never got the last, most important step - you must have a php.ini and have in it "extension=memcache.so."<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />So, the steps:<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />First - ./configure with --enable-memcache. This should show in your phpinfo() at the top (even though nothing of the memcache extension works yet).<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Second:<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />either pecl install memcache<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />OR<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />download the source<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />tar -xzvf [thesourcetarball]<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />phpize<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />./configure<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />make<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />make install<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Finally: Add extension=memcache.so to your php.ini. (If you don</span><span style="color: #000000;">'</span><span style="color: #000000;">t have one</span><span style="color: #000000;">,</span><span style="color: #000000;"> it should go in the root of where php is called ie</span><span style="color: #000000;">.,</span><span style="color: #000000;"> </span><span style="color: #000000;">/</span><span style="color: #000000;">usr</span><span style="color: #000000;">/</span><span style="color: #000000;">local</span><span style="color: #000000;">/</span><span style="color: #000000;">lib)<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Call </span><span style="color: #008080;">phpinfo</span><span style="color: #000000;">() and you should see a memcache section</span><span style="color: #000000;">.</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />iliya at pisem dot net<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">20</span><span style="color: #000000;">-</span><span style="color: #000000;">Jan</span><span style="color: #000000;">-</span><span style="color: #000000;">2006</span><span style="color: #000000;"> </span><span style="color: #000000;">04</span><span style="color: #000000;">:</span><span style="color: #000000;">35</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />one more </span><span style="color: #000000;">"</span><span style="color: #000000;">intelligent</span><span style="color: #000000;">"</span><span style="color: #000000;"> cache aggregator</span><span style="color: #000000;">:</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />https</span><span style="color: #000000;">:</span><span style="color: #008000;">//</span><span style="color: #008000;">svn.shadanakar.org/onPHP/ trunk/core/Cache/AggregateCache.class.php</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">can be used with several cache connectors </span><span style="color: #000000;">-</span><span style="color: #000000;"> memcached</span><span style="color: #000000;">,</span><span style="color: #000000;"> filesystem</span><span style="color: #000000;">,</span><span style="color: #000000;"> etc</span><span style="color: #000000;">.</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />(remove whitespace manually)<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Gregor J</span><span style="color: #000000;">.</span><span style="color: #000000;"> Rothfuss<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">22</span><span style="color: #000000;">-</span><span style="color: #000000;">Nov</span><span style="color: #000000;">-</span><span style="color: #000000;">2005</span><span style="color: #000000;"> </span><span style="color: #000000;">01</span><span style="color: #000000;">:</span><span style="color: #000000;">18</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />The </span><span style="color: #008080;">next</span><span style="color: #000000;"> version will have failover</span><span style="color: #000000;">.</span><span style="color: #000000;"> It</span><span style="color: #000000;">'</span><span style="color: #000000;">s been committed three weeks ago. Usage notes here: http://www.codecomments.com/archive367-2005-10-659421.html<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Ron<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />15-Sep-2005 04:19<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />An improvement to the above:<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />The above class will cause an error if all cache servers are down.  The preferred behavior is to just have a cache miss (or take no action in the case of write operations) and return false, so the app can run in non-cached mode if all cache servers are down.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />To make this happen, simply change the connection usage to look something like this in each affected function.  This code is for the get() function:<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       $con = $this-&gt;_getConForKey($key);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       if ($con === false) return false;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       return $con-&gt;get($key);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Similarly, the affected code in the set() function would look like this:<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   $con = $this-&gt;_getConForKey($key);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   if ($con === false) return false;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   return $con-&gt;set($key, $var, $compress, $expire);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Modify each function accordingly, and if all of your cache servers are down, you can still function (although more slowly due to the 100% cache miss rate).<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Ron<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />15-Sep-2005 01:20<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />Here is a simple memcached aggregator class which distributes the cache among multiple cache servers.  If a server fails, the load is redistributed automatically.  It uses persistent connections.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />The constructor takes an array of arrays, with each inner array representing a server, with a </span><span style="color: #000000;">'</span><span style="color: #000000;">server</span><span style="color: #000000;">'</span><span style="color: #000000;"> (string) attribute that is the IP addres or host name of the memcached server, and a </span><span style="color: #000000;">'</span><span style="color: #000000;">port</span><span style="color: #000000;">'</span><span style="color: #000000;"> (int) attribute that is the port number on which memcached is running on the server.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />All of the existing memcached API functions are implemented except getStats() and getVersion(), which are server-specific.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />&lt;?php<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />class MemcachedAggregator {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   var $connections;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   public function __construct($servers) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   // Attempt to establish/retrieve persistent connections to all servers.<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   // If any of them fail, they just don</span><span style="color: #000000;">'</span><span style="color: #000000;">t get put into our </span><span style="color: #0000ff;">list</span><span style="color: #000000;"> of active<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #008000;">//</span><span style="color: #008000;"> connections.</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">   </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">connections </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">array</span><span style="color: #000000;">();<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #800080;">$i</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$n</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">count</span><span style="color: #000000;">(</span><span style="color: #800080;">$servers</span><span style="color: #000000;">); </span><span style="color: #800080;">$i</span><span style="color: #000000;"> </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #800080;">$n</span><span style="color: #000000;">; </span><span style="color: #800080;">$i</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #800080;">$server</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$servers</span><span style="color: #000000;">[</span><span style="color: #800080;">$i</span><span style="color: #000000;">];<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #800080;">$con</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> memcache_pconnect(</span><span style="color: #800080;">$server</span><span style="color: #000000;">[</span><span style="color: #000000;">'</span><span style="color: #000000;">host</span><span style="color: #000000;">'</span><span style="color: #000000;">]</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$server</span><span style="color: #000000;">[</span><span style="color: #000000;">'</span><span style="color: #000000;">port</span><span style="color: #000000;">'</span><span style="color: #000000;">]);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">(</span><span style="color: #800080;">$con</span><span style="color: #000000;"> </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">)) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">connections[] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$con</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> _getConForKey(</span><span style="color: #800080;">$key</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #800080;">$hashCode</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #800080;">$i</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$len</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">strlen</span><span style="color: #000000;">(</span><span style="color: #800080;">$key</span><span style="color: #000000;">); </span><span style="color: #800080;">$i</span><span style="color: #000000;"> </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #800080;">$len</span><span style="color: #000000;">; </span><span style="color: #800080;">$i</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #800080;">$hashCode</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> (int)((</span><span style="color: #800080;">$hashCode</span><span style="color: #000000;">*</span><span style="color: #000000;">33</span><span style="color: #000000;">)</span><span style="color: #000000;">+</span><span style="color: #008080;">ord</span><span style="color: #000000;">(</span><span style="color: #800080;">$key</span><span style="color: #000000;">[</span><span style="color: #800080;">$i</span><span style="color: #000000;">])) </span><span style="color: #000000;">&amp;</span><span style="color: #000000;"> </span><span style="color: #000000;">0x7fffffff</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> ((</span><span style="color: #800080;">$ns</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #008080;">count</span><span style="color: #000000;">(</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">connections)) </span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">connections[</span><span style="color: #800080;">$hashCode</span><span style="color: #000000;">%</span><span style="color: #800080;">$ns</span><span style="color: #000000;">];<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> debug(</span><span style="color: #800080;">$on_off</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #800080;">$result</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #800080;">$i</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; </span><span style="color: #800080;">$i</span><span style="color: #000000;"> </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #008080;">count</span><span style="color: #000000;">(</span><span style="color: #800080;">$connections</span><span style="color: #000000;">); </span><span style="color: #800080;">$i</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">connections[</span><span style="color: #800080;">$i</span><span style="color: #000000;">]</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">debug(</span><span style="color: #800080;">$on_off</span><span style="color: #000000;">)) </span><span style="color: #800080;">$result</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$result</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> </span><span style="color: #008080;">flush</span><span style="color: #000000;">() {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #800080;">$result</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (</span><span style="color: #800080;">$i</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">; </span><span style="color: #800080;">$i</span><span style="color: #000000;"> </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #008080;">count</span><span style="color: #000000;">(</span><span style="color: #800080;">$connections</span><span style="color: #000000;">); </span><span style="color: #800080;">$i</span><span style="color: #000000;">++</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">connections[</span><span style="color: #800080;">$i</span><span style="color: #000000;">]</span><span style="color: #000000;">-&gt;</span><span style="color: #008080;">flush</span><span style="color: #000000;">()) </span><span style="color: #800080;">$result</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">true</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$result</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #008000;">//</span><span style="color: #008000;">/ The following are not implemented:</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />//</span><span style="color: #008000;">/getStats()</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />//</span><span style="color: #008000;">/getVersion()</span><span style="color: #008000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> get(</span><span style="color: #800080;">$key</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #008080;">is_array</span><span style="color: #000000;">(</span><span style="color: #800080;">$key</span><span style="color: #000000;">)) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #800080;">$dest</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">array</span><span style="color: #000000;">();<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #0000ff;">foreach</span><span style="color: #000000;"> (</span><span style="color: #800080;">$key</span><span style="color: #000000;"> </span><span style="color: #0000ff;">as</span><span style="color: #000000;"> </span><span style="color: #800080;">$subkey</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #800080;">$val</span><span style="color: #000000;"> </span><span style="color: #000000;">=</span><span style="color: #000000;"> get(</span><span style="color: #800080;">$subkey</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">(</span><span style="color: #800080;">$val</span><span style="color: #000000;"> </span><span style="color: #000000;">===</span><span style="color: #000000;"> </span><span style="color: #0000ff;">false</span><span style="color: #000000;">)) </span><span style="color: #800080;">$dest</span><span style="color: #000000;">[</span><span style="color: #800080;">$subkey</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">$val</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$dest</span><span style="color: #000000;">;<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_getConForKey(</span><span style="color: #800080;">$key</span><span style="color: #000000;">)</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">get(</span><span style="color: #800080;">$key</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> set(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$var</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$compress</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$expire</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_getConForKey(</span><span style="color: #800080;">$key</span><span style="color: #000000;">)</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">set(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$var</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$compress</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$expire</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> add(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$var</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$compress</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$expire</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_getConForKey(</span><span style="color: #800080;">$key</span><span style="color: #000000;">)</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">add(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$var</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$compress</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$expire</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> replace(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$var</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$compress</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$expire</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_getConForKey(</span><span style="color: #800080;">$key</span><span style="color: #000000;">)</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">replace<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />       (</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$var</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$compress</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$expire</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> delete(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$timeout</span><span style="color: #000000;">=</span><span style="color: #000000;">0</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_getConForKey(</span><span style="color: #800080;">$key</span><span style="color: #000000;">)</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">delete(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$timeout</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> increment(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$value</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_getConForKey(</span><span style="color: #800080;">$key</span><span style="color: #000000;">)</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">increment(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$value</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> decrement(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$value</span><span style="color: #000000;">=</span><span style="color: #000000;">1</span><span style="color: #000000;">) {<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #800080;">$this</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">_getConForKey(</span><span style="color: #800080;">$key</span><span style="color: #000000;">)</span><span style="color: #000000;">-&gt;</span><span style="color: #000000;">decrement(</span><span style="color: #800080;">$key</span><span style="color: #000000;">,</span><span style="color: #000000;"> </span><span style="color: #800080;">$value</span><span style="color: #000000;">);<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />   }<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" />}<br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span><span style="color: #000000;">?&gt;</span><span style="color: #000000;"><br />
<img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" alt="" align="top" /></span></div>
</div>
<p><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">难道，</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">4.3.3</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">都装上了吗？好像没有吧，用</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">4.4.4</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">好像都要另外装的</span></p>
<div><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">)</span></div>
</div>
<p><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">３。使用</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">memcache</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">情况，计数器、数据压缩</span></strong><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">使用情况一：统计</span></span></p>
<p>&nbsp;</p></blockquote>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;" align="left">(PHP 4.3.3 or newer is required to use the memcache extension.</p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; background: #e6e6e6; word-break: break-all; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;">&lt;?php<br />
</span><span style="color: #008000;">//</span></span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">访问统计</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = </span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">new</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> Memcache;<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;connect('localhost', 11211) or </span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">die</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> ("Could not connect");<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">if</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$s</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">=</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;get('a')) {<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$s</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">=</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$s</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">+1;<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;set('a',</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$s</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">);<br />
}<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">else</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;set('a',1);<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">访问结果为：</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$s</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">;<br />
?&gt;</span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;" align="left"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">其实我们可以用</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">increment</span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">方法代替上面的做法</span></p>
<div class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; background: #e6e6e6; word-break: break-all; mso-pagination: widow-orphan;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;">&lt;?php<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;"> = </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Memcache;<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;connect('localhost', 11211) or </span><span style="color: #0000ff;">die</span><span style="color: #000000;"> ("Could not connect");</span></span></div>
<p><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;"><span style="color: #0000ff;">if</span><span style="color: #000000;">(</span><span style="color: #800080;">$s</span><span style="color: #000000;">=</span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;increment('a',1)) {<br />
</span><span style="color: #0000ff;">echo</span><span style="color: #000000;"> </span><span style="color: #800080;">$s</span><span style="color: #000000;">;<br />
}<br />
</span><span style="color: #0000ff;">else</span><span style="color: #000000;"><br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;set('a',1);<br />
?&gt;</span></span></span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">数据压缩</span></span><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">：</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">  </span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; background: #e6e6e6; word-break: break-all; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;">&lt;?php<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;"> = </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Memcache;<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;connect('localhost', 11211) or </span><span style="color: #0000ff;">die</span><span style="color: #000000;"> ("Could not connect");<br />
</span><span style="color: #800080;">$test</span><span style="color: #000000;">=(</span><span style="color: #008080;">str_repeat</span><span style="color: #000000;">('jetwong',100000));<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;set('b',(</span><span style="color: #800080;">$test</span><span style="color: #000000;">));<br />
?&gt;<br />
</span></span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">使用压缩：</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
&lt;?php<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = </span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">new</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> Memcache;<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;connect('localhost', 11211) or </span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">die</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> ("Could not connect");<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$test</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">=(</span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">str_repeat</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">('jetwong',100000));<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;set('b',(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$test</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">),MEMCACHE_COMPRESSED);<br />
?&gt;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><br />
</span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">使用情况说明：</span></span></p>
<table class="MsoNormalTable" style="width: 275.25pt; mso-cellspacing: .7pt;" width="367" border="1" cellspacing="1" cellpadding="0">
<tbody>
<tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes;">
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">前台比较</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">目前内存</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
bytes</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">总共读取</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
bytes_read</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">总共写入</span><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><br />
bytes_written</span></p>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">压缩前</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">700085</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">700081</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">416</span></p>
</td>
</tr>
<tr style="mso-yfti-irow: 2; mso-yfti-lastrow: yes;">
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">压缩后</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">1131</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">1125</span></p>
</td>
<td style="background-color: transparent; border-color: #d4d0c8; padding: 0.75pt;">
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">13</span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">可能看到压缩后明显占用内存少了不少</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">４。</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">Memcache</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">内存的更新清理</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">(delete flush)</span></strong></p>
<div class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; background: #e6e6e6; word-break: break-all; mso-pagination: widow-orphan;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;">&lt;?php<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;"> = </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Memcache;<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;connect('localhost', 11211) or </span><span style="color: #0000ff;">die</span><span style="color: #000000;"> ("Could not connect");</span></span></div>
<p><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;"><span style="color: #008000;">/*</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">设置值</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">*/</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = </span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;getStats();<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">设置前内存使用情况</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">['bytes'].'&lt;br&gt;';<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">设置后</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">';<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">for</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">=0;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">&lt;9;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">++) {<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;set('b'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">,</span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">rand</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(1,99));<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '&lt;br&gt;'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">.'-&gt;'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;get('b'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span></span></span></p>
<div><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">);<br />
}</span></div>
<p><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">/*</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">查看设置的值</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">*/</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = </span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;getStats();<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> 'delete</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">前内存使用情况</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">['bytes'].'&lt;br&gt;';<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '&lt;br&gt;</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">开始</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">delete';<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">for</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">=0;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">&lt;9;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">++) {<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;delete('b'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">);<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '&lt;br&gt;'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">.'-&gt;'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;get('b'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span></span></p>
<div><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">);<br />
}</span></div>
<p><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">/*</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">查看</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">flush</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">使用的情况</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">*/</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = </span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;getStats();<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">使用</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">flush</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">前内存使用情况</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">['bytes'].'&lt;br&gt;';<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">使用</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">flush</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">情况：</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">';<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">for</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">=0;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">&lt;9;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">++) {<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;set('b'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">,</span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">rand</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(1,99));<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '&lt;br&gt;'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">.'-&gt;'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;get('b'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">);<br />
}<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;</span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">flush</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">();<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> 'flush</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">之后：</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">';<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">for</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">=0;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">&lt;9;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">++) {<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '&lt;br&gt;'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">.'-&gt;'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;get('b'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">);<br />
}<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = </span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;getStats();<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> 'flush</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">后内存使用情况</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">['bytes'].'&lt;br&gt;';<br />
?&gt;</span></span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><strong style="mso-bidi-font-weight: normal;"><span style="font-family: 宋体; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">５。内存超量的测试</span></strong><strong style="mso-bidi-font-weight: normal;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;">(set)</span></strong></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="mso-spacerun: yes;"><span style="font-family: Times New Roman;"> </span></span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">我们把内存设为</span><span lang="EN-US"><span style="font-family: Times New Roman;">2M</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;">./memcached -d -m 2 -p 11211 -u root</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span></p>
<div class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; background: #e6e6e6; word-break: break-all; mso-pagination: widow-orphan;"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;">&lt;?php<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;"> = </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Memcache;<br />
</span><span style="color: #800080;">$memcache</span><span style="color: #000000;">-&gt;connect('localhost', 11211) or </span><span style="color: #0000ff;">die</span><span style="color: #000000;"> ("Could not connect");</span></span></div>
<p><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;"><span style="color: #008000;">//600K</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">左右</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$test1</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">= </span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">str_repeat</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">('jetlee',100000);<br />
</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">//600K</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">左右</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$test2</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">= </span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">str_repeat</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">('jetlee',100000);<br />
</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">//600K</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">左右</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$test3</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">= </span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">str_repeat</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">('</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">李连杰</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">',200000);<br />
</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">//600K</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">左右</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$test4</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">= </span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">str_repeat</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">('</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">连杰李</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">',100000);<br />
</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">//200K<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$test5</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">= </span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">file_get_contents</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">('http://img.pconline.com.cn/images/photoblog/2988177/20068/4/1154688770042_mthumb.JPG');<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$test6</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">= </span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">file_get_contents</span></span></span></p>
<div><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">('http://img.pconline.com.cn/images/photoblog/1767557/20069/28/1159417108902_mthumb.jpg');</span></div>
<p><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">for</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">=1;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">&lt;=6;</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">++) {<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$j</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">='test'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$i</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">;<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">if</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;set(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$j</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">,$</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$j</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">)) {<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> </span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$j</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">.'-&gt;</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">设置成功</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">&lt;br&gt;';<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = </span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;getStats();<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> '</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">内存</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">:'.</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$status</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">['bytes'].'&lt;br&gt;';<br />
}<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">else</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> {<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">echo</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> </span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$j</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">.'-&gt;</span><span style="font-family: 宋体; color: #000000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">设置失败</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">&lt;br&gt;';<br />
}<br />
}<br />
?&gt;</span></span></p>
<p>&nbsp;</p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">执行结果：</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">test1-&gt;</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">设置成功</span></span><span lang="EN-US"><br />
</span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">内存</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">:600042<br />
test2-&gt;</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">设置成功</span></span><span lang="EN-US"><br />
</span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">内存</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">:1200084<br />
test3-&gt;</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">设置失败</span></span><span lang="EN-US"><br />
<span style="font-family: Times New Roman; font-size: small;">test4-&gt;</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">设置成功</span></span><span lang="EN-US"><br />
</span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">内存</span></span><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">:1200084<br />
test5-&gt;</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">设置失败</span></span><span lang="EN-US"><br />
<span style="font-family: Times New Roman; font-size: small;">test6-&gt;</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">设置失败</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: small;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">刚好印证我们的计算，不过</span><span lang="EN-US"><span style="font-family: Times New Roman;">20</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">万的</span><span lang="EN-US"><span style="font-family: Times New Roman;">repeat</span></span><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">为什么会失败，不是太了解，，，，，，</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">总结：</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';"><span style="font-size: small;">示例：</span></span></p>
<p class="MsoNormal" style="text-align: left; margin: 0cm 0cm 0pt; background: #e6e6e6; word-break: break-all; mso-pagination: widow-orphan;" align="left"><span style="font-family: Arial; font-size: 9pt; mso-font-kerning: 0pt;"><span style="color: #000000;">&lt;?php<br />
</span><span style="color: #008000;">//</span></span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">设置篇</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">if</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$data</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = </span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;get('k',</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$v</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">)) {<br />
</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">//</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">显示我们的数据</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">    }<br />
</span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">else</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> {<br />
</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$data</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;"> = get_from_database; </span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">//</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">得到数据源</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">    </span><span style="font-family: Arial; color: #0000ff; font-size: 9pt; mso-font-kerning: 0pt;">if</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">(!</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$memcache</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">-&gt;set('k',</span><span style="font-family: Arial; color: #800080; font-size: 9pt; mso-font-kerning: 0pt;">$data</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">),</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">MEMCACHE_COMPRESSED) </span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">//</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">开始设置</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">    </span><span style="font-family: Arial; color: #008080; font-size: 9pt; mso-font-kerning: 0pt;">log</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">();    </span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">//</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">不成功</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;">,</span><span style="font-family: 宋体; color: #008000; font-size: 9pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-font-kerning: 0pt; mso-bidi-font-family: Arial;">记录失败信息</span><span style="font-family: Arial; color: #008000; font-size: 9pt; mso-font-kerning: 0pt;"><br />
</span><span style="font-family: Arial; color: #000000; font-size: 9pt; mso-font-kerning: 0pt;">}<br />
?&gt;</span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span><span lang="EN-US"><span style="font-family: Times New Roman; font-size: small;"> </span></span><span class="Apple-style-span" style="font-family: 'Times New Roman'; font-size: small;">-----------OVER-----------</span></p>
<p><strong>相关教程:</strong></p>
<p>Linux下Memcached的安装及配置:<a href="http://blog.haohtml.com/archives/364" target="_blank">http://blog.haohtml.com/archives/364</a><br />
基于 PHP5 &amp; JQuery 的 Memcached 管理监控工具:<a href="http://www.junopen.com/memadmin/">http://www.junopen.com/memadmin/</a></p>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="在安装php的make时候,出现错误&quot;chmod: cannot access `ext/phar/phar.phar': No such file or directory&quot;的解决办法" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F12482&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">在安装php的make时候,出现错误&quot;chmod: cannot access `ext/phar/phar.phar': No such file or directory&quot;的解决办法</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached for win32" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached for win32</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="利用memcached构建高性能的Web应用程序" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026094.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用memcached构建高性能的Web应用程序</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="freebsd+php+memcache、memcached安装和使用" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">freebsd+php+memcache、memcached安装和使用</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/395/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>利用memcached构建高性能的Web应用程序</title>
		<link>http://blog.haohtml.com/archives/374</link>
		<comments>http://blog.haohtml.com/archives/374#comments</comments>
		<pubDate>Wed, 24 Sep 2008 11:25:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[服务器类]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[memcached]]></category>

		<guid isPermaLink="false">http://haohtml.com/wp/?p=374</guid>
		<description><![CDATA[Memcached 介绍

    Memcached是开源的分布式cache系统，现在很多的大型web应用程序包括facebook，youtube，wikipedia，yahoo等等都在使用memcached来支持他们每天数亿级的页面访问。通过把cache层与他们的web架构集成，他们的应用程序在提高了性能的同时，还大大降低了数据库的负载。
具体的memcached资料大家可以直接从它的官方网站[1]上得到。这里我就简单给大家介绍一下memcached的工作原理：

    Memcached处理的原子是每一个（key，value）对（以下简称kv对），key会通过一个hash算法转化成hash-key，便于查找、对比以及做到尽可能的散列。同时，memcached用的是一个二级散列，通过一张大hash表来维护。<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="[缓存加速]eAccelerator与memcached的区别与用途" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">[缓存加速]eAccelerator与memcached的区别与用途</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached配置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached配置</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached for win32" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached for win32</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Memcached深度分析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3955&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Memcached深度分析</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<div class="entry">
<p><strong>面临的问题</strong></p>
<p>    对于高并发高访问的Web应用程序来说，数据库存取瓶颈一直是个令人头疼的问题。特别当你的程序架构还是建立在单数据库模式，而一个数据池连接数峰值已经达到500的时候，那你的程序运行离崩溃的边缘也不远了。很多小网站的开发人员一开始都将注意力放在了产品需求设计上，缺忽视了程序整体性能，可扩展性等方面的考虑，结果眼看着访问量一天天网上爬，可突然发现有一天网站因为访问量过大而崩溃了，到时候哭都来不及。所以我们一定要未雨绸缪，在数据库还没罢工前，想方设法给它减负，这也是这篇文章的主要议题。</p>
<p>    大家都知道，当有一个request过来后，web服务器交给app服务器，app处理并从db中存取相关数据，但db存取的花费是相当高昂的。特别是每次都取相同的数据，等于是让数据库每次都在做高耗费的无用功，数据库如果会说话，肯定会发牢骚，你都问了这么多遍了，难道还记不住吗？是啊，如果app拿到第一次数据并存到内存里，下次读取时直接从内存里读取，而不用麻烦数据库，这样不就给数据库减负了？而且从内存取数据必然要比从数据库媒介取快很多倍，反而提升了应用程序的性能。</p>
<p>    因此，我们可以在web/app层与db层之间加一层cache层，主要目的：1. 减少数据库读取负担；2. 提高数据读取速度。而且，cache存取的媒介是内存，而一台服务器的内存容量一般都是有限制的，不像硬盘容量可以做到TB级别。所以，可以考虑采用分布式的cache层，这样更易于破除内存容量的限制，同时又增加了灵活性。</p>
<p><strong>Memcached 介绍</strong></p>
<p>    Memcached是开源的分布式cache系统，现在很多的大型web应用程序包括facebook，youtube，wikipedia，yahoo等等都在使用memcached来支持他们每天数亿级的页面访问。通过把cache层与他们的web架构集成，他们的应用程序在提高了性能的同时，还大大降低了数据库的负载。<br />
具体的memcached资料大家可以直接从它的<a href="http://danga.com/memcached/" target="_blank"><span style="color: #2349ff;">官方网站</span></a>[1]上得到。这里我就简单给大家介绍一下memcached的工作原理：</p>
<p>    Memcached处理的原子是每一个（key，value）对（以下简称kv对），key会通过一个hash算法转化成hash-key，便于查找、对比以及做到尽可能的散列。同时，memcached用的是一个二级散列，通过一张大hash表来维护。</p>
<p>    Memcached有两个核心组件组成：服务端（ms）和客户端（mc），在一个memcached的查询中，mc先通过计算key的hash值来确定kv对所处在的ms位置。当ms确定后，客户端就会发送一个查询请求给对应的ms，让它来查找确切的数据。因为这之间没有交互以及多播协议，所以memcached交互带给网络的影响是最小化的。</p>
<p>举例说明：考虑以下这个场景，有三个mc分别是X，Y，Z，还有三个ms分别是A，B，C：</p>
<p>设置kv对<br />
X想设置key=”foo”,value=”seattle”<br />
X拿到ms列表，并对key做hash转化，根据hash值确定kv对所存的ms位置<br />
B被选中了<br />
X连接上B，B收到请求，把（key=”foo”,value=”seattle”）存了起来</p>
<p>获取kv对<br />
Z想得到key=”foo”的value<br />
Z用相同的hash算法算出hash值，并确定key=”foo”的值存在B上<br />
Z连接上B，并从B那边得到value=”seattle”<br />
其他任何从X，Y，Z的想得到key=”foo”的值的请求都会发向B</p>
<p><a href="http://it.dianping.com/static/2008/07/a.jpg" rel="lightbox[374]"><img class="alignnone size-medium wp-image-85" src="http://it.dianping.com/static/2008/07/a.jpg" alt="" width="355" height="423" /></a></p>
<p><strong>Memcached服务器(ms)<br />
</strong></p>
<p><strong>内存分配</strong></p>
<p>默认情况下，ms是用一个内置的叫“块分配器”的组件来分配内存的。舍弃c++标准的malloc/free的内存分配，而采用块分配器的主要目的是为了避免内存碎片，否则操作系统要花费更多时间来查找这些逻辑上连续的内存块（实际上是断开的）。用了块分配器，ms会轮流的对内存进行大块的分配，并不断重用。当然由于块的大小各不相同，当数据大小和块大小不太相符的情况下，还是有可能导致内存的浪费。</p>
<p>同时，ms对key和data都有相应的限制，key的长度不能超过250字节，data也不能超过块大小的限制 --- 1MB。<br />
因为mc所使用的hash算法，并不会考虑到每个ms的内存大小。理论上mc会分配概率上等量的kv对给每个ms，这样如果每个ms的内存都不太一样，那可能会导致内存使用率的降低。所以一种替代的解决方案是，根据每个ms的内存大小，找出他们的最大公约数，然后在每个ms上开n个容量=最大公约数的instance，这样就等于拥有了多个容量大小一样的子ms，从而提供整体的内存使用率。</p>
<p><strong>缓存策略</strong></p>
<p>当ms的hash表满了之后，新的插入数据会替代老的数据，更新的策略是LRU（最近最少使用），以及每个kv对的有效时限。Kv对存储有效时限是在mc端由app设置并作为参数传给ms的。</p>
<p>同时ms采用是偷懒替代法，ms不会开额外的进程来实时监测过时的kv对并删除，而是当且仅当，新来一个插入的数据，而此时又没有多余的空间放了，才会进行清除动作。</p>
<p><strong>缓存数据库查询</strong><br />
现在memcached最流行的一种使用方式是缓存数据库查询，下面举一个简单例子说明：</p>
<p>App需要得到userid=xxx的用户信息，对应的查询语句类似：</p>
<p>“SELECT * FROM users WHERE userid = xxx”</p>
<p>App先去问cache，有没有“user:userid”（key定义可预先定义约束好）的数据，如果有，返回数据；如果没有，App会从数据库中读取数据，并调用cache的add函数，把数据加入cache中。</p>
<p>当取的数据需要更新，app会调用cache的update函数，来保持数据库与cache的数据同步。</p>
<p>从上面的例子我们也可以发现，一旦数据库的数据发现变化，我们一定要及时更新cache中的数据，来保证app读到的是同步的正确数据。当然我们可以通过定时器方式记录下cache中数据的失效时间，时间一过就会激发事件对cache进行更新，但这之间总会有时间上的延迟，导致app可能从cache读到脏数据，这也被称为狗洞问题。（以后我会专门描述研究这个问题）</p>
<p><strong>数据冗余与故障预防</strong></p>
<p>从设计角度上，memcached是没有数据冗余环节的，它本身就是一个大规模的高性能cache层，加入数据冗余所能带来的只有设计的复杂性和提高系统的开支。</p>
<p>当一个ms上丢失了数据之后，app还是可以从数据库中取得数据。不过更谨慎的做法是在某些ms不能正常工作时，提供额外的ms来支持cache，这样就不会因为app从cache中取不到数据而一下子给数据库带来过大的负载。</p>
<p>同时为了减少某台ms故障所带来的影响，可以使用“热备份”方案，就是用一台新的ms来取代有问题的ms，当然新的ms还是要用原来ms的IP地址，大不了数据重新装载一遍。</p>
<p>另外一种方式，就是提高你ms的节点数，然后mc会实时侦查每个节点的状态，如果发现某个节点长时间没有响应，就会从mc的可用server列表里删除，并对server节点进行重新hash定位。当然这样也会造成的问题是，原本key存储在B上，变成存储在C上了。所以此方案本身也有其弱点，最好能和“热备份”方案结合使用，就可以使故障造成的影响最小化。</p>
<p><strong>Memcached客户端（mc）</strong></p>
<p>Memcached客户端有各种语言的版本供大家使用，包括java，c，php，.net等等，具体可参见<a href="http://danga.com/memcached/apis.bml" target="_blank"><span style="color: #2349ff;">memcached api page</span></a>[2]。<br />
大家可以根据自己项目的需要，选择合适的客户端来集成。</p>
<p><strong>缓存式的Web应用程序架构</strong><br />
有了缓存的支持，我们可以在传统的app层和db层之间加入cache层，每个app服务器都可以绑定一个mc，每次数据的读取都可以从ms中取得，如果没有，再从db层读取。而当数据要进行更新时，除了要发送update的sql给db层，同时也要将更新的数据发给mc，让mc去更新ms中的数据。</p>
<p><a href="http://it.dianping.com/static/2008/07/b.jpg" rel="lightbox[374]"><img class="alignnone size-medium wp-image-86" src="http://it.dianping.com/static/2008/07/b.jpg" alt="" width="556" height="459" /></a></p>
<p>假设今后我们的数据库可以和ms进行通讯了，那可以将更新的任务统一交给db层，每次数据库更新数据的同时会自动去更新ms中的数据，这样就可以进一步减少app层的逻辑复杂度。如下图：</p>
<p><a href="http://it.dianping.com/static/2008/07/c.jpg" rel="lightbox[374]"><img class="alignnone size-medium wp-image-87" src="http://it.dianping.com/static/2008/07/c.jpg" alt="" width="556" height="459" /></a></p>
<p>不过每次我们如果没有从cache读到数据，都不得不麻烦数据库。为了最小化数据库的负载压力，我们可以部署数据库复写，用slave数据库来完成读取操作，而master数据库永远只负责三件事：1.更新数据；2.同步slave数据库；3.更新cache。如下图：</p>
<p><a href="http://it.dianping.com/static/2008/07/d1.jpg" rel="lightbox[374]"><img class="alignnone size-medium wp-image-96" src="http://it.dianping.com/static/2008/07/d1.jpg" alt="" width="555" height="417" /></a></p>
<p>以上这些缓存式web架构在实际应用中被证明是能有效并能极大地降低数据库的负载同时又能提高web的运行性能。当然这些架构还可以根据具体的应用环境进行变种，以达到不同硬件条件下性能的最优化。</p>
<p><strong>未来的憧憬</strong><br />
Memcached的出现可以说是革命性的，第一次让我们意识到可以用内存作为存储媒介来大规模的缓存数据以提高程序的性能。不过它毕竟还是比较新的东西，还需要很多有待优化和改进的地方，例如：<br />
如何利用memcached实现cache数据库，让数据库跑在内存上。这方面，tangent software 开发的<a href="http://tangent.org/506/memcache_engine.html" target="_blank"><span style="color: #2349ff;">memcached_engine</span></a>[3]已经做了不少工作，不过现在的版本还只是处于实验室阶段。<br />
如何能方便有效的进行批量key清理。因为现在key是散列在不同的server上的，所以对某类key进行大批量清理是很麻烦的。因为memcached本身是一个大hash表，是不具备key的检索功能的。所以memcached是压根不知道某一类的key到底存了多少个，都存在哪些server上。而这类功能在实际应用中却是经常用到。</p>
<p><strong>交流</strong><br />
作者也是刚接触memcached方面的内容，所以严格来说还只是个新手，班门弄斧地说了一大通，如果有不对的地方，还请各位大侠多多指正。当然，如果有什么和memcached方面有关的问题或建议，也欢迎和我联系。<br />
联系Email: rongwei.yang@dianping.com</p>
<p><strong>参考</strong><br />
[1]. Memcached website: http://danga.com/memcached/<br />
[2]. Memcached API Page: http://danga.com/memcached/apis.bml<br />
[3]. memcached_engine: http://tangent.org/506/memcache_engine.html</div>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="[缓存加速]eAccelerator与memcached的区别与用途" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">[缓存加速]eAccelerator与memcached的区别与用途</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached配置" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached配置</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached for win32" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached for win32</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Memcached深度分析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3955&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">Memcached深度分析</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/374/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>memcached配置</title>
		<link>http://blog.haohtml.com/archives/364</link>
		<comments>http://blog.haohtml.com/archives/364#comments</comments>
		<pubDate>Wed, 24 Sep 2008 11:16:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[数据库类]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[memcached]]></category>

		<guid isPermaLink="false">http://haohtml.com/wp/?p=364</guid>
		<description><![CDATA[一、memcached 简介

在很多场合，我们都会听到 memcached 这个名字，但很多同学只是听过，并没有用过或实际了解过，只知道它是一个很不错的东东。这里简单介绍一下，memcached 是高效、快速的分布式内存对象缓存系统，主要用于加速 WEB 动态应用程序。

二、memcached 安装

首先是下载 memcached 了，目前最新版本是 1.1.12，直接从官方网站即可下载到 memcached-1.1.12.tar.gz。除此之外，memcached 用到了 libevent，我下载的是 libevent-1.1a.tar.gz。<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="[缓存加速]eAccelerator与memcached的区别与用途" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">[缓存加速]eAccelerator与memcached的区别与用途</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="利用memcached构建高性能的Web应用程序" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026094.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用memcached构建高性能的Web应用程序</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached for win32" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached for win32</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached安装+php使用手记" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached安装+php使用手记</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p><strong>一、memcached 简介</strong></p>
<p>在很多场合，我们都会听到 <a href="http://www.danga.com/memcached/"><span style="color: #333333;">memcached</span></a> 这个名字，但很多同学只是听过，并没有用过或实际了解过，只知道它是一个很不错的东东。这里简单介绍一下，memcached 是高效、快速的分布式内存对象缓存系统，主要用于加速 WEB 动态应用程序。</p>
<p><strong>二、memcached 安装</strong></p>
<p>首先是下载 memcached 了，目前最新版本是 1.1.12，直接从官方网站即可下载到 <a href="http://www.danga.com/memcached/dist/memcached-1.1.12.tar.gz"><span style="color: #333333;">memcached-1.1.12.tar.gz</span></a>。除此之外，memcached 用到了 <a href="http://monkey.org/~provos/libevent/"><span style="color: #333333;">libevent</span></a>，我<a href="http://monkey.org/~provos/libevent-1.1a.tar.gz"><span style="color: #333333;">下载的是 libevent-1.1a.tar.gz</span></a>。</p>
<p>接下来是分别将 libevent-1.1a.tar.gz 和 memcached-1.1.12.tar.gz 解开包、编译、安装：</p>
<pre class="code" style="padding-bottom: 20px; width: 500px;"># tar -xzf libevent-1.1a.tar.gz
# cd libevent-1.1a
# ./configure --prefix=/usr
# make
# make install
# cd ..
# tar -xzf memcached-1.1.12.tar.gz
# cd memcached-1.1.12
# ./configure --prefix=/usr
# make
# make install</pre>
<p>安装完成之后，memcached 应该在 /usr/bin/memcached。</p>
<p>如果在安装过程中提示：</p>
<blockquote><p><span style="color: #0000ff;">checking for libevent directory... configure: error: libevent is required. You can get it from http://www.monkey.org/~provos/libevent/</span><br />
<span style="color: #0000ff;"> If it's already installed, specify its path using --with-libevent=/dir/</span></p></blockquote>
<p>解决办法请参考:<a href="http://blog.haohtml.com/archives/11664">http://blog.haohtml.com/archives/11664</a></p>
<p><strong>三、运行 memcached 守护程序</strong></p>
<p>运行 memcached 守护程序很简单，只需一个命令行即可，不需要修改任何配置文件（也没有配置文件给你修改  ）：</p>
<pre class="code" style="padding-bottom: 20px; width: 500px;">/usr/bin/memcached -d -m 128 -l 192.168.1.1 -p 11211 -u root</pre>
<p>参数解释：</p>
<pre class="code" style="padding-bottom: 20px; width: 500px;">-d 以守护程序（daemon）方式运行 memcached；
-m 设置 memcached 可以使用的内存大小，单位为 M；
-l 设置监听的 IP 地址，如果是本机的话，通常可以不设置此参数；
-p 设置监听的端口，默认为 11211，所以也可以不设置此参数；
-u 指定用户，如果当前为 root 的话，需要使用此参数指定用户。</pre>
<p>当然，还有其它参数可以用，<code><span style="font-family: 新宋体;">man memcached</span></code> 一下就可以看到了。</p>
<p><strong>四、memcached 的工作原理</strong></p>
<p>首先 memcached 是以守护程序方式运行于一个或多个服务器中，随时接受客户端的连接操作，客户端可以由各种语言编写，目前已知的客户端 API 包括 Perl/PHP/Python/Ruby/Java/C#/C 等等。PHP 等客户端在与 memcached 服务建立连接之后，接下来的事情就是存取对象了，每个被存取的对象都有一个唯一的标识符 key，存取操作均通过这个 key 进行，保存到 memcached 中的对象实际上是放置内存中的，并不是保存在 cache 文件中的，这也是为什么 memcached 能够如此高效快速的原因。注意，这些对象并不是持久的，服务停止之后，里边的数据就会丢失。</p>
<p><strong>三、PHP 如何作为 memcached 客户端</strong></p>
<p>有两种方法可以使 PHP 作为 memcached 客户端，调用 memcached 的服务进行对象存取操作。</p>
<p>第一种，PHP 有一个叫做 <a href="http://www.php.net/manual/en/ref.memcache.php"><span style="color: #333333;">memcache 的扩展</span></a>，Linux 下编译时需要带上 –enable-memcache[=DIR] 选项，Window 下则在 php.ini 中去掉 php_memcache.dll 前边的注释符，使其可用。</p>
<p>除此之外，还有一种方法，可以避开扩展、重新编译所带来的麻烦，那就是直接使用 <a href="http://wikipedia.sourceforge.net/doc/memcached-client/_includes_memcached-client_php.html"><span style="color: #333333;">php-memcached-client</span></a>。</p>
<p>本文选用第二种方式，虽然效率会比扩展库稍差一些，但问题不大。</p>
<p><strong>四、PHP memcached 应用示例</strong></p>
<p>首先 <a name="p481" href="http://www.infor96.com/~nio/wp-content/uploads/2006/04/memcached-client.zip"></a><span style="color: #333333;">下载 memcached-client.php</span>，在下载了 memcached-client.php 之后，就可以通过这个文件中的类“memcached”对 memcached 服务进行操作了。其实代码调用非常简单，主要会用到的方法有 add()、get()、replace() 和 delete()，方法说明如下：</p>
<p><span style="font-family: 新宋体;"><code>add ($key, $val, $exp = 0)</code><br />
</span>往 memcached 中写入对象，$key 是对象的唯一标识符，$val 是写入的对象数据，$exp 为过期时间，单位为秒，默认为不限时间；</p>
<p><span style="font-family: 新宋体;"><code>get ($key)</code><br />
</span>从 memcached 中获取对象数据，通过对象的唯一标识符 $key 获取；</p>
<p><span style="font-family: 新宋体;"><code>replace ($key, $value, $exp=0)</code><br />
</span>使用 $value 替换 memcached 中标识符为 $key 的对象内容，参数与 add() 方法一样，只有 $key 对象存在的情况下才会起作用；</p>
<p><span style="font-family: 新宋体;"><code>delete ($key, $time = 0)</code><br />
</span>删除 memcached 中标识符为 $key 的对象，$time 为可选参数，表示删除之前需要等待多长时间。</p>
<p>下面是一段简单的测试代码，代码中对标识符为 ‘mykey’ 的对象数据进行存取操作：</p>
<pre class="code" style="padding-bottom: 20px; width: 500px;"><code><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php <br></span><span style="color: #ff8000;">//   包含 memcached 类文件<br> </span><span style="color: #007700;">require_once(</span><span style="color: #dd0000;">‘memcached-client.php’</span><span style="color: #007700;">);<br> </span><span style="color: #ff8000;">//   选项设置<br> </span><span style="color: #0000bb;">$options </span><span style="color: #007700;">= array(<br>     </span><span style="color: #dd0000;">’servers’ </span><span style="color: #007700;">=&gt; array(</span><span style="color: #dd0000;">‘192.168.1.1:11211′</span><span style="color: #007700;">),/</span><span style="color: #ff8000;">/memcached 服务的地址、端口，可用多个数组元素表示多个 memcached 服务<br>     </span><span style="color: #dd0000;">‘debug’ </span><span style="color: #007700;">=&gt; </span><span style="color: #0000bb;">true</span><span style="color: #007700;">,  </span><span style="color: #ff8000;">//是否打开 debug<br>     </span><span style="color: #dd0000;">‘compress_threshold’ </span><span style="color: #007700;">=&gt; </span><span style="color: #0000bb;">10240</span><span style="color: #007700;">,<br>  </span><span style="color: #ff8000;">//超过多少字节的数据时进行压缩<br>     </span><span style="color: #dd0000;">‘persistant’ </span><span style="color: #007700;">=&gt; </span><span style="color: #0000bb;">false  </span><span style="color: #ff8000;">//是否使用持久连接<br>     </span><span style="color: #007700;">);<br> </span><span style="color: #ff8000;">//   创建 memcached 对象实例 <br></span><span style="color: #0000bb;">$mc </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">memcached</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$options</span><span style="color: #007700;">); <br></span><span style="color: #ff8000;">//   设置此脚本使用的唯一标识符 <br></span><span style="color: #0000bb;">$key </span><span style="color: #007700;">= </span><span style="color: #dd0000;">‘mykey’</span><span style="color: #007700;">; <br></span><span style="color: #ff8000;">//   往 memcached 中写入对象 </span><span style="color: #0000bb;"><br>$mc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">add</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">, </span><span style="color: #dd0000;">’some random strings’</span><span style="color: #007700;">);<br> </span><span style="color: #0000bb;">$val </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$mc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">get</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">); <br>echo </span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">.</span><span style="color: #0000bb;">str_pad</span><span style="color: #007700;">(</span><span style="color: #dd0000;">‘$mc-&gt;add() ’</span><span style="color: #007700;">, </span><span style="color: #0000bb;">60</span><span style="color: #007700;">, </span><span style="color: #dd0000;">‘_’</span><span style="color: #007700;">).</span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">; <br></span><span style="color: #0000bb;">var_dump</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$val</span><span style="color: #007700;">); <br></span><span style="color: #ff8000;">//   替换已写入的对象数据值 <br></span><span style="color: #0000bb;">$mc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">replace</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">, array(</span><span style="color: #dd0000;">’some’</span><span style="color: #007700;">=&gt;</span><span style="color: #dd0000;">‘haha’</span><span style="color: #007700;">, </span><span style="color: #dd0000;">‘array’</span><span style="color: #007700;">=&gt;</span><span style="color: #dd0000;">‘xxx’</span><span style="color: #007700;">));<br> </span><span style="color: #0000bb;">$val </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$mc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">get</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">); <br>echo </span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">.</span><span style="color: #0000bb;">str_pad</span><span style="color: #007700;">(</span><span style="color: #dd0000;">‘$mc-&gt;replace() ’</span><span style="color: #007700;">, </span><span style="color: #0000bb;">60</span><span style="color: #007700;">, </span><span style="color: #dd0000;">‘_’</span><span style="color: #007700;">).</span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">;<br> </span><span style="color: #0000bb;">var_dump</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$val</span><span style="color: #007700;">);<br> </span><span style="color: #ff8000;">//   删除 memcached 中的对象 <br></span><span style="color: #0000bb;">$mc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">delete</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">); <br></span><span style="color: #0000bb;">$val </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$mc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">get</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">);<br> echo </span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">.</span><span style="color: #0000bb;">str_pad</span><span style="color: #007700;">(</span><span style="color: #dd0000;">‘$mc-&gt;delete() ’</span><span style="color: #007700;">, </span><span style="color: #0000bb;">60</span><span style="color: #007700;">, </span><span style="color: #dd0000;">‘_’</span><span style="color: #007700;">).</span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">; <br></span><span style="color: #0000bb;">var_dump</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$val</span><span style="color: #007700;">); </span><span style="color: #0000bb;"><br>?&gt; </span></span></code></pre>
<p>是不是很简单，在实际应用中，通常会把数据库查询的结果集保存到 memcached 中，下次访问时直接从 memcached 中获取，而不再做数据库查询操作，这样可以在很大程度上减轻数据库的负担。通常会将 SQL 语句 md5() 之后的值作为唯一标识符 key。下边是一个利用 memcached 来缓存数据库查询结果集的示例（此代码片段紧接上边的示例代码）：</p>
<pre class="code" style="padding-bottom: 20px; width: 500px;"><code><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php
$sql </span><span style="color: #007700;">= </span><span style="color: #dd0000;">‘SELECT * FROM users’</span><span style="color: #007700;">; </span><span style="color: #0000bb;">
$key </span><span style="color: #007700;">= </span><span style="color: #0000bb;">md5</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">);   </span><span style="color: #ff8000;">
//memcached 对象标识符
</span><span style="color: #007700;">if ( !(</span><span style="color: #0000bb;">$datas </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$mc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">get</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">)) ) {
     </span><span style="color: #ff8000;">//   在 memcached 中未获取到缓存数据，则使用数据库查询获取记录集。
</span><span style="color: #007700;"><span class="code" style="padding-bottom: 20px; width: 500px;"><code><span style="color: #ff8000;">     </span></code></span>echo </span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">.</span><span style="color: #0000bb;">str_pad</span><span style="color: #007700;">(</span><span style="color: #dd0000;">‘Read datas from MySQL.’</span><span style="color: #007700;">, </span><span style="color: #0000bb;">60</span><span style="color: #007700;">, </span><span style="color: #dd0000;">‘_’</span><span style="color: #007700;">).</span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">;
     </span><span style="color: #0000bb;">$conn </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">‘localhost’</span><span style="color: #007700;">, </span><span style="color: #dd0000;">‘test’</span><span style="color: #007700;">, </span><span style="color: #dd0000;">‘test’</span><span style="color: #007700;">);
     </span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">‘test’</span><span style="color: #007700;">);
     </span><span style="color: #0000bb;">$result </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">);
      while (</span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_fetch_object</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$result</span><span style="color: #007700;">))
         </span><span style="color: #0000bb;">$datas</span><span style="color: #007700;">[] = </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">;</span></span><span style="color: #000000;"><span style="color: #007700;">     </span><span style="color: #ff8000;">//   将数据库中获取到的结果集数据保存到 memcached 中，以供下次访问时使用。
     </span><span style="color: #0000bb;">$mc</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">add</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$key</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$datas</span><span style="color: #007700;">);
 } else {
      echo </span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">.</span><span style="color: #0000bb;">str_pad</span><span style="color: #007700;">(</span><span style="color: #dd0000;">‘Read datas from memcached.’</span><span style="color: #007700;">, </span><span style="color: #0000bb;">60</span><span style="color: #007700;">, </span><span style="color: #dd0000;">‘_’</span><span style="color: #007700;">).</span><span style="color: #dd0000;">“n”</span><span style="color: #007700;">;
}
</span><span style="color: #0000bb;">var_dump</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$datas</span><span style="color: #007700;">);
 </span><span style="color: #0000bb;">?&gt;</span> </span> </code></pre>
<p>可以看出，使用 memcached 之后，可以减少数据库连接、查询操作，数据库负载下来了，脚本的运行速度也提高了。</p>
<p>之前我曾经写过一篇名为<a href="http://www.infor96.com/~nio/sharing-php-session-data-between-servers/"><span style="color: #333333;">《PHP 实现多服务器共享 SESSION 数据》</span></a>文章，文中的 SESSION 是使用数据库保存的，在并发访问量大的时候，服务器的负载会很大，经常会超出 MySQL 最大连接数，利用 memcached，我们可以很好地解决这个问题，工作原理如下：</p>
<ul>
<li>用户访问网页时，查看 memcached 中是否有当前用户的 SESSION 数据，使用 session_id() 作为唯一标识符；如果数据存在，则直接返回，如果不存在，再进行数据库连接，获取 SESSION 数据，并将此数据保存到 memcached 中，供下次使用；</li>
<li>当前的 PHP 运行结束（或使用了 <a href="http://php.liukang.com/manual/en/function.session-write-close.php"><span style="color: #333333;">session_write_close()</span></a>）时，会调用 My_Sess::write() 方法，将数据写入数据库，这样的话，每次仍然会有数据库操作，对于这个方法，也需要进行优化。使用一个全局变量，记录用户进入页面时的 SESSION 数据，然后在 write() 方法内比较此数据与想要写入的 SESSION 数据是否相同，不同才进行数据库连接、写入数据库，同时将 memcached 中对应的对象删除，如果相同的话，则表示 SESSION 数据未改变，那么就可以不做任何操作，直接返回了；</li>
<li>那么用户 SESSION 过期时间怎么解决呢？记得 memcached 的 add() 方法有个过期时间参数 $exp 吗？把这个参数值设置成小于 SESSION 最大存活时间即可。另外别忘了给那些一直在线的用户延续 SESSION 时长，这个可以在 write() 方法中解决，通过判断时间，符合条件则更新数据库数据。</li>
</ul>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="[缓存加速]eAccelerator与memcached的区别与用途" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">[缓存加速]eAccelerator与memcached的区别与用途</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="利用memcached构建高性能的Web应用程序" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/03/22/4026094.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">利用memcached构建高性能的Web应用程序</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached for win32" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F408&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached for win32</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached安装+php使用手记" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F364">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached安装+php使用手记</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/364/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>清除缓存，php,html,jsp,asp中防止模式窗口页面不更新的情况</title>
		<link>http://blog.haohtml.com/archives/279</link>
		<comments>http://blog.haohtml.com/archives/279#comments</comments>
		<pubDate>Mon, 10 Mar 2008 10:07:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[缓存]]></category>

		<guid isPermaLink="false">http://haohtml.com/wp/?p=279</guid>
		<description><![CDATA[清除缓存，php,html,jsp,asp中防止页面不更新的情况
Code:

HTML
<META HTTP-EQUIV="pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<META HTTP-EQUIV="expires" CONTENT="Mon, 23 Jan 1978 20:52:30 GMT">
ASP
<%
Response.Expires = -1
Response.ExpiresAbsolute = Now() - 1
Response.cachecontrol = "no-cache"
%>
PHP

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="smarty局部缓存" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F279">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">smarty局部缓存</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="搭建一个大型网站架构的实验环境(Squid缓存服务器篇)" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F524&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F279">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/04/14/5592016.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">搭建一个大型网站架构的实验环境(Squid缓存服务器篇)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="[缓存加速]eAccelerator与memcached的区别与用途" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F279">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">[缓存加速]eAccelerator与memcached的区别与用途</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached安装+php使用手记" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F279">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached安装+php使用手记</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></description>
			<content:encoded><![CDATA[<p>清除缓存，php,html,jsp,asp中防止页面不更新的情况<br />
Code:</p>
<div class="code">HTML<br />
&lt;META HTTP-EQUIV="pragma" CONTENT="no-cache"&gt;<br />
&lt;META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"&gt;<br />
&lt;META HTTP-EQUIV="expires" CONTENT="Mon, 23 Jan 1978 20:52:30 GMT"&gt;</div>
<div class="code">ASP</div>
<div class="code">&lt;%<br />
Response.Expires = -1<br />
Response.ExpiresAbsolute = Now() - 1<br />
Response.cachecontrol = "no-cache"<br />
%&gt;</div>
<p>PHP</p>
<div class="code">header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");<br />
header("Cache-Control: no-cache, must-revalidate");<br />
header("Pragma: no-cache");</div>
<p>JSP</p>
<div class="code">response.setHeader("Pragma","No-Cache");<br />
response.setHeader("Cache-Control","No-Cache");<br />
response.setDateHeader("Expires", 0);</div>
<table class="wumii-related-items" cellspacing="0" cellpadding="3" border="0"  style="clear: both;">
    
    <tr>
        <td colspan="4"><b><font size="-1"  style="display: block !important; padding: 20px 0 5px !important;">您可能也喜欢：</font></b></td>
    </tr>
    
        <tr>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important;">
                    <a target="_blank" title="smarty局部缓存" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F2777&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F279">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">smarty局部缓存</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="搭建一个大型网站架构的实验环境(Squid缓存服务器篇)" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F524&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F279">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/site_images/2011/04/14/5592016.jpg" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">搭建一个大型网站架构的实验环境(Squid缓存服务器篇)</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="[缓存加速]eAccelerator与memcached的区别与用途" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F399&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F279">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">[缓存加速]eAccelerator与memcached的区别与用途</font>
                    </a>
                </td>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="memcached安装+php使用手记" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect.htm?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F279">
                        <img style="margin: 0 !important; padding: 2px !important; border: 1px solid #DDDDDD !important; width: 96px !important; height: 96px !important;" src="http://static.wumii.com/images/blogWidget/wordpress_default.gif" width="96px" height="96px" /><br />
                        <font size="-1" color="#333333" style="display: block !important; line-height: 15px !important; width: 102px !important; font: 12px/15px arial !important; height: 60px !important; margin: 3px 0 0 0 !important; padding: 0 !important; overflow: hidden !important;">memcached安装+php使用手记</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems.htm" target="_blank" title="无觅相关文章插件">
                <font size="-1" color="#bbbbbb" style="display: block !important; font-family: arial !important; padding: 5px 0 !important; font-size: 12px !important; color: #bbb !important;">无觅</font>
            </a>
        </td>
    </tr>
</table>]]></content:encoded>
			<wfw:commentRss>http://blog.haohtml.com/archives/279/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

