<?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; memcache</title>
	<atom:link href="http://blog.haohtml.com/tag/memcache/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.haohtml.com</link>
	<description>haohtml&#039;s life and works</description>
	<lastBuildDate>Tue, 22 May 2012 04:57:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>php客户端memcached的安装教程</title>
		<link>http://blog.haohtml.com/archives/11873</link>
		<comments>http://blog.haohtml.com/archives/11873#comments</comments>
		<pubDate>Thu, 27 Oct 2011 02:10:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[memcached]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=11873</guid>
		<description><![CDATA[我们在上篇文章里（http://blog.haohtml.com/archives/6051）介绍了LNMP的安装方法．只安装了memcache客户端．有些用户可能需要memcached这种客户端的．这里介绍一种php客户端memcached的安装方法． 之前在安装memcache时有提到memcached客户端是叫memcache，其实还有一个基于libmemcached的客户端叫memcached，据说性能更好，功能也更多。参考：http://blog.haohtml.com/archives/392 memcache的官方主页：http://pecl.php.net/package/memcache memcached的官方主页：http://pecl.php.net/package/memcached 以下是我安装Memcached版本的PHP模块的过程记录： #wget http://download.tangent.org/libmemcached-0.9.tar.gz #tar zxf libmemcached-0.9.tar.gz #cd libmemcached-0.9 #./configure --prefix=/usr/local/libmemcached --with-memcached #make #make install #wget http://pecl.php.net/get/memcached-1.0.2.tgz #tar zxf memcached-1.0.2.tgz #cd memcached-1.0.2 #/usr/local/php/bin/phpize #./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --prefix=/usr/local/phpmemcached #make #make install 在php.ini中加入 extension=memcached.so 完成 另：在安装libmemcached时，如果只用./configure，可能会提示： checking for memcached… no configure: error: “could not find memcached binary” 两者使用起来几乎一模一样。 $mem = new Memcache; $mem-&#62;addServer($memcachehost, '11211'); [...]<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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11873">
                        <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="Windows下的Memcache安装" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F402&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11873">
                        <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;">Windows下的Memcache安装</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="windows环境下memcache服务器使用经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11873">
                        <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;">windows环境下memcache服务器使用经验</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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11873">
                        <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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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>我们在上篇文章里（<a href="http://blog.haohtml.com/archives/6051">http://blog.haohtml.com/archives/6051</a>）介绍了LNMP的安装方法．只安装了memcache客户端．有些用户可能需要memcached这种客户端的．这里介绍一种php客户端memcached的安装方法．</p>
<p>之前在安装memcache时有提到memcached客户端是叫memcache，其实还有一个基于libmemcached的客户端叫memcached，据说性能更好，功能也更多。参考：<a href="http://blog.haohtml.com/archives/392">http://blog.haohtml.com/archives/392</a></p>
<p>memcache的官方主页：<a href="http://pecl.php.net/package/memcache" target="_blank">http://pecl.php.net/package/memcache</a><br />
memcached的官方主页：<a href="http://pecl.php.net/package/memcached" target="_blank">http://pecl.php.net/package/memcached</a></p>
<p>以下是我安装Memcached版本的PHP模块的过程记录：<span id="more-11873"></span></p>
<pre class="brush:[bash]">#wget http://download.tangent.org/libmemcached-0.9.tar.gz
#tar zxf libmemcached-0.9.tar.gz
#cd libmemcached-0.9
#./configure --prefix=/usr/local/libmemcached --with-memcached
#make
#make install

#wget http://pecl.php.net/get/memcached-1.0.2.tgz
#tar zxf memcached-1.0.2.tgz
#cd memcached-1.0.2
#/usr/local/php/bin/phpize
#./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --prefix=/usr/local/phpmemcached
#make
#make install</pre>
<p>在php.ini中加入</p>
<blockquote><p>extension=memcached.so</p></blockquote>
<p>完成<a name="entrymore"></a></p>
<p>另：在安装libmemcached时，如果只用./configure，可能会提示：</p>
<blockquote><p>checking for memcached… no<br />
configure: error: “could not find memcached binary”</p></blockquote>
<p>两者使用起来几乎一模一样。</p>
<pre class="brush:[php]">$mem = new Memcache;
$mem-&gt;addServer($memcachehost, '11211');
$mem-&gt;addServer($memcachehost, '11212');
$mem-&gt;set('hx','9enjoy');
echo $mem-&gt;get('hx');</pre>
<p>&nbsp;</p>
<pre class="brush:[php]">$md = new Memcached;
$servers = array(
array($memcachehost, '11211'),
array($memcachehost, '11212')
);
$md-&gt;addServers($servers);
$md-&gt;set('hx','9enjoy');
echo $md-&gt;get('hx');</pre>
<p>memcached的方法比memcache多不少，比如getMulti，getByKey，addServers等。<br />
memcached没有memcache的connect方法，目前也还不支持长连接。<br />
memcached 支持 Binary Protocol，而 memcache 不支持，意味着 memcached 会有更高的性能。<br />
Memcache是原生实现的，支持OO和非OO两套接口并存，memcached是使用libmemcached，只支持OO接口。<br />
更详细的区别：<a href="http://code.google.com/p/memcached/wiki/PHPClientComparison" target="_blank">http://code.google.com/p/memcached/wiki/PHPClientComparison</a></p>
<p>=================================<br />
memcached服务端是集中式的缓存系统，分布式实现方法是由客户端决定的。<br />
memcached的分布算法一般有两种选择：<br />
1、根据hash(key)的结果，模连接数的余数决定存储到哪个节点，也就是hash(key)% sessions.size()，这个算法简单快速，表现良好。然而这个算法有个缺点，就是在memcached节点增加或者删除的时候，原有的缓存数据将大规模失效，命中率大受影响，如果节点数多，缓存数据多，重建缓存的代价太高，因此有了第二个算法。<br />
2、Consistent Hashing，一致性哈希算法，他的查找节点过程如下：<br />
首先求出memcached服务器（节点）的哈希值，并将其配置到0～232的圆（continuum）上。然后用同样的方法求出存储数据的键的哈希值，并映射到圆上。然后从数据映射到的位置开始顺时针查找，将数据保存到找到的第一个服务器上。如果超过2的32次方后仍然找不到服务器，就会保存到第一台memcached服务器上。</p>
<p>memcache在没有任何配置的情况下，是使用第一种方法。memcached要实现第一种方法，似乎是使用(未确认)：<br />
$md-&gt;setOption(Memcached::OPT_HASH, Memcached::HASH_CRC);</p>
<p>第二种一致性哈希算法：</p>
<p>memcache在php.ini中加</p>
<div>Memcache.hash_strategy =consistent<br />
Memcache.hash_function =crc32</div>
<p>memcached在程序中加(未确认)</p>
<div>$md-&gt;setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);<br />
$md-&gt;setOption(Memcached::OPT_HASH, Memcached::HASH_CRC);<br />
或<br />
$mem-&gt;setOption(Memcached::OPT_DISTRIBUTION,Memcached::DISTRIBUTION_CONSISTENT);<br />
$mem-&gt;setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE,true);</div>
<p>一些参考文档:<br />
memcached分布测试报告（一致性哈希情况下的散列函数选择）：<a href="http://www.iteye.com/topic/346682" target="_blank">http://www.iteye.com/topic/346682</a><br />
php模块memcache和memcached区别：<a href="http://hi.baidu.com/dong_love_yan/blog/item/afbe1e12d22e7512203f2e21.html" target="_blank">http://hi.baidu.com/dong_love_yan/blog/item/afbe1e12d22e7512203f2e21.html</a><br />
PHP模块：Memcached &gt; Memcache：<a href="http://hi.baidu.com/thinkinginlamp/blog/item/717cd42a11f6e491023bf67a.html" target="_blank">http://hi.baidu.com/thinkinginlamp/blog/item/717cd42a11f6e491023bf67a.html</a></p>
<p>20110509@@UPDATE：<br />
如果安装libmemcached有如下出错提示：<br />
make[2]: *** [clients/ms_conn.o] Error 1<br />
make[2]: Leaving directory `/www/soft/libmemcached-0.48'<br />
make[1]: *** [all-recursive] Error 1<br />
make[1]: Leaving directory `/www/soft/libmemcached-0.48'<br />
make: *** [all] Error 2</p>
<p>可在configure时增加--disable-64bit CFLAGS="-O3 -march=i686"<br />
即：./configure --prefix=/usr/local/libmemcached --with-memcached --disable-64bit CFLAGS="-O3 -march=i686"</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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11873">
                        <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="Windows下的Memcache安装" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F402&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11873">
                        <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;">Windows下的Memcache安装</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="windows环境下memcache服务器使用经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11873">
                        <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;">windows环境下memcache服务器使用经验</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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F374&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11873">
                        <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>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/11873/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>遍历memcache中的key和value</title>
		<link>http://blog.haohtml.com/archives/11795</link>
		<comments>http://blog.haohtml.com/archives/11795#comments</comments>
		<pubDate>Wed, 19 Oct 2011 05:51:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[nosql]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=11795</guid>
		<description><![CDATA[什么是memcache memcache是一个高性能的分布式的内存对象缓存系统，通过在内存里维护一个统一的巨大的hash表，它能够用来存储各种格式的数据，包括图像、视频、文件以及数据库检索的结果等。Memcache是danga.com的一个项目，最早是为 LiveJournal 服务的，最初为了加速 LiveJournal 访问速度而开发的，后来被很多大型的网站采用。目前全世界不少人使用这个缓存项目来构建自己大负载的网站，来分担数据库的压力。 为什么要遍历 目前，用到memcache的公司和网站也越来越多。Memcache的客户端操作一般都只提供了get,set等简单的操作，这些操作都是非常高效的。   虽然memcache是个key-value存储的系统，但是在某些时候，我们可能需要遍历memcache的数据。 通过使用memcache 内置方法Memcache::getExtendedStats，首先获得items信息。 最后得到的解决类似与 &#60;php $memcache = new Memcache(); $all_items = $memcache-&#62;getExtendedStats('items'); var_export($all_items); ?&#62; 其中$all_items中的key“192.168.0.110:11211” 就是memcache的host和port。 如何遍历memcache  stats命令 memcache的stats命令包括： 1.        stats 2.        stats reset 3.        stats malloc 4.        stats maps 5.        stats sizes 6.        stats slabs 7.        stats items 8.        stats cachedump slab_id limit_num 9.        stats detail [on&#124;off&#124;dump] 通过命令完成遍历 通过这些stats命令我们就可以完成memcache存储的内容的遍历，OK，下面我们通过telnet直接连接到memcache通过这些命令来完成相关的操作。 telnet到192.168.15.225（局域网测试机器）的memcache服务器 执行stats items命令，可以看到出现 很多的items行。 执行stats cachedump 3 0命令。这里的3表示上面图中items后面的数字，0标示显示全部的数据，如果是1就标示只显示1条。 [...]<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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11795">
                        <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="Memcache的安全性" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8085&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11795">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11795">
                        <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 memcache手册</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的addServer的故障转移机制" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11795">
                        <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的addServer的故障转移机制</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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>
<p><strong>什么是</strong><strong>memcache</strong></p>
<p>memcache是一个高性能的分布式的内存对象缓存系统，通过在内存里维护一个统一的巨大的hash表，它能够用来存储各种格式的数据，包括图像、视频、文件以及数据库检索的结果等。Memcache是danga.com的一个项目，最早是为 LiveJournal 服务的，最初为了加速 LiveJournal 访问速度而开发的，后来被很多大型的网站采用。目前全世界不少人使用这个缓存项目来构建自己大负载的网站，来分担数据库的压力。</p>
<p><strong>为什么要遍历</strong><strong></strong></p>
<p>目前，用到memcache的公司和网站也越来越多。Memcache的客户端操作一般都只提供了get,set等简单的操作，这些操作都是非常高效的。   虽然memcache是个key-value存储的系统，但是在某些时候，我们可能需要遍历memcache的数据。<br />
通过使用memcache 内置方法Memcache::getExtendedStats，首先获得items信息。</p>
<p>最后得到的解决类似与</p>
<pre class="brush:[php]">&lt;php
$memcache = new Memcache();
$all_items = $memcache-&gt;getExtendedStats('items');
var_export($all_items);
?&gt;</pre>
<p>其中$all_items中的key“192.168.0.110:11211” 就是memcache的host和port。<br />
<span id="more-11795"></span></p>
<p><strong>如何遍历</strong><strong>memcache</strong></p>
<p><strong> stats</strong><strong>命令</strong><strong></strong></p>
<p>memcache的stats命令包括：</p>
<p>1.        stats</p>
<p>2.        stats reset</p>
<p>3.        stats malloc</p>
<p>4.        stats maps</p>
<p>5.        stats sizes</p>
<p>6.        stats slabs</p>
<p>7.        stats items</p>
<p>8.        stats cachedump slab_id limit_num</p>
<p>9.        stats detail [on|off|dump]</p>
<p><strong>通过命令完成遍历</strong><strong></strong></p>
<p>通过这些stats命令我们就可以完成memcache存储的内容的遍历，OK，下面我们通过telnet直接连接到memcache通过这些命令来完成相关的操作。<br />
telnet到192.168.15.225（局域网测试机器）的memcache服务器<br />
执行stats items命令，可以看到出现 很多的items行。</p>
<p>执行stats cachedump 3 0命令。这里的3表示上面图中items后面的数字，0标示显示全部的数据，如果是1就标示只显示1条。<br />
下图为执行后的结果，item后面的字符串为key<br />
通过上面列出的key我们就可以遍历所有的数据了，下面我们取出某一条数据，key为Uc!uLh的数据。<br />
到这里，你也许明白了怎么去遍历memcache的数据了。</p>
<p><strong>代码实现</strong><strong></strong></p>
<p>下面贴上一段php实现的遍历memcache数据的代码，其他语言可以参考代码自己实现。</p>
<pre class="bursh:[php]">&lt;?php

$host='225';
$port=11211;

$mem=new Memcache();
$mem-&gt;connect($host,$port);

$items=$mem-&gt;getExtendedStats ('items');
$items=$items["$host:$port"]['items'];
for($i=0,$len=count($items);$i&lt;$len;$i++){

$number=$items[$i]['number'];

 $str=$mem-&gt;getExtendedStats ("cachedump",$number,0);

 $line=$str["$host:$port"];

 if( is_array($line) &amp;&amp; count($line)&gt;0){

     foreach($line as $key=&gt;$value){

         echo $key . '=&gt;';

         print_r($mem-&gt;get($key));

         echo "\r\n";

     }

 }

}

?&gt;</pre>
<p><strong>扩展功能</strong><strong></strong></p>
<p>由此可以实现查找memcache某个前缀的key的数据，或者查询某些value的key。甚至实现数据库的like功能。请注意：遍历memcache的操作并没有memcache的get操作那么高效。</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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11795">
                        <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="Memcache的安全性" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8085&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11795">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11795">
                        <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 memcache手册</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的addServer的故障转移机制" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11795">
                        <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的addServer的故障转移机制</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/11795/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>memcache的addServer的故障转移机制</title>
		<link>http://blog.haohtml.com/archives/11782</link>
		<comments>http://blog.haohtml.com/archives/11782#comments</comments>
		<pubDate>Wed, 19 Oct 2011 04:47:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[系统架构]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=11782</guid>
		<description><![CDATA[如果有多台memcached服务器端（当然其他支持memcache协议的服务前端都可以，比如Tokyo Tyrant），最好使用$memcache-&#62;addServer 来连接服务前端。 连接示例： &#60;?php $mem_conf = array( array('host'=&#62;'192.168.0.11', 'port'=&#62;'11211'), array('host'=&#62;'192.168.0.12', 'port'=&#62;'11211'), array('host'=&#62;'192.168.0.13', 'port'=&#62;'11211') ); $memcache = new Memcache ( ); foreach ( $mem_conf as $v ) { $memcache-&#62;addServer ( $v ['host'], $v ['port'] ); } ?&#62; 使用$memcache-&#62;addServer 而不是 $memcache-&#62;connect 去连接 Memcached 服务器，是因为当 Memcache 客户端使用 addServer 服务器池时，是根据“crc32(key) % current_server_num”哈希算法将 key 哈希到不同的服务器的，PHP、C 和 python 的客户端都是如此的算法。 如: &#60;?php [...]<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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782">
                        <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="Memcache mutex设计模式" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F6772&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782">
                        <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 mutex设计模式</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 VS Memcached VS MemcacheDB" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8089&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782">
                        <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 VS Memcached VS MemcacheDB</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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F370&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782">
                        <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" 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><span style="color: #0000ff;">如果有多台memcached服务器端（当然其他支持memcache协议的服务前端都可以，比如Tokyo Tyrant），最好使用$memcache-&gt;addServer 来连接服务前端。</span></p>
<p><strong>连接示例：</strong></p>
<pre class="brush:[php]">&lt;?php
$mem_conf = array(
    array('host'=&gt;'192.168.0.11', 'port'=&gt;'11211'),
    array('host'=&gt;'192.168.0.12', 'port'=&gt;'11211'),
    array('host'=&gt;'192.168.0.13', 'port'=&gt;'11211')
);

$memcache = new Memcache ( );
foreach ( $mem_conf as $v ) {
    $memcache-&gt;addServer ( $v ['host'], $v ['port'] );
}
?&gt;</pre>
<p>使用$memcache-&gt;addServer 而不是 $memcache-&gt;connect 去连接 Memcached 服务器，是因为当 Memcache 客户端使用 addServer 服务器池时，是根据“<span style="color: #0000ff;">crc32(key) % current_server_num</span>”哈希算法将 key 哈希到不同的服务器的，PHP、C 和 python 的客户端都是如此的算法。</p>
<p>如:</p>
<pre class="brush:[php]">&lt;?php
$mem = new Memcache;
$mem-&gt;addServer('192.168.0.71', 11211);
$mem-&gt;addServer('192.168.0.72', 11211);
$mem-&gt;addServer('192.168.0.73', 11211);
for($i=0; $i &lt; 100; $i++)
	$mem-&gt;set('key'.$i, md5($i).'This is a memcached test!', 0, 60);

?&gt;</pre>
<p>然后我们通过<a href="http://www.junopen.com/memadmin" target="_blank">memadmin</a>工具可以看到上面的key被分布存储在这三台Memcached上了(根据算法自动计算).</p>
<p>Memcache 客户端的 addserver 具有故障转移机制，当 addserver 了2台 Memcached 服务器，而其中1台宕机了，那么 current_server_num 会由原先的2变成1。(2011-10-11个人测试的时候发现无法实现此功能,第二台Memcached不可用的时候,会造成正常要保存到这台server的数据会丢失的现象)</p>
<p>引用 memcached 官方网站和 PHP 手册中的两段话：<br />
引用<br />
<a href="http://www.danga.com/memcached/">http://www.danga.com/memcached/</a><br />
If a host goes down, the API re-maps that dead host's requests onto the servers that are available.</p>
<p><a href="http://cn.php.net/manual/zh/function.Memcache-addServer.php">http://cn.php.net/manual/zh/function.Memcache-addServer.php</a><br />
Failover may occur at any stage in any of the methods, as long as other servers are available the request the user won't notice. Any kind of socket or Memcached server level errors (except out-of-memory) may trigger the failover. Normal client errors such as adding an existing key will not trigger a failover.</p>
<p><strong>二、repcached实现memcached的复制功能</strong></p>
<p>repcached是日本人开发的实现memcached复制功能，它是一个单 master单 slave的方案，但它的 master/slave都是可读写的，而且可以相互同步，如果 master坏掉， slave侦测到连接断了，它会自动 listen而成为 master；而如果 slave坏掉， master也会侦测到连接断，它就会重新 listen等待新的 slave加入</p>
<p><span style="color: #0000ff;">安装：先安装memcached(我安装的1.2.8)</span></p>
<p>有两种方式：</p>
<p>方式一、下载对应的repcached版本</p>
<blockquote><p>#wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz<br />
#tar zxf memcached-1.2.8-repcached-2.2.tar.gz<br />
#cd memcached-1.2.8-repcached-2.2</p></blockquote>
<p>方式二、下载对应patch版本</p>
<blockquote><p>#wget http://downloads.sourceforge.net/repcached/repcached-2.2-1.2.8.patch.gz<br />
#gzip -cd ../repcached-2.2-1.2.8.patch.gz | patch -p1<br />
#./configure --enable-replication<br />
# make<br />
# make install</p></blockquote>
<p>启动：</p>
<blockquote><p>启动master</p>
<p>#<span style="color: #0000ff;">memcached -v -l 192.168.50.240 -p 11211 -u root</span><br />
replication: listen (master监听)启动salve<br />
#<span style="color: #0000ff;">memcached -v -l 192.168.50.241 -p 11213 -u root -x 127.0.0.1 -X 11212</span><br />
replication: connect (peer=192.168.50.240:11212)<br />
replication: marugoto copying<br />
replication: start启动正常后，master将accept。</p></blockquote>
<p>测试：</p>
<blockquote><p>操作master</p>
<p>#telnet 192.168.50.240 11211<br />
#set key1 0 0 3</p>
<p>111查看slave</p>
<p>#telnet 192.168.50.241 11213<br />
#get key1如果正常表示，配置成功应用：</p></blockquote>
<p>可以实现cache冗余注意：如果master down机，slave接管并成为master，这时down机的master只能启用slave,他们之间互换角色，才能保持复制功能。换句话说，master没有抢占功能。</p>
<p><strong>相关教程:</strong></p>
<p>Memcache基础教程:<a href="http://blog.haohtml.com/archives/8986">http://blog.haohtml.com/archives/8986</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="memcached完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782">
                        <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="Memcache mutex设计模式" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F6772&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782">
                        <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 mutex设计模式</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 VS Memcached VS MemcacheDB" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8089&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782">
                        <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 VS Memcached VS MemcacheDB</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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F370&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F11782">
                        <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" 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/11782/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux下的Memcache安装</title>
		<link>http://blog.haohtml.com/archives/9841</link>
		<comments>http://blog.haohtml.com/archives/9841#comments</comments>
		<pubDate>Wed, 15 Jun 2011 08:31:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[服务器类]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=9841</guid>
		<description><![CDATA[服务器端主要是安装memcache服务器端，目前的最新版本是 memcached-1.3.0 。 下载：http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz 另外，Memcache用到了libevent这个库用于Socket的处理，所以还需要安装libevent，libevent的最新版本是libevent-1.3。（如果你的系统已经安装了libevent，可以不用安装） 官网：http://www.monkey.org/~provos/libevent/ 下载：http://www.monkey.org/~provos/libevent-1.3.tar.gz 用wget指令直接下载这两个东西.下载回源文件后。 1.先安装libevent。这个东西在配置时需要指定一个安装路径，即./configure –prefix=/usr；然后make；然后make install； 2.再安装memcached，只是需要在配置时需要指定libevent的安装路径即./configure –with-libevent=/usr；然后make；然后make install； 这样就完成了Linux下Memcache服务器端的安装。 详细的方法如下： 1.分别把memcached和libevent下载回来，放到 /tmp 目录下： # cd /tmp # wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz # wget http://www.monkey.org/~provos/libevent-1.2.tar.gz 2.先安装libevent： # tar zxvf libevent-1.2.tar.gz # cd libevent-1.2 # ./configure --prefix=/usr # make # make install 3.测试libevent是否安装成功： # ls -al /usr/lib &#124; grep libevent lrwxrwxrwx 1 root [...]<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="windows下memcached的安装（转载）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F406&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F9841">
                        <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;">windows下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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F9841">
                        <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="memcache简要安装步骤" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F370&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F9841">
                        <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>
                <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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3955&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F9841">
                        <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" 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>服务器端主要是安装memcache服务器端，目前的最新版本是 memcached-1.3.0 。<br />
下载：http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz<br />
另外，Memcache用到了libevent这个库用于Socket的处理，所以还需要安装libevent，libevent的最新版本是libevent-1.3。（如果你的系统已经安装了libevent，可以不用安装）<br />
官网：<a href="http://www.monkey.org/~provos/libevent/" target="_blank">http://www.monkey.org/~provos/libevent/</a><br />
下载：<a href="http://www.monkey.org/~provos/libevent-1.3.tar.gz" target="_blank">http://www.monkey.org/~provos/libevent-1.3.tar.gz</a></p>
<p>用wget指令直接下载这两个东西.下载回源文件后。<br />
1.先安装libevent。这个东西在配置时需要指定一个安装路径，即./configure –prefix=/usr；然后make；然后make install；</p>
<p>2.再安装memcached，只是需要在配置时需要指定libevent的安装路径即./configure –with-libevent=/usr；然后make；然后make install；</p>
<p>这样就完成了Linux下Memcache服务器端的安装。<span id="more-9841"></span></p>
<p>详细的方法如下：</p>
<blockquote><p><span style="text-decoration: underline;">1.分别把memcached和libevent下载回来，放到 /tmp 目录下：</span><br />
# cd /tmp<br />
# wget <a href="http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz" target="_blank">http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz</a><br />
# wget <a href="http://www.monkey.org/~provos/libevent-1.2.tar.gz" target="_blank">http://www.monkey.org/~provos/libevent-1.2.tar.gz</a></p>
<p><span style="text-decoration: underline;">2.先安装libevent：</span><br />
# tar zxvf libevent-1.2.tar.gz<br />
# cd libevent-1.2<br />
# ./configure --prefix=/usr<br />
# make<br />
# make install</p>
<p><span style="text-decoration: underline;">3.测试libevent是否安装成功：</span><br />
# ls -al /usr/lib | grep libevent<br />
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -&gt; libevent-1.2.so.1.0.3<br />
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3<br />
-rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a<br />
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la<br />
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -&gt; libevent-1.2.so.1.0.3<br />
还不错，都安装上了。</p>
<p><span style="text-decoration: underline;">4.安装memcached，同时需要安装中指定libevent的安装位置：</span><br />
# cd /tmp<br />
# tar zxvf memcached-1.2.0.tar.gz<br />
# cd memcached-1.2.0<br />
# ./configure --with-libevent=/usr<br />
# make<br />
# make install<br />
如果中间出现报错，请仔细检查错误信息，按照错误信息来配置或者增加相应的库或者路径。<br />
安装完成后会把memcached放到 /usr/local/bin/memcached ，</p>
<p><span style="text-decoration: underline;">5.测试是否成功安装memcached：</span><br />
# ls -al /usr/local/bin/mem*<br />
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached<br />
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug</p></blockquote>
<p><strong>安装Memcache的PHP扩展</strong><br />
1.在<a href="http://pecl.php.net/package/memcache" target="_blank">http://pecl.php.net/package/memcache</a> 选择相应想要下载的memcache版本。<br />
2.安装PHP的memcache扩展</p>
<blockquote><p>tar vxzf memcache-2.2.1.tgz<br />
cd memcache-2.2.1<br />
/usr/local/php/bin/phpize<br />
./configure –enable-memcache –with-php-config=/usr/local/php/bin/php-config –with-zlib-dir<br />
make<br />
make install</p></blockquote>
<p>3.上述安装完后会有类似这样的提示：</p>
<blockquote><p>Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/</p></blockquote>
<p>4.把php.ini中的extension_dir = “./”修改为</p>
<blockquote><p>extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/”</p></blockquote>
<p>5.添加一行来载入memcache扩展：extension=memcache.so</p>
<p><strong>memcached的基本设置</strong>：<br />
1.启动Memcache的服务器端：<br />
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 12000 -c 256 -P /tmp/memcached.pid</p>
<blockquote><p>-d选项是启动一个守护进程，<br />
-m是分配给Memcache使用的内存数量，单位是MB，我这里是10MB，<br />
-u是运行Memcache的用户，我这里是root，<br />
-l是监听的服务器IP地址，如果有多个地址的话，我这里指定了服务器的IP地址192.168.0.200，<br />
-p是设置Memcache监听的端口，我这里设置了12000，最好是1024以上的端口，<br />
-c选项是最大运行的并发连接数，默认是1024，我这里设置了256，按照你服务器的负载量来设定，<br />
-P是设置保存Memcache的pid文件，我这里是保存在 /tmp/memcached.pid，</p></blockquote>
<p>2.如果要结束Memcache进程，执行：</p>
<blockquote><p># kill `cat /tmp/memcached.pid`</p></blockquote>
<p>也可以启动多个守护进程，不过端口不能重复。</p>
<p>3.重启apache</p>
<blockquote><p>service httpd restart</p></blockquote>
<p><strong>Memcache环境测试</strong>：<br />
运行下面的php文件，如果有输出This is a test!，就表示环境搭建成功。开始领略Memcache的魅力把！</p>
<blockquote><p>&lt;?php<br />
$mem = new Memcache;<br />
$mem-&gt;connect(“127.0.0.1″, 11211);<br />
$mem-&gt;set(‘key’, ‘This is a test!’, 0, 60);<br />
$val = $mem-&gt;get(‘key’);<br />
echo $val;<br />
?&gt;</p></blockquote>
<p><strong>参考资料</strong>：<br />
对Memcached有疑问的朋友可以参考下列文章：<br />
<a href="http://www.ccvita.com/257.html">Linux下的Memcache安装</a>：http://www.ccvita.com/257.html<br />
<a href="http://www.ccvita.com/258.html">Windows下的Memcache安装</a>：http://www.ccvita.com/258.html<br />
<a href="http://www.ccvita.com/259.html">Memcache基础教程</a>：http://www.ccvita.com/259.html<br />
<a href="http://www.ccvita.com/261.html">Discuz!的Memcache缓存实现</a>：http://www.ccvita.com/261.html<br />
<a href="http://www.ccvita.com/306.html">Memcache协议中文版</a>：http://www.ccvita.com/306.html<br />
<a href="http://www.ccvita.com/395.html">Memcache分布式部署方案</a>：http://www.ccvita.com/395.html</p>
<p>来源:<a href="http://www.ccvita.com/257.html">http://www.ccvita.com/257.html</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="windows下memcached的安装（转载）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F406&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F9841">
                        <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;">windows下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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F9841">
                        <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="memcache简要安装步骤" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F370&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F9841">
                        <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>
                <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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3955&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F9841">
                        <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" 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/9841/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memcache基础教程</title>
		<link>http://blog.haohtml.com/archives/8986</link>
		<comments>http://blog.haohtml.com/archives/8986#comments</comments>
		<pubDate>Tue, 05 Apr 2011 04:32:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[服务器类]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=8986</guid>
		<description><![CDATA[Memcache是什么 Memcache是danga.com的一个项目，最早是为 LiveJournal 服务的，目前全世界不少人使用这个缓存项目来构建自己大负载的网站，来分担数据库的压力。 它可以应对任意多个连接，使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间，然后建立一个HashTable，Memcached自管理这些HashTable。 Memcache官方网站：http://www.danga.com/memcached，更多详细的信息可以来这里了解 . 为什么会有Memcache和memcached两种名称？ 其实Memcache是这个项目的名称，而memcached是它服务器端的主程序文件名，知道我的意思了把~~~~。一个是项目名称，一个是主程序文件名，在网上看到了很多人不明白，于是混用了。 Memcache的安装 分为两个过程：memcache服务器端的安装和memcached客户端的安装。 所谓服务器端的安装就是在服务器（一般都是linux系统）上安装Memcache实现数据的存储 所谓客户端的安装就是指php（或者其他程序，Memcache还有其他不错的api接口提供）去使用服务器端的Memcache提供的函数，需要php添加扩展。 具体的配置大家可以参考： Linux下的Memcache安装：http://www.ccvita.com/257.html Windows下的Memcache安装：http://www.ccvita.com/258.html Memcache基础教程：http://www.ccvita.com/259.html Discuz!的Memcache缓存实现：http://www.ccvita.com/261.html Memcache协议中文版：http://www.ccvita.com/306.html Memcache分布式部署方案：http://www.ccvita.com/395.html PHP的Memcache &#60; ?php //连接 $mem = new Memcache; $mem-&#62;connect("192.168.0.200", 12000);//保存数据 $mem-&#62;set('key1', 'This is first value', 0, 60); $val = $mem-&#62;get('key1'); echo "Get key1 value: " . $val ."&#60;br /&#62;"; //替换数据 $mem-&#62;replace('key1', 'This is replace value', [...]<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="Windows下的Memcache安装" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F402&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986">
                        <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;">Windows下的Memcache安装</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 mutex设计模式" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F6772&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986">
                        <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 mutex设计模式</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="windows环境下memcache服务器使用经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986">
                        <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;">windows环境下memcache服务器使用经验</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="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986">
                        <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 memcache手册</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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>Memcache是什么</strong><br />
Memcache是danga.com的一个项目，最早是为 LiveJournal 服务的，目前全世界不少人使用这个缓存项目来构建自己大负载的网站，来分担数据库的压力。<br />
它可以应对任意多个连接，使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间，然后建立一个HashTable，Memcached自管理这些HashTable。<br />
Memcache官方网站：http://www.danga.com/memcached，更多详细的信息可以来这里了解 .</p>
<p>为什么会有Memcache和memcached两种名称？<br />
其实Memcache是这个项目的名称，而memcached是它服务器端的主程序文件名，知道我的意思了把~~~~。一个是项目名称，一个是主程序文件名，在网上看到了很多人不明白，于是混用了。</p>
<p><strong>Memcache的安装</strong><br />
分为两个过程：memcache服务器端的安装和memcached客户端的安装。<br />
所谓服务器端的安装就是在服务器（一般都是linux系统）上安装Memcache实现数据的存储<br />
所谓客户端的安装就是指php（或者其他程序，Memcache还有其他不错的api接口提供）去使用服务器端的Memcache提供的函数，需要php添加扩展。<span id="more-8986"></span></p>
<p>具体的配置大家可以参考：<br />
<a href="http://www.ccvita.com/257.html">Linux下的Memcache安装</a>：http://www.ccvita.com/257.html<br />
<a href="http://www.ccvita.com/258.html">Windows下的Memcache安装</a>：http://www.ccvita.com/258.html<br />
<a href="http://www.ccvita.com/259.html">Memcache基础教程</a>：http://www.ccvita.com/259.html<br />
<a href="http://www.ccvita.com/261.html">Discuz!的Memcache缓存实现</a>：http://www.ccvita.com/261.html<br />
<a href="http://www.ccvita.com/306.html">Memcache协议中文版</a>：http://www.ccvita.com/306.html<br />
<a href="http://www.ccvita.com/395.html">Memcache分布式部署方案</a>：http://www.ccvita.com/395.html</p>
<p><strong>PHP的Memcache</strong></p>
<div>
<blockquote>
<div>&lt; ?php<br />
//连接<br />
$mem = new Memcache;<br />
$mem-&gt;connect("192.168.0.200", 12000);//保存数据<br />
$mem-&gt;set('key1', 'This is first value', 0, 60);<br />
$val = $mem-&gt;get('key1');<br />
echo "Get key1 value: " . $val ."&lt;br /&gt;";</p>
<p>//替换数据<br />
$mem-&gt;replace('key1', 'This is replace value', 0, 60);<br />
$val = $mem-&gt;get('key1');<br />
echo "Get key1 value: " . $val . "&lt;br /&gt;";</p>
<p>//保存数组<br />
$arr = array('aaa', 'bbb', 'ccc', 'ddd');<br />
$mem-&gt;set('key2', $arr, 0, 60);<br />
$val2 = $mem-&gt;get('key2');<br />
echo "Get key2 value: ";<br />
print_r($val2);<br />
echo "&lt;br /&gt;";</p>
<p>//删除数据<br />
$mem-&gt;delete('key1');<br />
$val = $mem-&gt;get('key1');<br />
echo "Get key1 value: " . $val . "&lt;br /&gt;";</p>
<p>//清除所有数据<br />
$mem-&gt;flush();<br />
$val2 = $mem-&gt;get('key2');<br />
echo "Get key2 value: ";<br />
print_r($val2);<br />
echo "&lt;br /&gt;";</p>
<p>//关闭连接<br />
$mem-&gt;close();<br />
?&gt;</p>
</div>
</blockquote>
</div>
<p>如果正常的话，浏览器将输出：</p>
<blockquote><p>Get key1 value: This is first value<br />
Get key1 value: This is replace value<br />
Get key2 value: Array ( [0] =&gt; aaa [1] =&gt; bbb [2] =&gt; ccc [3] =&gt; ddd )<br />
Get key1 value:<br />
Get key2 value:</p></blockquote>
<p><strong>程序代码分析</strong></p>
<p>初始化一个Memcache的对象：<br />
<span style="color: #993300;">$mem</span> = new <span style="color: #000000;">Memcache</span>;</p>
<p>连接到我们的Memcache服务器端，第一个参数是服务器的IP地址，也可以是主机名，第二个参数是Memcache的开放的端口：<br />
<span style="color: #993300;">$mem-&gt;</span>connect(<span style="color: #339966;">"192.168.0.200"</span>, <span style="color: #ff0000;">12000</span>);</p>
<p>保存一个数据到Memcache服务器上，第一个参数是数据的key，用来定位一个数据，第二个参数是需要保存的数据内容，这里是一个字符串，第三个参数是一个标记，一般设置为0或者MEMCACHE_COMPRESSED就行了，第四个参数是数据的有效期，就是说数据在这个时间内是有效的，如果过去这个时间，那么会被Memcache服务器端清除掉这个数据，单位是秒，如果设置为0，则是永远有效，我们这里设置了60，就是一分钟有效时间：<br />
<span style="color: #993300;">$mem</span>-&gt;set(<span style="color: #339966;">‘key1</span>‘, <span style="color: #339966;">‘This is first value’</span>, <span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">60</span>);</p>
<p>从Memcache服务器端获取一条数据，它只有一个参数，就是需要获取数据的key，我们这里是上一步设置的key1，现在获取这个数据后输出输出：<br />
<span style="color: #993300;">$val</span> = <span style="color: #993300;">$mem</span>-&gt;get(’<span style="color: #339966;">key1′</span>);<br />
echo <span style="color: #339966;">"Get key1 value: "</span> . <span style="color: #993300;">$val;</span></p>
<p>现在是使用replace方法来替换掉上面key1的值，replace方法的参数跟set是一样的，不过第一个参数key1是必须是要替换数据内容的key，最后输出了：<br />
<span style="color: #993300;">$mem</span>-&gt;replace(<span style="color: #339966;">‘key1′</span>, <span style="color: #339966;">‘This is replace value’</span>, <span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">60</span>);<br />
<span style="color: #993300;">$val</span> = <span style="color: #993300;">$mem</span>-&gt;get(<span style="color: #339966;">‘key1′</span>);<br />
echo "<span style="color: #339966;">Get key1 value:</span> " . <span style="color: #993300;">$val</span>;</p>
<p>同样的，Memcache也是可以保存数组的，下面是在Memcache上面保存了一个数组，然后获取回来并输出<br />
<span style="color: #993300;">$arr</span> = array(<span style="color: #339966;">‘aaa’,</span> <span style="color: #339966;">‘bbb’,</span> <span style="color: #339966;">‘ccc’</span>, <span style="color: #339966;">‘ddd’</span>);<br />
<span style="color: #993300;">$mem</span>-&gt;set(<span style="color: #339966;">‘key2′</span>, <span style="color: #ff0000;">$arr</span>, <span style="color: #ff0000;">0</span>, <span style="color: #ff0000;">60</span>);<br />
<span style="color: #993300;">$val2</span> = <span style="color: #993300;">$mem</span>-&gt;get(<span style="color: #339966;">‘key2′</span>);<br />
print_r(<span style="color: #993300;">$val2</span>);</p>
<p>现在删除一个数据，使用delte接口，参数就是一个key，然后就能够把Memcache服务器这个key的数据删除，最后输出的时候没有结果<br />
<span style="color: #0000ff;">$</span><span style="color: #993300;">mem</span>-&gt;delete(<span style="color: #339966;">‘key1′</span>);<br />
$<span style="color: #993300;">val</span> = $<span style="color: #993300;">mem</span>-&gt;get(<span style="color: #339966;">‘key1′</span>);<br />
echo "<span style="color: #339966;">Get key1 value:</span> " . $<span style="color: #993300;">val</span> . <span style="color: #339966;">"&lt;br&gt;";</span></p>
<p>最后我们把所有的保存在Memcache服务器上的数据都清除，会发现数据都没有了，最后输出key2的数据为空，最后关闭连接<br />
<span style="color: #0000ff;">$</span><span style="color: #993300;">mem</span>-&gt;flush();<br />
$<span style="color: #993300;">val2</span> = $<span style="color: #993300;">mem</span>-&gt;get(<span style="color: #339966;">‘key2′</span>);<br />
echo "<span style="color: #339966;">Get key2 value:</span> ";<br />
print_r(<span style="color: #993300;">$val2</span>);<br />
echo <span style="color: #339966;">"&lt;br&gt;";</span></p>
<p><strong>Memcache的使用</strong><br />
使用Memcache的网站一般流量都是比较大的，为了缓解数据库的压力，让Memcache作为一个缓存区域，把部分信息保存在内存中，在前端能够迅速的进行存取。那么一般的焦点就是集中在如何分担数据库压力和进行分布式，毕竟单台Memcache的内存容量的有限的。我这里简单提出我的个人看法，未经实践，权当参考。</p>
<p><em>分布式应用</em><br />
Memcache本来支持分布式，我们客户端稍加改造，更好的支持。我们的key可以适当进行有规律的封装，比如以user为主的网站来说，每个用户都有User ID，那么可以按照固定的ID来进行提取和存取，比如1开头的用户保存在第一台Memcache服务器上，以2开头的用户的数据保存在第二胎Mecache服务器上，存取数据都先按照User ID来进行相应的转换和存取。</p>
<p>但是这个有缺点，就是需要对User ID进行判断，如果业务不一致，或者其他类型的应用，可能不是那么合适，那么可以根据自己的实际业务来进行考虑，或者去想更合适的方法。</p>
<p><em>减少数据库压力</em><br />
这个算是比较重要的，所有的数据基本上都是保存在数据库当中的，每次频繁的存取数据库，导致数据库性能极具下降，无法同时服务更多的用户，比如MySQL，特别频繁的锁表，那么让Memcache来分担数据库的压力吧。我们需要一种改动比较小，并且能够不会大规模改变前端的方式来进行改变目前的架构。</p>
<p>我考虑的一种简单方法：<br />
后端的数据库操作模块，把所有的Select操作提取出来（update/delete/insert不管），然后把对应的SQL进行相应的hash算法计算得出一个hash数据key（比如MD5或者SHA），然后把这个key去Memcache中查找数据，如果这个数据不存在，说明还没写入到缓存中，那么从数据库把数据提取出来，一个是数组类格式，然后把数据在set到Memcache中，key就是这个SQL的hash值，然后相应的设置一个失效时间，比如一个小时，那么一个小时中的数据都是从缓存中提取的，有效减少数据库的压力。缺点是数据不实时，当数据做了修改以后，无法实时到前端显示，并且还有可能对内存占用比较大，毕竟每次select出来的数据数量可能比较巨大，这个是需要考虑的因素。</p>
<p><strong>Memcache的安全</strong><br />
我们上面的Memcache服务器端都是直接通过客户端连接后直接操作，没有任何的验证过程，这样如果服务器是直接暴露在互联网上的话是比较危险，轻则数据泄露被其他无关人员查看，重则服务器被入侵，因为Mecache是以root权限运行的，况且里面可能存在一些我们未知的bug或者是缓冲区溢出的情况，这些都是我们未知的，所以危险性是可以预见的。为了安全起见，我做两点建议，能够稍微的防止黑客的入侵或者数据的泄露。</p>
<p><span style="color: #ff0000;"><em>内网访问</em></span><br />
最好把两台服务器之间的访问是内网形态的，一般是Web服务器跟Memcache服务器之间。普遍的服务器都是有两块网卡，一块指向互联网，一块指向内网，那么就让Web服务器通过内网的网卡来访问Memcache服务器，我们Memcache的服务器上启动的时候就监听内网的IP地址和端口，内网间的访问能够有效阻止其他非法的访问。<br />
<span style="color: #0000ff;"># memcached -d -m 1024 -u root -l 192.168.0.200 -p 11211 -c 1024 -P /tmp/memcached.pid</span><br />
Memcache服务器端设置监听通过内网的192.168.0.200的ip的11211端口，占用1024MB内存，并且允许最大1024个并发连接</p>
<p><span style="color: #ff0000;"><em>设置防火墙</em></span><br />
防火墙是简单有效的方式，如果却是两台服务器都是挂在网的，并且需要通过外网IP来访问Memcache的话，那么可以考虑使用防火墙或者代理程序来过滤非法访问。<br />
一般我们在Linux下可以使用iptables或者FreeBSD下的ipfw来指定一些规则防止一些非法的访问，比如我们可以设置只允许我们的Web服务器来访问我们Memcache服务器，同时阻止其他的访问。</p>
<blockquote><p># iptables -F<br />
# iptables -P INPUT DROP<br />
# iptables -A INPUT -p tcp -s 192.168.0.2 –dport 11211 -j ACCEPT<br />
# iptables -A INPUT -p udp -s 192.168.0.2 –dport 11211 -j ACCEPT</p></blockquote>
<p>上面的iptables规则就是只允许192.168.0.2这台Web服务器对Memcache服务器的访问，能够有效的阻止一些非法访问，相应的也可以增加一些其他的规则来加强安全性，这个可以根据自己的需要来做。</p>
<p>本文章是整理收集而来，主要来源是：<a href="http://blog.csdn.net/heiyeshuwu/archive/2006/11/13/1380838.aspx" target="_blank">http://blog.csdn.net/heiyeshuwu/archive/2006/11/13/1380838.aspx</a></p>
<p>&nbsp;</p>
<p><strong>相关教程:</strong></p>
<p>memcache的addServer的故障转移机制: <a href="http://blog.haohtml.com/archives/11782">http://blog.haohtml.com/archives/11782</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="Windows下的Memcache安装" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F402&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986">
                        <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;">Windows下的Memcache安装</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 mutex设计模式" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F6772&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986">
                        <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 mutex设计模式</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="windows环境下memcache服务器使用经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986">
                        <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;">windows环境下memcache服务器使用经验</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="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986">
                        <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 memcache手册</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/8986/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>遍历memcache中已缓存的key</title>
		<link>http://blog.haohtml.com/archives/8111</link>
		<comments>http://blog.haohtml.com/archives/8111#comments</comments>
		<pubDate>Wed, 23 Mar 2011 08:18:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=8111</guid>
		<description><![CDATA[最近需要做一个缓存管理的功能。其中有一个需要模糊匹配memcached的key然后进行删除匹配上的key对应的数据。 难点在于memcache 没有提供模糊匹配key删除缓存的功能，也没有提供遍历memcache key的功能。在网上search了下， 点击10个连接有9个都是一篇文章的copy。网上最流行的方法发现key不能正确的获得。baidu google 全用上了就是没有解决。。。 最后还是抱着试试的心态，终于把问题给解决了。废话少说。说说，我实现的代码： 遍历memcache的可以需要有一下几个步骤： 1、通过使用memcache 内置方法Memcache::getExtendedStats，首先获得items信息。 最后得到的解决类似与 1 $memcache = new Memcache(); 2 3  $all_items = $memcache-&#62;getExtendedStats('items'); 4 5 var_export($all_items); &#160; 1 array ( 2 '192.168.0.110:11211' =&#62; 3 array ( 4 'items' =&#62; 5 array ( 6 1 =&#62; 7 array ( 8 'number' =&#62; '1', 9 'age' =&#62; '1851', [...]<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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8111">
                        <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="Memcache基础教程" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8111">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8111">
                        <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 memcache手册</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="windows环境下memcache服务器使用经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8111">
                        <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;">windows环境下memcache服务器使用经验</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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 id="cnblogs_post_body">
<p>最近需要做一个缓存管理的功能。其中有一个需要模糊匹配memcached的key然后进行删除匹配上的key对应的数据。</p>
<p>难点在于memcache 没有提供模糊匹配key删除缓存的功能，也没有提供遍历memcache key的功能。在网上search了下，</p>
<p>点击10个连接有9个都是一篇文章的copy。网上最流行的方法发现key不能正确的获得。baidu google 全用上了就是没有解决。。。</p>
<p>最后还是抱着试试的心态，终于把问题给解决了。废话少说。说说，我实现的代码：</p>
<p>遍历memcache的可以需要有一下几个步骤：</p>
<p>1、通过使用memcache 内置方法Memcache::getExtendedStats，首先获得items信息。</p>
<p>最后得到的解决类似与<span id="more-8111"></span></p>
<div>
<pre>
<div>1 $memcache = new Memcache();
2
3  $all_items = $memcache-&gt;getExtendedStats('items');
4
5 var_export($all_items);</div>
</pre>
</div>
<p>&nbsp;</p>
<div>
<pre>
<div> 1 array (
 2   '192.168.0.110:11211' =&gt;
 3   array (
 4     'items' =&gt;
 5     array (
 6       1 =&gt;
 7       array (
 8         'number' =&gt; '1',
 9         'age' =&gt; '1851',
10       ),
11       2 =&gt;
12       array (
13         'number' =&gt; '1',
14         'age' =&gt; '1851',
15       ),
16       3 =&gt;
17       array (
18         'number' =&gt; '2',
19         'age' =&gt; '1864',
20       ),
21       7 =&gt;
22       array (
23         'number' =&gt; '1',
24         'age' =&gt; '1851',
25       ),
26       9 =&gt;
27       array (
28         'number' =&gt; '1',
29         'age' =&gt; '1',
30       ),
31       12 =&gt;
32       array (
33         'number' =&gt; '2',
34         'age' =&gt; '1851',
35       ),
36       13 =&gt;
37       array (
38         'number' =&gt; '1',
39         'age' =&gt; '1851',
40       ),
41       14 =&gt;
42       array (
43         'number' =&gt; '1',
44         'age' =&gt; '1851',
45       ),
46       15 =&gt;
47       array (
48         'number' =&gt; '1',
49         'age' =&gt; '1851',
50       ),
51       16 =&gt;
52       array (
53         'number' =&gt; '1',
54         'age' =&gt; '1850',
55       ),
56       18 =&gt;
57       array (
58         'number' =&gt; '2',
59         'age' =&gt; '1851',
60       ),
61       19 =&gt;
62       array (
63         'number' =&gt; '1',
64         'age' =&gt; '1851',
65       ),
66       20 =&gt;
67       array (
68         'number' =&gt; '1',
69         'age' =&gt; '1851',
70       ),
71     ),
72   ),
73 )</div>
</pre>
</div>
<p>$all_items中的key“192.168.0.110:11211” 就是memcache的host和port。</p>
<p>2、已$all_items做为数据源，再次调用Memcache::getExtendedStats，我们需要的数据就在返回的结果里面</p>
<p>﻿我们假设memcache所有的host信息为$options = array('192.168.0.110:11211',);</p>
<p>﻿</p>
<div>
<pre>
<div> 1 foreach ($options as $option) {
 2      if (isset($all_items[$option]['items'])) {
 3            $items      = $all_items[$option]['items'];
 4
 5            foreach ($items as $number =&gt; $item) {
 6                  $str    = $memcache-&gt;getExtendedStats('cachedump', $number, 0);
 7                  $line   = $str[$option];
 8                  if (is_array($line) &amp;&amp; count($line) &gt; 0) {
 9                       foreach ($line as $key =&gt; $value) {
10                            $keys[] = $key;
11                       }
12                  }
13            }
14       }
15 }</div>
</pre>
</div>
<p>上面的$keys数组就是我们需要的数据了。</p>
<p>﻿下面贴出来完整的代码</p>
<p>﻿</p>
<div>
<pre>
<div> 1 function list_key() {
 2     $memcache = new Memcache();
 3     $all_items = $memcache-&gt;getExtendedStats('items');
 4     $keys      = array();
 5     foreach ($this-&gt;_options as $options) {
 6         foreach ($options as $option) {
 8         　　if (isset($all_items[$option]['items'])) {
 9                  $items = $all_items[$option]['items'];
10                  foreach ($items as $number =&gt; $item) {
11                        $str    = $memcache-&gt;getExtendedStats('cachedump', $number, 0);
12                        $line   = $str[$option];
13                    　　if (is_array($line) &amp;&amp; count($line) &gt; 0){</div>
<div>　　　　　　　　　　　　　　　　foreach ($line as $key =&gt; $value) {</div>
<div>                                 $keys[] = $key;
14                      　　 }
15                    　　}
16                }
17            }
18         }
19     }
20
21     return array_unique($keys);
22
23 }</div>
</pre>
</div>
<p>﻿</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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8111">
                        <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="Memcache基础教程" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8111">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8111">
                        <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 memcache手册</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="windows环境下memcache服务器使用经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8111">
                        <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;">windows环境下memcache服务器使用经验</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/8111/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php memcache手册</title>
		<link>http://blog.haohtml.com/archives/8109</link>
		<comments>http://blog.haohtml.com/archives/8109#comments</comments>
		<pubDate>Wed, 23 Mar 2011 07:07:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=8109</guid>
		<description><![CDATA[php memcache手册 1.简介 memcache模块是一个高效的守护进程，提供用于内存缓存的过程式程序和面向对象的方便的接口，特别是对于设计动态web程序时减少对数据库的 访问。 memcache也提供用于通信对话（session_handler）的处理。 更多Memcache 模块相关信息可以到 http://www.danga.com/memcached/ 查阅。 1.1.memcache在php.ini中的配置项列表 memcache在php.ini中的配置项列表 名称 默认值 是否可变 改变日志 memcache.allow_failover “1” PHP_INI_ALL Available since memcache 2.0.2. memcache.max_failover_attempts "20" PHP_INI_ALL Available since memcache 2.1.0. memcache.chunk_size "8192" PHP_INI_ALL Available since memcache 2.0.2. memcache.default_port "11211" PHP_INI_ALL Available since memcache 2.0.2. memcache.hash_strategy "standard" PHP_INI_ALL Available since memcache 2.2.0. memcache.hash_function "crc32" PHP_INI_ALL Available [...]<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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109">
                        <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="Memcache基础教程" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="windows环境下memcache服务器使用经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109">
                        <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;">windows环境下memcache服务器使用经验</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="windows下memcached的安装（转载）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F406&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109">
                        <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;">windows下memcached的安装（转载）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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>
<h2>php memcache手册</h2>
</div>
<div>
<div>
<h2>1.简介</h2>
<p>memcache模块是一个高效的守护进程，提供用于内存缓存的过程式程序和面向对象的方便的接口，特别是对于设计动态web程序时减少对数据库的 访问。</p>
<p>memcache也提供用于通信对话（session_handler）的处理。</p>
<p>更多Memcache 模块相关信息可以到 <a href="http://www.danga.com/memcached/">http://www.danga.com/memcached/</a> 查阅。</p>
<h3>1.1.memcache在php.ini中的配置项列表</h3>
<table border="1" cellspacing="1" cellpadding="1" width="600">
<caption>memcache在php.ini中的配置项列表</caption>
<tbody>
<tr>
<td><strong>名称</strong></td>
<td><strong>默认值</strong></td>
<td><strong>是否可变</strong></td>
<td><strong>改变日志</strong></td>
</tr>
<tr>
<td>memcache.allow_failover</td>
<td>“1”</td>
<td>PHP_INI_ALL</td>
<td>Available since memcache 2.0.2.</td>
</tr>
<tr>
<td>memcache.max_failover_attempts</td>
<td>"20"</td>
<td>PHP_INI_ALL</td>
<td>Available since memcache 2.1.0.</td>
</tr>
<tr>
<td>memcache.chunk_size</td>
<td>"8192"</td>
<td>PHP_INI_ALL</td>
<td>Available since memcache 2.0.2.</td>
</tr>
<tr>
<td>memcache.default_port</td>
<td>"11211"</td>
<td>PHP_INI_ALL</td>
<td>Available since memcache 2.0.2.</td>
</tr>
<tr>
<td>memcache.hash_strategy</td>
<td>"standard"</td>
<td>PHP_INI_ALL</td>
<td>Available since memcache 2.2.0.</td>
</tr>
<tr>
<td>memcache.hash_function</td>
<td>"crc32"</td>
<td>PHP_INI_ALL</td>
<td>Available since memcache 2.2.0.</td>
</tr>
<tr>
<td>session.save_handler</td>
<td>"files"</td>
<td>PHP_INI_ALL</td>
<td>Supported since memcache 2.1.2</td>
</tr>
<tr>
<td>session.save_path</td>
<td>""</td>
<td>PHP_INI_ALL</td>
<td>Supported since memcache 2.1.2</td>
</tr>
</tbody>
</table>
<p>有关 PHP_INI_* 常量进一步的细节与定义参见PHP手册php.ini 配置选项。<span id="more-8109"></span></p>
<h3>1.2.以下是配置项的简要解释</h3>
<p><em>memcache.allow_failover</em> Boolean</p>
<p>在错误时是否透明的故障转移到 其他服务器上处理（注：故障转移是动词）。</p>
<p><em>memcache.max_failover_attempts</em> integer</p>
<p>定义服务器的数量类设置和获取数据，只联合 memcache.allow_failover 一同使用。</p>
<p><em>memcache.chunk_size</em> integer</p>
<p>数据将会被分成指定大小（chunk_size）的块来传输，这个值（chunk_size）越小，写操作的请求就越多，如果发现其他的无法解释的 减速，请试着将这个值增大到32768.</p>
<p><em>memcache.default_port</em> string</p>
<p>当连接memcache服务器的时候，如果没有指定端口这个默认的tcp端口将被用。</p>
<p><em>memcache.hash_strategy</em> string</p>
<p>控制在映射 key 到服务器时使用哪种策略。设置这个值一致能使hash 算法始终如一的使用于服务器接受添加或者删除池中变量时将不会被重新映射。设置这个值以标准的结果在旧的策略被使用时。</p>
<p><em>memcache.hash_function</em> string</p>
<p>控制哪种 hsah 函数被应用于 key映射 到服务器过程中，默认值“crc32”使用 CRC32 算法，而“fnv”则表示使用 FNV-1a 算法。</p>
<p><em>session.save_handler</em> string</p>
<p>通过设置这个值为memcache来确定使用 memcache 用于通信对话的处理（session handler）。</p>
<p><em>session.save_path</em> string</p>
<p>定义用于通话存储的各服务器链接的分隔符号，例如：“tcp://host1:11211, tcp://host2:11211”。</p>
<p>每服务器个链接可以包含被接受于该服务器的参数，比较类似使用 Memcache::addServer() 来添加的服务器，例如：“tcp://host1:11211?persistent=1&amp;weight=1&amp;timeout=1&amp; amp; amp; amp;retry_interval=15”。</p>
<h3>1.3.memcache常量列表</h3>
<table border="1" cellspacing="1" cellpadding="1" width="600">
<caption>memcache常量列表</caption>
<tbody>
<tr>
<td><strong>名称</strong></td>
<td><strong>类型</strong></td>
<td><strong>描述</strong></td>
</tr>
<tr>
<td>MEMCACHE_COMPRESSED</td>
<td>integer</td>
<td>用于调整在使用 Memcache::set(), Memcache::add() 和 Memcache::replace() 几个函数时的压缩比率。</td>
</tr>
<tr>
<td>MEMCACHE_HAVE_SESSION</td>
<td>integer</td>
<td>如果通信对话的处理（session handler）被允许使用其值为 1，其他情况值为 0。</td>
</tr>
</tbody>
</table>
<h2>2Memcache Functions 函数列表</h2>
<h3>2.1.Memcache::connect</h3>
<h4>2.1.1.说明</h4>
<p>bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )</p>
<p>连接memcache服务器</p>
<h4>2.1.2.参数</h4>
<p>$host(string) 服务器域名或ip</p>
<p>$port(int) 服务器tcp端口号，默认值是11211</p>
<p>$timeout 连接memcache进程的失效时间，在修改它的默认值1的时候要三思，以免失去所有memcache缓存的优势导致连接变得很慢。</p>
<h4>2.1.3.返回值</h4>
<p>如果成功则返回true，失败则返回false</p>
<h4>2.1.4.范例</h4>
<pre>&lt;?php

$memcache_obj = memcache_connect('memcache_host', 11211);

$memcache = new Memcache;
$memcache-&gt;connect('memcache_host', 11211);
?&gt;</pre>
<h3>2.2.Memcache::pconnect</h3>
<h4>2.2.1.说明</h4>
<p>bool Memcache::pconnect ( string $host [, int $port [, int $timeout ]] )</p>
<p>以常连接方式连接服务器</p>
<h4>2.2.2.参数</h4>
<p>$host(string) 服务器域名或ip</p>
<p>$port(int) 服务器tcp端口号，默认值是11211</p>
<p>$timeout 连接memcache进程的失效时间，在修改它的默认值1的时候要三思，以免失去所有memcache缓存的优势导致连接变得很慢。</p>
<h4>2.2.3.返回值</h4>
<p>如果成功则返回true，失败则返回false</p>
<h4>2.2.4.范例</h4>
<pre>&lt;?php
$memcache_obj = memcache_pconnect('memcache_host', 11211);

$memcache_obj = new Memcache;
$memcache_obj-&gt;pconnect('memcache_host', 11211);
?&gt;</pre>
<h3>2.3.Memcache::close</h3>
<h4>2.3.1.说明</h4>
<p>bool Memcache::close ( void )</p>
<p>关闭对象 (对常连接不起作用)</p>
<h4>2.3.2.返回值</h4>
<p>如果成功则返回true，失败则返回false</p>
<h4>2.3.3.范例</h4>
<pre>&lt;?php

$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_close($memcache_obj);

$memcache_obj = new Memcache;
$memcache_obj-&gt;connect('memcache_host', 11211);

$memcache_obj-&gt;close();
?&gt;</pre>
<h3>2.4.Memcache::addServer</h3>
<h4>2.4.1.说明</h4>
<p>bool Memcache::addServer ( string $host [, int $port [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback ]]]]]]] )</p>
<p>向对象添加一个服务器（注：addServer没有连接到服务器的动作，所以在memcache进程没有启动的时候，执行addServer成功也 会返回true）</p>
<h4>2.4.2.参数</h4>
<p>host               服务器域名或 IP</p>
<p>port               端口号，默认为 11211</p>
<p>persistent         是否使用常连接，默认为 TRUE</p>
<p>weight             权重，在多个服务器设置中占的比重</p>
<p>timeout          连接服务器失效的秒数，修改默认值 1 时要三思，有可能失去所有缓存方面的优势导致连接变得很慢</p>
<p>retry_interval    服务器连接失败时的重试频率，默认是 15 秒一次，如果设置为 -1 将禁止自动重试，当扩展中加载了 dynamically via dl() 时，无论本参数还是常连接设置参数都会失效。</p>
<p>每一个失败的服务器在失效前都有独自的生存期，选择后端请求时会被跳过而不服务于请求。一个过期的连接将成功的重新连接或者被标记为失败的连接等待下一次 重试。这种效果就是说每一个 web server 的子进程在服务于页面时的重试连接都跟他们自己的重试频率有关。</p>
<p>status             控制服务器是否被标记为 online，设置这个参数为 FALSE 并设置 retry_interval 为 -1 可以使连接失败的服务器被放到一个描述不响应请求的服务器池子中，对这个服务器的请求将失败，接受设置为失败服务器的设置，默认参数为 TRUE，代表该服务器可以被定义为 online。</p>
<p>failure_callback   失败时的回调函数，函数的两个参数为失败服务器的 hostname 和 port</p>
<h4>2.4.3.返回值</h4>
<p>成功返回 <span style="color: #0000ff;">TRUE</span>，失败返回 <span style="color: #0000ff;">FALSE</span>。</p>
<p>注：在测试addServer函数的时候我们主要测试了其参数 retry_interval和status</p>
<h4>2.4.4.范例</h4>
<p><strong>2.4.4.1.retry_interval参数的测试</strong></p>
<pre>&lt;?php
$mem = new Memcache;
$is_add = $mem-&gt;addServer('localhost', 11211, true, 1, 1, 15, true); // retrt_interval=15
$is_set = $mem-&gt;set('key1', '中华人民共和国');
?&gt;</pre>
<p>上面的例子中如果localhost服务器down掉或是memcache守护进程当掉，执行请求的时候连接服务器失败时算起15秒后会自动重试连 接服务器，但是在这15秒内不会去连接这个服务器，就是只要有请求，没15秒就会尝试连接服务器，但是每个服务器连接重试是独立的。比如说我一次添加了两 个服务器一个是localhost，一个是172.16.100.60，它们分别是从各自连接失败那个时间算起，只要对各自服务器有请求就会每隔15秒去 连接各自的服务器的。</p>
<p><strong>2.4.4.2.retry_interval和status结合使用的情况</strong></p>
<pre>&lt;?php
$mem = new Memcache;
$is_add = $mem-&gt;addServer('localhost', 11211, true, 1, 1, -1, false); // retrt_interval=-1, status=false
$is_set = $mem-&gt;set('key1', '中华人民共和国');
?&gt;</pre>
<p>在上面的retrt_interval=-1, status=false这种情况下，将连接失败的服务器放到一个不响应请求的一个池子中，因此对key分配的算法也就没有影响了，而他是立即返回错误失 败还是故障转移还要看memcache.allow_failover的设置，执行set， add， replace，get等请求的时候都会失败返回false，即使memcache进程运行正常。</p>
<h4>2.4.4.3.status参数的测试</h4>
<p>除了与retry_interval结合使用，status单独使用的情况会对函数memcache::getServerStatu获得的结果产 生影响</p>
<p>无论memcache进程的正常运行还是当掉，status为true的时候getServerStatus的结果都是true，反之则为 false</p>
<p>但是在memcache进程正常运行的情况下，对set，add，replace，get等函数都没有影响。</p>
<h3>2.5.Memcache::add</h3>
<h4>2.5.1.说明</h4>
<p><span style="color: #0000ff;">bool </span>Memcache::add ( string $key , mixed $var [, int $flag [, int $expire ]] )</p>
<p>添加一个要缓存的数据如果作为这个缓存的数据的键在服务器上还不存在的情况下,</p>
<h4>2.5.2.参数</h4>
<p>key                缓存数据的键 其长度不能超过250个字符</p>
<p>var                值，整型将直接存储，其他类型将被序列化存储 ，其值最大为1M</p>
<p>flag               是否使用 zlib 压缩 ,当flag=MEMCACHE_COMPRESSED的时侯，数据很小的时候不会采用zlib压缩，只有数据达到一定大小才对数据进行zlib压 缩。（没有具体的测试数据进行压缩的最小值是多少）</p>
<p>expire             过期时间，0 为永不过期，可使用 unix 时间戳格式或距离当前时间的秒数，设为秒数时不能大于 2592000（30 天）</p>
<h4>2.5.3.返回值</h4>
<p>成功返回 TRUE，失败返回 FALSE，如果这个键已经存在，其他方面memcache::add()的行为与memcache::set相似</p>
<h4>2.5.4.范例</h4>
<pre>&lt;?php
memcache_add($memcache_obj, 'var_key', 'test variable', FALSE, 30);
$memcache_obj = memcache_connect("localhost", 11211);
$memcache_obj-&gt;add('var_key', 'test variable', FALSE, 30);
?&gt;</pre>
<h3>2.6.Memcache::replace</h3>
<h4>2.6.1.说明</h4>
<p><span style="color: #0000ff;">bool</span> Memcache::replace ( string $key , mixed $var [, int $flag [, int $expire ]] )</p>
<p>替换一个指定 已存在key 的的缓存变量内容</p>
<h4>2.6.2.参数</h4>
<p>key                缓存数据的键， 其长度不能超过250个字符</p>
<p>var                值，整型将直接存储，其他类型将被序列化存储，其值最大为1M</p>
<p>flag               是否使用 zlib 压缩 ,当flag=MEMCACHE_COMPRESSED的时侯，数据很小的时候不会采用zlib压缩，只有数据达到一定大小才对数据进行zlib压 缩。（没有具体的测试数据进行压缩的最小值是多少）</p>
<p>expire             过期时间，0 为永不过期，可使用 unix 时间戳格式或距离当前时间的秒数，设为秒数时不能大于 2592000（30 天）</p>
<h4>2.6.3.返回值</h4>
<p>成功返回 TRUE，失败返回 FALSE。</p>
<h4>2.***.范例</h4>
<pre>&lt;?php
$memcache_obj = memcache_connect('memcache_host', 11211);

memcache_replace($memcache_obj, "test_key", "some variable", FALSE, 30);

$memcache_obj-&gt;replace("test_key", "some variable", FALSE, 30);
?&gt;</pre>
<h3>2.7.Memcache::set</h3>
<h4>2.7.1.说明</h4>
<p><span style="color: #0000ff;">bool</span> Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )</p>
<p>设置一个指定 key 的缓存变量内容</p>
<h4>2.7.2.参数</h4>
<p>key                缓存数据的键， 其长度不能超过250个字符</p>
<p>var                值，整型将直接存储，其他类型将被序列化存储，其值最大为1M</p>
<p>flag               是否使用 zlib 压缩 ,当flag=MEMCACHE_COMPRESSED的时侯，数据很小的时候不会采用zlib压缩，只有数据达到一定大小才对数据进行zlib压 缩。（没有具体的测试数据进行压缩的最小值是多少）</p>
<p>expire             过期时间，0 为永不过期，可使用 unix 时间戳格式或距离当前时间的秒数，设为秒数时不能大于 2592000（30 天）</p>
<h4>2.7.3.返回值</h4>
<p>成功返回 TRUE，失败返回 FALSE。</p>
<h4>2.7.4.范例</h4>
<pre>&lt;?php
$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_set($memcache_obj, 'var_key', 'some variable', 0, 30);
echo memcache_get($memcache_obj, 'var_key');
?&gt;
&lt;?php
$memcache_obj = new Memcache;
$memcache_obj-&gt;connect('memcache_host', 11211);
$memcache_obj-&gt;set('var_key', 'some really big variable', MEMCACHE_COMPRESSED, 50);
echo $memcache_obj-&gt;get('var_key');
?&gt;</pre>
<h3>2.8.Memcache::get</h3>
<h4>2.8.1.说明</h4>
<p>string Memcache::get ( string $key [, int &amp;$flags ] )</p>
<p>array Memcache::get ( array $keys [, array &amp;$flags ] )</p>
<p>获取某个 key 的变量缓存值</p>
<h4>2.8.2.参数</h4>
<p>key                缓存值的键</p>
<p>flags              如果是传址某个变量，获取缓存值被set或是add的flag结果将被存于该变量</p>
<h4>2.8.3.返回值</h4>
<p>返回缓存的指定 key 的变量内容或者是在失败或该变量的值不存在时返回 FALSE</p>
<p>如果传出的key的数组中的key都不存在，返回的结果是一个空数组，反之则返回key与缓存值相关联的关联数组</p>
<h4>2.8.4.范例</h4>
<pre>&lt;?php
$memcache_obj = memcache_connect('memcache_host', 11211);
$var = memcache_get($memcache_obj, 'some_key');

$memcache_obj = new Memcache;
$memcache_obj-&gt;connect('memcache_host', 11211);
$var = $memcache_obj-&gt;get('some_key');

$memcache_obj = memcache_connect('memcache_host', 11211);
$var = memcache_get($memcache_obj, Array('some_key', 'another_key'));
//如果some_key，another_key不存在 $var = array();
//如果some_key，another_key存在 $var = array('some_key'=&gt;'缓存值', 'another_key'=&gt;'缓存值');

$memcache_obj = new Memcache;
$memcache_obj-&gt;connect('memcache_host', 11211);
$var = $memcache_obj-&gt;get(Array('some_key', 'second_key'));
?&gt;</pre>
<h3>2.9.Memcache::delete</h3>
<h4>2.9.1.说明</h4>
<p>bool Memcache::delete ( string $key [, int $timeout ] )</p>
<p>删除某一个变量的缓存</p>
<h4>2.9.2.参数</h4>
<p>key    缓存的键 键值不能为null和'’，当它等于前面两个值的时候php会有警告错误。</p>
<p>timeout   删除这项的时间，如果它等于0，这项将被立刻删除反之如果它等于30秒，那么这项被删除在30秒内</p>
<h4>2.9.3.返回值</h4>
<p>成功返回 TRUE，失败返回 FALSE。</p>
<p>2.9.4.范例</p>
<pre>&lt;?php
$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_delete($memcache_obj, 'key_to_delete', 10);

$memcache_obj = new Memcache;
$memcache_obj-&gt;connect('memcache_host', 11211);
$memcache_obj-&gt;delete('key_to_delete', 10);
?&gt;</pre>
<h3>2.10.Memcache::flush</h3>
<h4>2.10.1.说明</h4>
<p>bool Memcache::flush ( void )</p>
<p>清空所有缓存内容，不是真的删除缓存的内容，只是使所有变量的缓存过期，使内存中的内容被重写</p>
<h4>2.10.2.返回值</h4>
<p>成功返回 TRUE，失败返回 FALSE。</p>
<h4>2.10.3.范例</h4>
<pre>&lt;?php

$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_flush($memcache_obj);

$memcache_obj = new Memcache;
$memcache_obj-&gt;connect('memcache_host', 11211);
$memcache_obj-&gt;flush();
?&gt;</pre>
<h3>2.11.Memcache::getExtendedStats</h3>
<h4>2.11.1.说明</h4>
<p>array Memcache::getExtendedStats ([ string $type [, int $slabid [, int $limit ]]] )</p>
<p>获取所有服务器扩展静态信息</p>
<h4>2.11.2.参数</h4>
<p>type       静态信息类型，有效值包括{reset, malloc, maps, cachedump, slabs, items, sizes}，依照一定规则协议这个可选参数是为了方便开发人员查看不同类别的信息而输入的标题</p>
<p>slabid   用于按指定类型联合设置 cache 堆为有效的片到堆中。缓存堆被被命令绑定到服务器上并被严格的用于调试用途</p>
<p>limit    用于按指定类型联合设置 cache 堆为输入的数字所限制的大小到堆，默认值为 100</p>
<h4>2.11.3.返回值</h4>
<p>返回一个由服务器扩展静态信息二维数组，失败时返回 FALSE</p>
<h4>2.11.4.范例</h4>
<pre>&lt;?php
$memcache_obj = new Memcache;
$memcache_obj-&gt;addServer('memcache_host', 11211);
$memcache_obj-&gt;addServer('failed_host', 11211);
$stats = $memcache_obj-&gt;getExtendedStats(); print_r($stats);
?&gt;
输出结果
Array(
[memcache_host:11211] =&gt; Array(
[pid] =&gt; 3756
[uptime] =&gt; 603011
[time] =&gt; 1133810435
[version] =&gt; 1.1.12
[rusage_user] =&gt; 0.451931
[rusage_system] =&gt; 0.634903
[curr_items] =&gt; 2483
[total_items] =&gt; 3079
[bytes] =&gt; 2718136
[curr_connections] =&gt; 2
[total_connections] =&gt; 807
[connection_structures] =&gt; 13
[cmd_get] =&gt; 9748
[cmd_set] =&gt; 3096
[get_hits] =&gt; 5976
[get_misses] =&gt; 3772
[bytes_read] =&gt; 3448968
[bytes_written] =&gt; 2318883
[limit_maxbytes] =&gt; 33554432
),
[failed_host:11211] =&gt;
)</pre>
<h3>2.12.Memcache::getStats</h3>
<h4>2.12.1.说明</h4>
<p>array Memcache::getStats ([ string $type [, int $slabid [, int $limit ]]] )</p>
<p>获取最后添加服务器静态信息</p>
<h4>2.12.2.参数</h4>
<p>type 静态信息类型，有效值包括{reset, malloc, maps, cachedump, slabs, items, sizes}，依照一定规则协议这个可选参数是为了方便开发人员查看不同类别的信息而输入的标题</p>
<p>slabid   用于按指定类型联合设置 cache 堆为有效的片到堆中。缓存堆被被命令绑定到服务器上并被严格的用于调试用途</p>
<p>limit      用于按指定类型联合设置 cache 堆为输入的数字所限制的大小到堆，默认值为 100</p>
<h4>2.12.3.返回值</h4>
<p>返回一个服务器静态信息数组，失败时返回 FALSE</p>
<h3>2.13.Memcache::getServerStatus</h3>
<h4>2.13.1.说明</h4>
<p>int Memcache::getServerStatus ( string $host [, int $port ] )</p>
<p>通过输入的 host 及 port 来获取相应的服务器信息</p>
<h4>2.13.2.参数</h4>
<p>host 服务器域名或 IP</p>
<p>port 端口号，默认为 11211</p>
<h4>2.13.3.返回值</h4>
<p>返回服务器状态，0 为失败，其他情况返回非 0 数字</p>
<h4>2.13.4.范例</h4>
<pre>&lt;?php
$memcache = new Memcache;
$memcache-&gt;addServer('memcache_host', 11211);
echo $memcache-&gt;getServerStatus('memcache_host', 11211);

$memcache = memcache_connect('memcache_host', 11211);
echo memcache_get_server_status($memcache, 'memcache_host', 11211);
?&gt;</pre>
<h3>2.14.Memcache::getVersion</h3>
<h4>2.14.1.说明</h4>
<p>string Memcache::getVersion ( void )</p>
<p>获取服务器的版本号信息</p>
<h4>2.14.2.返回值</h4>
<p>成功返回服务器的版本号字符串，失败返回 FALSE</p>
<h4>2.14.3.范例</h4>
<pre>&lt;?php

$memcache = new Memcache;
$memcache-&gt;connect('memcache_host', 11211);
echo $memcache-&gt;getVersion();

$memcache = memcache_connect('memcache_host', 11211);
echo memcache_get_version($memcache);
?&gt;</pre>
<h3>2.15.Memcache::setCompressThreshold</h3>
<p>bool Memcache::setCompressThreshold ( int $threshold [, float $min_savings ] )</p>
<p>设置压缩极限</p>
<h4>2.15.2.参数</h4>
<p>threshold 设置控制自动压缩的变量长度的最小值</p>
<p>min_saving 指定的最低压缩比率，值必须介于 0 - 1 之间，默认为 0.2 代表 20% 的压缩比率</p>
<h4>2.15.3.返回值</h4>
<p>成功返回 TRUE，失败返回 FALSE。</p>
<h4>2.15.4.范例</h4>
<pre>&lt;?php
$memcache_obj = new Memcache;
$memcache_obj-&gt;addServer('memcache_host', 11211);
$memcache_obj-&gt;setCompressThreshold(20000, 0.2);

$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_set_compress_threshold($memcache_obj, 20000, 0.2);
?&gt;</pre>
<h3>2.16.Memcache::setServerParams</h3>
<h4>2.16.1.说明</h4>
<p>bool Memcache::setServerParams ( string $host [, int $port [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback ]]]]] )</p>
<p>Memcache version 2.1.0 后增加的函数，运行时设置服务器参数</p>
<h4>2.16.2.参数</h4>
<p>host           服务器域名或 IP</p>
<p>port 端口号，默认为 11211</p>
<p>timeout     超时连接失效的秒数，修改默认值 1 时要三思，有可能失去所有缓存方面的优势导致连接变得很慢</p>
<p>retry_interval     服务器连接失败时的重试频率，默认是 15 秒一次，如果设置为 -1 将禁止自动重试，当扩展中加载了 dynamically via dl() 时，无论本参数还是常连接设置参数都会失效。每一个失败的服务器在失效前都有独自的生存期，选择后端请求时会被跳过而不服务于请求。一个过期的连接将成功 的重新连接或者被标记为失败的连接等待下一次重试。这种效果就是说每一个 web server 的子进程在服务于页面时的重试连接都跟他们自己的重试频率有关。</p>
<p>status    控制服务器是否被标记为 online，设置这个参数为 FALSE 并设置 retry_interval 为 -1 可以使连接失败的服务器被放到一个描述不响应请求的服务器池子中，对这个服务器的请求将失败，接受设置为失败服务器的设置，默认参数为 TRUE，代表该服务器可以被定义为 online。</p>
<p>failure_callback    失败时的回调函数，函数的两个参数为失败服务器的 hostname 和 port</p>
<h4>2.16.3.返回值</h4>
<p>成功返回 TRUE，失败返回 FALSE。</p>
<h4>2.1***.范例</h4>
<pre>&lt;?php
function _callback_memcache_failure($host, $port)
{
print "memcache '$host:$port' failed";
}

$memcache = new Memcache;
// Add the server in offline mode
$memcache-&gt;addServer('memcache_host', 11211, FALSE, 1, 1, -1, FALSE);
// Bring the server back online
$memcache-&gt;setServerParams('memcache_host', 11211, 1, 15, TRUE, '_callback_memcache_failure');

$memcache_obj = memcache_connect('memcache_host', 11211);
memcache_set_server_params($memcache_obj, 'memcache_host', 11211, 1, 15, TRUE, '_callback_memcache_failure');
?&gt;</pre>
<h3>2.17.Memcache::increment</h3>
<h4>2.17.1.说明</h4>
<p><span style="color: #0000ff;">int</span> Memcache::increment ( string $key [, int $value ] )</p>
<p>给指定 key 的缓存变量一个增值，如果该变量不是数字时不会被转化为数字，这个增值将会加到该变量原有的数字之上，变量不存在不会新增变量，对于压缩存储的变量不要使 用本函数因为相应的取值方法会失败。</p>
<h4>2.17.2.参数</h4>
<p>key 缓存值的键</p>
<p>var 值，整型将直接存储，其他类型将被序列化存储</p>
<h4>2.17.3.返回值</h4>
<p>成功返回新的变量值，失败返回 FALSE。</p>
<h4>2.17.4.范例</h4>
<pre>&lt;?php
$memcache_obj = memcache_connect('memcache_host', 11211);
$current_value = memcache_increment($memcache_obj, 'counter', 2);

$memcache_obj = new Memcache;
$memcache_obj-&gt;connect('memcache_host', 11211);
$current_value = $memcache_obj-&gt;increment('counter', 3);
?&gt;</pre>
<h3>2.18.Memcache::decrement</h3>
<h4>2.18.2.说明</h4>
<p>int Memcache::decrement ( string $key [, int $value ] )</p>
<p>给指定 key 的缓存变量一个递减值，与 increment 操作类似，将在原有变量基础上减去这个值，该项的值将会在转化为数字后减去，新项的值不会小于 0，对于压缩存储的变量不要使用本函数因为相应的取值方法会失败。</p>
<h4>2.18.2.参数</h4>
<p>key 缓存值的键</p>
<p>var 值，整型将直接存储，其他类型将被序列化存储</p>
<h4>2.18.3.返回值</h4>
<p>成功返回新的变量值，失败返回 FALSE。</p>
<h4>2.18.4.范例</h4>
<pre>&lt;?php
$memcache_obj = memcache_connect('memcache_host', 11211);
$new_value = memcache_decrement($memcache_obj, 'test_item', 2);

$memcache_obj = new Memcache;
$memcache_obj-&gt;connect('memcache_host', 11211);
$new_value = $memcache_obj-&gt;decrement('test_item', 3);
?&gt;</pre>
<h3>2.19.memcache_debug</h3>
<h4>2.19.1.说明</h4>
<p>bool memcache_debug ( bool $on_off )</p>
<p>设置 memcache 的调试器是否开启，值为 TRUE 或 FALSE。 受影响于 php 安装时是否使用了 --enable-debug 选项，如果使用了该函数才会返回 TRUE，其他情况将始终返回 FALSE。</p>
<h4>2.19.2.参数</h4>
<p>on_off 设置调试模式是否开启，TRUE 为开启，FALSE 为关闭</p>
<h4>2.19.3.返回值</h4>
<p>php 安装时如果使使用了 --enable-debug 选项返回 TRUE，否则将返回 FALSE。</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="memcached完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109">
                        <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="Memcache基础教程" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8986&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="windows环境下memcache服务器使用经验" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109">
                        <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;">windows环境下memcache服务器使用经验</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="windows下memcached的安装（转载）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F406&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109">
                        <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;">windows下memcached的安装（转载）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/8109/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>windows环境下memcache服务器使用经验</title>
		<link>http://blog.haohtml.com/archives/8098</link>
		<comments>http://blog.haohtml.com/archives/8098#comments</comments>
		<pubDate>Wed, 23 Mar 2011 06:06:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[服务器类]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=8098</guid>
		<description><![CDATA[将memcache服务器安装包解压到C:\memcached文件夹后，使用cmd命令窗口安装。 1&#62;开始&#62;运行:CMD(确定) 2&#62;cd C:\memcached（回车） 3&#62;memcached -d install(回车 这步执行安装) 4&#62;memcached -d start(回车 这步执行启动memcache服务器，默认分配64M内存，使用11211端口) 此时memcache服务器已经可以正常使用了。 memcache服务器安全： Memcache服务器端都是直接通过客户端连接后直接操作，没有任何的验证过程，这样如果服务器是直接暴露在互联网上的话是比较危险，轻则数据泄 露被其他无关人员查看，重则服务器被入侵，况且里面可能存在一些我们未知的bug或者是缓冲区溢出的情况，这些都是我们未知的，所以危险性是可以预见的。 为了安全起见，做两点建议，能够稍微的防止黑客的入侵或者数据的泄露。 现在就关于修改memcache服务器配置的问题说明如下： 1&#62;用内网ip的方式提供web应用服务器调用，不允许直接通过外网调用，如将memcache服务器放在192.168.1.55的服务器上 2&#62;修改端口，如改为11200 3&#62;分配内存，如分配1024M(1G内存) 方法如下： 1&#62;开始&#62;运行:CMD(确定) 2&#62;cd C:\memcached（回车） 3&#62;memcached -m 1024 -p 11200 -l 192.168.1.55(回车) 注意，此时命令行不会回到C:\memcached&#62;状态，并且实际上memcache服务器悄悄变为stop状态了。此窗口不可以关闭。新开一个cmd窗口 4&#62;开始&#62;运行:CMD(确定) 5&#62;cd C:\memcached（回车） 6&#62;memcached -d start(回车)可以关闭此cmd窗口。 此时可以使用新配置的memcache服务器了。 &#160; 上述方法虽然解决了修改默认配置的问题，但是始终会有一个cmd窗口不可以关闭，否则就回到11211端口的默认配置。 更好的解决方案是通过修改服务的注册表配置： 1&#62;开始&#62;运行：regedit(回车) 2&#62;在注册表中找到：HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server 3&#62;默认的ImagePath键的值是："c:\memcached\memcached.exe" -d runservice，改为："c:\memcached\memcached.exe" -d runservice -m 512 -p  11200 [...]<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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098">
                        <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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3955&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098">
                        <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与memcache的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F392&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Memcache使用方法（PHP脚本）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098">
                        <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使用方法（PHP脚本）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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>将memcache服务器安装包解压到C:\memcached文件夹后，使用cmd命令窗口安装。</p>
<p>1&gt;开始&gt;运行:CMD(确定)</p>
<p>2&gt;cd C:\memcached（回车）</p>
<p>3&gt;memcached -d install(回车 这步执行安装)</p>
<p>4&gt;memcached -d start(回车 这步执行启动memcache服务器，默认分配64M内存，使用11211端口)</p>
<p>此时memcache服务器已经可以正常使用了。</p>
<p><strong>memcache服务器安全：</strong></p>
<p>Memcache服务器端都是直接通过客户端连接后直接操作，没有任何的验证过程，这样如果服务器是直接暴露在互联网上的话是比较危险，轻则数据泄 露被其他无关人员查看，重则服务器被入侵，况且里面可能存在一些我们未知的bug或者是缓冲区溢出的情况，这些都是我们未知的，所以危险性是可以预见的。 为了安全起见，做两点建议，能够稍微的防止黑客的入侵或者数据的泄露。</p>
<p>现在就关于修改memcache服务器配置的问题说明如下：</p>
<p>1&gt;用内网ip的方式提供web应用服务器调用，不允许直接通过外网调用，如将memcache服务器放在192.168.1.55的服务器上</p>
<p>2&gt;修改端口，如改为11200</p>
<p>3&gt;分配内存，如分配1024M(1G内存)<span id="more-8098"></span></p>
<p><strong>方法如下：</strong></p>
<p>1&gt;开始&gt;运行:CMD(确定)</p>
<p>2&gt;cd C:\memcached（回车）</p>
<p>3&gt;memcached -m 1024 -p 11200 -l 192.168.1.55(回车)</p>
<p>注意，此时命令行不会回到C:\memcached&gt;状态，并且实际上memcache服务器悄悄变为stop状态了。此窗口不可以关闭。新开一个cmd窗口</p>
<p>4&gt;开始&gt;运行:CMD(确定)</p>
<p>5&gt;cd C:\memcached（回车）</p>
<p>6&gt;memcached -d start(回车)可以关闭此cmd窗口。</p>
<p>此时可以使用新配置的memcache服务器了。</p>
<p>&nbsp;</p>
<p>上述方法虽然解决了修改默认配置的问题，但是始终会有一个cmd窗口不可以关闭，否则就回到11211端口的默认配置。</p>
<p>更好的解决方案是通过修改服务的注册表配置：</p>
<p>1&gt;开始&gt;运行：regedit(回车)</p>
<p>2&gt;在注册表中找到：HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server</p>
<p>3&gt;默认的ImagePath键的值是："c:\memcached\memcached.exe" -d  runservice，改为："c:\memcached\memcached.exe" -d runservice -m 512 -p   11200 -l 192.168.1.55（确定，关闭注册表）</p>
<p>4&gt;我的电脑（右键）&gt;管理&gt;服务 找到memcache的服务，重新启动一次即可生效。</p>
<p>此时，同网段内的电脑仍然可以利用这台memcache服务器，我们限定指定的web应用服务器才能够使用，通过防火墙的方式。如只允许 192.168.1.2这台Web服务器对Memcache服务器的访问，能够有效的阻止一些非法访问，相应的也可以增加一些其他的规则来加强安全性，这 个可以根据自己的需要来做。</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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098">
                        <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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F3955&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098">
                        <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与memcache的区别" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F392&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="Memcache使用方法（PHP脚本）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8098">
                        <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使用方法（PHP脚本）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/8098/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memcache使用方法（PHP脚本）</title>
		<link>http://blog.haohtml.com/archives/8095</link>
		<comments>http://blog.haohtml.com/archives/8095#comments</comments>
		<pubDate>Wed, 23 Mar 2011 06:02:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[服务器类]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=8095</guid>
		<description><![CDATA[Memcache是danga.com的一个项目，最早是为 LiveJournal 服务的，目前全世界不少人使用这个缓存项目来构建自己大负载的网站，来分担数据库的压力。它可以应对任意多个连接，使用非阻塞的网络IO。由于它的工作机 制是在内存中开辟一块空间，然后建立一个HashTable，Memcached自管理这些HashTable。Memcache官方网 站：http://www.danga.com/memcached，更多详细的信息可以来这里了解。 为什么会有Memcache和memcached两种名称？其实Memcache是这个项目的名称，而memcached是它服务器端的主程序文件名，知道我的意思了把~~~~。一个是项目名称，一个是主程序文件名，在网上看到了很多人不明白，于是混用了。 Memcache的安装 分为两个过程：memcache服务器端的安装和memcached客户端的安装。 所谓服务器端的安装就是在服务器（一般都是linux系统）上安装Memcache实现数据的存储。 所谓客户端的安装就是指php（或者其他程序，Memcache还有其他不错的api接口提供）去使用服务器端的Memcache提供的函数，需要php添加扩展。 PHP的Memcache 01 &#60;?php 02 //连接 03 $mem = new Memcache; 04 $mem-&#62;connect("db.nowamagic.net", 12000); 05 06 //保存数据 07 $mem-&#62;set('key1', 'This is first value', 0, 60); 08 $val = $mem-&#62;get('key1'); 09 echo "Get key1 value: " . $val ."&#60;br /&#62;"; 10 11 //替换数据 12 $mem-&#62;replace('key1', 'This is [...]<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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095">
                        <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="Memcache的安全性" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8085&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095">
                        <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 memcache手册</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="windows下memcached的安装（转载）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F406&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095">
                        <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;">windows下memcached的安装（转载）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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>
<p>Memcache是danga.com的一个项目，最早是为 LiveJournal  服务的，目前全世界不少人使用这个缓存项目来构建自己大负载的网站，来分担数据库的压力。它可以应对任意多个连接，使用非阻塞的网络IO。由于它的工作机 制是在内存中开辟一块空间，然后建立一个HashTable，Memcached自管理这些HashTable。Memcache官方网 站：http://www.danga.com/memcached，更多详细的信息可以来这里了解。</p>
<p>为什么会有Memcache和memcached两种名称？其实Memcache是这个项目的名称，而memcached是它服务器端的主程序文件名，知道我的意思了把~~~~。一个是项目名称，一个是主程序文件名，在网上看到了很多人不明白，于是混用了。</p>
<h4>Memcache的安装</h4>
<p>分为两个过程：memcache服务器端的安装和memcached客户端的安装。</p>
<p>所谓服务器端的安装就是在服务器（一般都是linux系统）上安装Memcache实现数据的存储。</p>
<p>所谓客户端的安装就是指php（或者其他程序，Memcache还有其他不错的api接口提供）去使用服务器端的Memcache提供的函数，需要php添加扩展。<span id="more-8095"></span></p>
<h4>PHP的Memcache</h4>
<div id="highlighter_4526">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>01</code></td>
<td><code>&lt;?php</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>02</code></td>
<td><code> </code><code>//连接</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>03</code></td>
<td><code> </code><code>$mem</code> <code>= </code><code>new</code> <code>Memcache;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>04</code></td>
<td><code> </code><code>$mem</code><code>-&gt;connect(</code><code>"db.nowamagic.net"</code><code>, 12000);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>05</code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>06</code></td>
<td><code> </code><code>//保存数据</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>07</code></td>
<td><code> </code><code>$mem</code><code>-&gt;set(</code><code>'key1'</code><code>, </code><code>'This is first value'</code><code>, 0, 60);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>08</code></td>
<td><code> </code><code>$val</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key1'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>09</code></td>
<td><code> </code><code>echo</code> <code>"Get key1 value: "</code> <code>. </code><code>$val</code> <code>.</code><code>"&lt;br /&gt;"</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>10</code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>11</code></td>
<td><code> </code><code>//替换数据</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>12</code></td>
<td><code> </code><code>$mem</code><code>-&gt;replace(</code><code>'key1'</code><code>, </code><code>'This is replace value'</code><code>, 0, 60);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>13</code></td>
<td><code> </code><code>$val</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key1'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>14</code></td>
<td><code> </code><code>echo</code> <code>"Get key1 value: "</code> <code>. </code><code>$val</code> <code>. </code><code>"&lt;br /&gt;"</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>15</code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>16</code></td>
<td><code> </code><code>//保存数组</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>17</code></td>
<td><code> </code><code>$arr</code> <code>= </code><code>array</code><code>(</code><code>'aaa'</code><code>, </code><code>'bbb'</code><code>, </code><code>'ccc'</code><code>, </code><code>'ddd'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>18</code></td>
<td><code> </code><code>$mem</code><code>-&gt;set(</code><code>'key2'</code><code>, </code><code>$arr</code><code>, 0, 60);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>19</code></td>
<td><code> </code><code>$val2</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key2'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>20</code></td>
<td><code> </code><code>echo</code> <code>"Get key2 value: "</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>21</code></td>
<td><code> </code><code>print_r(</code><code>$val2</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>22</code></td>
<td><code> </code><code>echo</code> <code>"&lt;br /&gt;"</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>23</code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>24</code></td>
<td><code> </code><code>//删除数据</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>25</code></td>
<td><code> </code><code>$mem</code><code>-&gt;</code><code>delete</code><code>(</code><code>'key1'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>26</code></td>
<td><code> </code><code>$val</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key1'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>27</code></td>
<td><code> </code><code>echo</code> <code>"Get key1 value: "</code> <code>. </code><code>$val</code> <code>. </code><code>"&lt;br /&gt;"</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>28</code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>29</code></td>
<td><code> </code><code>//清除所有数据</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>30</code></td>
<td><code> </code><code>$mem</code><code>-&gt;</code><code>flush</code><code>();</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>31</code></td>
<td><code> </code><code>$val2</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key2'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>32</code></td>
<td><code> </code><code>echo</code> <code>"Get key2 value: "</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>33</code></td>
<td><code> </code><code>print_r(</code><code>$val2</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>34</code></td>
<td><code> </code><code>echo</code> <code>"&lt;br /&gt;"</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>35</code></td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>36</code></td>
<td><code> </code><code>//关闭连接</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>37</code></td>
<td><code> </code><code>$mem</code><code>-&gt;close();</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>38</code></td>
<td><code>?&gt;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>如果正常的话，浏览器将输出：</p>
<div id="highlighter_348799">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>Get key1 value: This is first value</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>Get key1 value: This is replace value</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>Get key2 value: Array ( [0] =&gt; aaa [1] =&gt; bbb [2] =&gt; ccc [3] =&gt; ddd )</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>Get key1 value:</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>Get key2 value:</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<h4>程序代码分析</h4>
<p>初始化一个Memcache的对象：$mem = new Memcache;</p>
<p>连接到我们的Memcache服务器端，第一个参数是服务器的IP地址，也可以是主机名，第二个参数是Memcache的开放的端口：$mem-&gt;connect("192.168.0.200", 12000);</p>
<p>保存一个数据到Memcache服务器上，第一个参数是数据的key，用来定位一个数据，第二个参数是需要保存的数据内容，这里是一个字符 串，第三个参数是一个标记，一般设置为0或者MEMCACHE_COMPRESSED就行了，第四个参数是数据的有效期，就是说数据在这个时间内是有效 的，如果过去这个时间，那么会被Memcache服务器端清除掉这个数据，单位是秒，如果设置为0，则是永远有效，我们这里设置了60，就是一分钟有效时 间：$mem-&gt;set(‘key1‘, ‘This is first value’, 0, 60);</p>
<p>从Memcache服务器端获取一条数据，它只有一个参数，就是需要获取数据的key，我们这里是上一步设置的key1，现在获取这个数据后输出输出：</p>
<div id="highlighter_31675">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>$val</code> <code>= </code><code>$mem</code><code>-&gt;get(’key1′); </code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>echo</code> <code>"Get key1 value: "</code> <code>. </code><code>$val</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>现在是使用replace方法来替换掉上面key1的值，replace方法的参数跟set是一样的，不过第一个参数key1是必须是要替换数据内容的key，最后输出了：</p>
<div id="highlighter_842784">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>$mem</code><code>-&gt;replace(</code><code>'key1'</code><code>, </code><code>'This is replace value'</code><code>, 0, 60);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>$val</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key1'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>echo</code> <code>"Get key1 value: "</code> <code>. </code><code>$val</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>同样的，Memcache也是可以保存数组的，下面是在Memcache上面保存了一个数组，然后获取回来并输出：</p>
<div id="highlighter_719959">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>$arr</code> <code>= </code><code>array</code><code>(</code><code>'aaa'</code><code>, </code><code>'bbb'</code><code>, </code><code>'ccc'</code><code>, </code><code>'ddd'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>$mem</code><code>-&gt;set(</code><code>'key2'</code><code>, </code><code>$arr</code><code>, 0, 60);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>$val2</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key2'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>print_r(</code><code>$val2</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>现在删除一个数据，使用delte接口，参数就是一个key，然后就能够把Memcache服务器这个key的数据删除，最后输出的时候没有结果：</p>
<div id="highlighter_37589">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>$mem</code><code>-&gt;</code><code>delete</code><code>(</code><code>'key1'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>$val</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key1'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>echo</code> <code>"Get key1 value: "</code> <code>. </code><code>$val</code> <code>. </code><code>"&lt;br /&gt;"</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>最后我们把所有的保存在Memcache服务器上的数据都清除，会发现数据都没有了，最后输出key2的数据为空，最后关闭连接：</p>
<div id="highlighter_753455">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code>$mem</code><code>-&gt;</code><code>flush</code><code>();</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code>$val2</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key2'</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code>echo</code> <code>"Get key2 value: "</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code>print_r(</code><code>$val2</code><code>);</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>5</code></td>
<td><code>echo</code> <code>"&lt;br /&gt;"</code><code>;</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<h4>Memcache的使用</h4>
<p>使用Memcache的网站一般流量都是比较大的，为了缓解数据库的压力，让Memcache作为一个缓存区域，把部分信息保存在内存中，在 前端能够迅速的进行存取。那么一般的焦点就是集中在如何分担数据库压力和进行分布式，毕竟单台Memcache的内存容量的有限的。我这里简单提出我的个 人看法，未经实践，权当参考。</p>
<p><strong>分布式应用</strong></p>
<p>Memcache本来支持分布式，我们客户端稍加改造，更好的支持。我们的key可以适当进行有规律的封装，比如以user为主的网站来 说，每个用户都有User  ID，那么可以按照固定的ID来进行提取和存取，比如1开头的用户保存在第一台Memcache服务器上，以2开头的用户的数据保存在第二胎 Mecache服务器上，存取数据都先按照User ID来进行相应的转换和存取。</p>
<p>但是这个有缺点，就是需要对User ID进行判断，如果业务不一致，或者其他类型的应用，可能不是那么合适，那么可以根据自己的实际业务来进行考虑，或者去想更合适的方法。</p>
<p><strong>减少数据库压力</strong></p>
<p>这个算是比较重要的，所有的数据基本上都是保存在数据库当中的，每次频繁的存取数据库，导致数据库性能极具下降，无法同时服务更多的用户， 比如MySQL，特别频繁的锁表，那么让Memcache来分担数据库的压力吧。我们需要一种改动比较小，并且能够不会大规模改变前端的方式来进行改变目 前的架构。</p>
<p>我考虑的一种简单方法：</p>
<p>后端的数据库操作模块，把所有的Select操作提取出来（update/delete/insert不管），然后把对应的SQL进行相应 的hash算法计算得出一个hash数据key（比如MD5或者SHA），然后把这个key去Memcache中查找数据，如果这个数据不存在，说明还没 写入到缓存中，那么从数据库把数据提取出来，一个是数组类格式，然后把数据在set到Memcache中，key就是这个SQL的hash值，然后相应的 设置一个失效时间，比如一个小时，那么一个小时中的数据都是从缓存中提取的，有效减少数据库的压力。缺点是数据不实时，当数据做了修改以后，无法实时到前 端显示，并且还有可能对内存占用比较大，毕竟每次select出来的数据数量可能比较巨大，这个是需要考虑的因素。</p>
<h4>Memcache的安全</h4>
<p>我们上面的Memcache服务器端都是直接通过客户端连接后直接操作，没有任何的验证过程，这样如果服务器是直接暴露在互联网上的话是比 较危险，轻则数据泄露被其他无关人员查看，重则服务器被入侵，因为Mecache是以root权限运行的，况且里面可能存在一些我们未知的bug或者是缓 冲区溢出的情况，这些都是我们未知的，所以危险性是可以预见的。为了安全起见，我做两点建议，能够稍微的防止黑客的入侵或者数据的泄露。</p>
<p><strong>内网访问</strong></p>
<p>最好把两台服务器之间的访问是内网形态的，一般是Web服务器跟Memcache服务器之间。普遍的服务器都是有两块网卡，一块指向互联 网，一块指向内网，那么就让Web服务器通过内网的网卡来访问Memcache服务器，我们Memcache的服务器上启动的时候就监听内网的IP地址和 端口，内网间的访问能够有效阻止其他非法的访问。</p>
<blockquote><p># memcached -d -m 1024 -u root -l 192.168.0.200 -p 11211 -c 1024 -P /tmp/memcached.pid</p></blockquote>
<p>Memcache服务器端设置监听通过内网的192.168.0.200的ip的11211端口，占用1024MB内存，并且允许最大1024个并发连接。</p>
<p><strong>设置防火墙</strong></p>
<p>防火墙是简单有效的方式，如果却是两台服务器都是挂在网的，并且需要通过外网IP来访问Memcache的话，那么可以考虑使用防火墙或者 代理程序来过滤非法访问。一般我们在Linux下可以使用iptables或者FreeBSD下的ipfw来指定一些规则防止一些非法的访问，比如我们可 以设置只允许我们的Web服务器来访问我们Memcache服务器，同时阻止其他的访问。</p>
<div id="highlighter_778230">
<div>
<div>
<table>
<tbody>
<tr>
<td><code>1</code></td>
<td><code># iptables -F</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>2</code></td>
<td><code># iptables -P INPUT DROP</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>3</code></td>
<td><code># iptables -A INPUT -p tcp -s 192.168.0.2 –dport 11211 -j ACCEPT</code></td>
</tr>
</tbody>
</table>
</div>
<div>
<table>
<tbody>
<tr>
<td><code>4</code></td>
<td><code># iptables -A INPUT -p udp -s 192.168.0.2 –dport 11211 -j ACCEPT</code></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>上面的iptables规则就是只允许192.168.0.2这台Web服务器对Memcache服务器的访问，能够有效的阻止一些非法访问，相应的也可以增加一些其他的规则来加强安全性，这个可以根据自己的需要来做。</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完全剖析" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F366&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095">
                        <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="Memcache的安全性" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8085&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095">
                        <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 memcache手册</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="windows下memcached的安装（转载）" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F406&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8095">
                        <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;">windows下memcached的安装（转载）</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/8095/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>freebsd+php+memcache、memcached安装和使用</title>
		<link>http://blog.haohtml.com/archives/8092</link>
		<comments>http://blog.haohtml.com/archives/8092#comments</comments>
		<pubDate>Wed, 23 Mar 2011 06:00:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[程序开发]]></category>
		<category><![CDATA[服务器类]]></category>
		<category><![CDATA[FreeBSD]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.haohtml.com/?p=8092</guid>
		<description><![CDATA[来源：http://www.lifecrunch.biz/archives/55 Memcache 介绍 memcache是一个高性能的分布式的内存对象缓存系统，通过在内存里维护一个统一的巨大的hash表，它能够用来存储各种格式的数据，包括图 像、视频、文件以及数据库检索的结果等。Memcache是danga.com的一个项目，最早是为 LiveJournal 服务的，最初为了加速 LiveJournal 访问速度而开发的，后来被很多大型的网站采用。目前全世界不少人使用这个缓存项目来构建自己大负载的网站，来分担数据库的压力。起初作 者编写它可能是为了提高动态网页应用，为了减轻数据库检索的压力，来做的这个缓存系统。它的缓存是一种分布式的，也就是可以允许不同主机上的多个用户同时 访问这个缓存系统， 这种方法不仅解决了共享内存只能是单机的弊端，同时也解决了数据库检索的压力，最大的优点是提高了访问获取数据的速度！基于memcache作者对分布式 cache的理解和解决方案。 memcache完全可以用到其他地方 比如分布式数据库， 分布式计算等领域。 Memcache官方网站：http://www.danga.com/memcached Memcached服务器端安装： freebsd下服务器端的memcached使用ports安装非常简单， cd /usr/ports/databases/memcached make install clean rehash 安装好memcache以后，编辑/etc/rc.conf文件,添加一行 memcached_enable="YES" 然后保存退出。memcache会随着开机自动启动，手动启动的命令是： /usr/local/etc/rc.d/memcached start 好了，现在memcache已经安装并启动完毕了。 手动调整服务命令如下: memcached  -p 11211 -l 172.16.236.150 -d -u nobody -P  /var/run/memcached/memcached.pid -m  64M -c 1024 -vv 几个参数的解释： -p memcached监听的TCP端口 -l 监听的ip地址，172.16.236.150是我服务器的IP地址，如果你需要多个服务器都能够读取这台memcached的缓存数据，那么就必须设定 这个ip -d 以daemon方式运行，将程序放入后台 [...]<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="[精典教程]freebsd下安装mysql,apache,php,phpmyadmin记录" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F435&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092">
                        <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/4022261.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;">[精典教程]freebsd下安装mysql,apache,php,phpmyadmin记录</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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="freeBSD 安装php扩展：iconv" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F7001&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092">
                        <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/4022307.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;">freeBSD 安装php扩展：iconv</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="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092">
                        <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 memcache手册</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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>
<p>来源：<a href="http://www.lifecrunch.biz/archives/55">http://www.lifecrunch.biz/archives/55</a></p>
<h2>Memcache 介绍</h2>
<p>memcache是一个高性能的分布式的内存对象缓存系统，通过在内存里维护一个统一的巨大的hash表，它能够用来存储各种格式的数据，包括图 像、视频、文件以及数据库检索的结果等。Memcache是danga.com的一个项目，最早是为 LiveJournal 服务的，最初为了加速  LiveJournal  访问速度而开发的，后来被很多大型的网站采用。目前全世界不少人使用这个缓存项目来构建自己大负载的网站，来分担数据库的压力。起初作  者编写它可能是为了提高动态网页应用，为了减轻数据库检索的压力，来做的这个缓存系统。它的缓存是一种分布式的，也就是可以允许不同主机上的多个用户同时  访问这个缓存系统，  这种方法不仅解决了共享内存只能是单机的弊端，同时也解决了数据库检索的压力，最大的优点是提高了访问获取数据的速度！基于memcache作者对分布式  cache的理解和解决方案。 memcache完全可以用到其他地方 比如分布式数据库， 分布式计算等领域。</p>
<p>Memcache官方网站：<a href="http://www.danga.com/memcached" target="_blank"><span style="color: #2970a6;">http://www.danga.com/memcached</span></a></p>
<h2>Memcached服务器端安装：</h2>
<p>freebsd下服务器端的memcached使用ports安装非常简单，<span id="more-8092"></span></p>
<div>
<div>
<pre>cd /usr/ports/databases/memcached
make install clean
rehash</pre>
</div>
</div>
<pre>安装好memcache以后，编辑/etc/rc.conf文件,添加一行
<strong>memcached_enable="YES"
</strong>然后保存退出。memcache会随着开机自动启动，手动启动的命令是：
<strong>/usr/local/etc/rc.d/memcached start
</strong>好了，现在memcache已经安装并启动完毕了。
手动调整服务命令如下:
<span style="color: #0000ff;">memcached  -p 11211 -l 172.16.236.150 -d -u nobody -P  /var/run/memcached/memcached.pid -m  64M -c 1024 -vv</span>
</pre>
<p><strong>几个参数的解释：</strong><br />
-p memcached监听的TCP端口<br />
-l 监听的ip地址，172.16.236.150是我服务器的IP地址，如果你需要多个服务器都能够读取这台memcached的缓存数据，那么就必须设定 这个ip<br />
-d 以daemon方式运行，将程序放入后台<br />
-u memcached的运行用户，我设定的是nobody，memcache默认不允许以root用户登录<br />
-P memcached的pid文件路径<br />
-m memcached可以使用的最大内存数量<br />
-c memcached同时可以接受的最大的连接数<br />
如果你希望以socket方式来访问memcached，那么在启动的时候就必须去掉 -l和-p参数，并加上-s参数：<br />
-s memcached的socket文件路径</p>
<p>-vv显示debug信息</p>
<h2>PHP Memecache 客户端的安装</h2>
<p>PHP下使用Memcache 有3种方式。<br />
1. 使用memcache 扩展 手册上有说明：http://php.net/manual/en/book.memcache.php<br />
2. 使用memcached扩展 手册上有说明：http://php.net/manual/en/book.memcached.php<br />
3. 使用memcache-client.php类库. 网上没有找到具体出处，暂且提供我珍藏的.(稍候上传..)<br />
这三种方式都可以和Memcache缓存系统交互.但是有一些细微差别。</p>
<p><strong>第一种</strong>，常见方式，这个扩展在Win环境下使用方便只要去下载一个memcache.dll 配置一下 php.ini就可以了。win32平台扩展的下载地址<a href="http://www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll" target="_blank">www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll</a> for PHP 5.2.*</p>
<p>当然freebsd下配置也很简单。</p>
<p>freebsd下安装：</p>
<p>下载 memcache:<a href="http://pecl.php.net/get/memcache-2.2.5.tgz" target="_blank">http://pecl.php.net/get/memcache-2.2.5.tgz</a></p>
<blockquote><p><code><span style="font-family: NSimsun;">tar -zvxf memcache-2.2.5.tgz</span></code></p>
<p>cd memcache-2.2.5</p>
<p>phpize</p>
<p>./configure</p>
<p>make</p>
<p>make install</p></blockquote>
<p>最后在<span style="color: #0000ff;">/usr/local/etc/php/extension.ini</span>中加上</p>
<blockquote><p>extension=”memcache.so”</p></blockquote>
<p>安装完毕之后重新启动apache，可以用下面的测试代码检查memcache工作是否正常：</p>
<div>
<blockquote>
<pre>&lt;?php

/* OO API */

$memcache = new Memcache;
$memcache-&gt;addServer('192.168.30.192', 11211);
$memcache-&gt;addServer('192.168.30.191', 11211);

$tmp_object = new stdClass;
$tmp_object-&gt;str_attr = 'test';
$tmp_object-&gt;int_attr = 123;

$memcache-&gt;set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)&lt;br/&gt;\n";

$get_result = $memcache-&gt;get('key');
echo "Data from the cache:&lt;br/&gt;\n";

var_dump($get_result);
 ?&gt;</pre>
</blockquote>
</div>
<p><strong>第二种方式</strong>，memcached 的版本比较新，并且使用的是 libmemcached 库。libmemcached  被认为做过更好的优化，应该比 php only 版本的 memcache 有着更高的性能。差别比较大的一点是，memcached 支持  Binary Protocol，而 memcache 不支持，这也意味着 memcached  会有更高的性能。但安装配置起来也比较麻烦.该扩展目前我没有看到能支持Win平台的。所以只能在freebsd下自行编译安装了<br />
以下是linux安装：<br />
1.首先安装libmemcached请参考：<a href="http://www.phpup.net/post/54" target="_blank">http://www.phpup.net/post/54</a></p>
<p>2.下载php的memcached:<a href="http://pecl.php.net/get/memcached-1.0.1.tgz" target="_blank">http://pecl.php.net/get/memcached-1.0.1.tgz</a></p>
<p>3.安装：</p>
<p>1) [root@localhost soft]# tar -zvxf phpmemcached-1.0.1.tgz</p>
<p>2)cd memcached-1.0.1</p>
<p>3) 在此目录下运行phpize(此处phpize是要安装php的开发包后才会有的，因为phpize是在/usr/bin目录下，所以可直接运行,完了会 在此目录下生成configure等文件)</p>
<p>4)在此目录下运行./configure –with-php-config=/usr/local/php/bin/php-config –with-libmemcached-dir=/usr/local/</p>
<p>5)运行 make &amp;&amp; make install （实际安装过程中，这一步编译出错）</p>
<p>6)在/etc/php.ini中加入 extension=”memcached.so” 命令行：</p>
<blockquote><p>echo “extension = memcached.so” &gt;&gt; /usr/local/php/etc/php.ini</p></blockquote>
<p>7）重启apache</p>
<p><strong>第三种方式</strong>,使用简单方便。适合新手使用，无须任何php配置，只要一个包含就可以使用了.并非底层，所以性能上面自然是稍逊不少。<br />
以下是使用：</p>
<blockquote><p><code><span style="font-family: NSimsun;">include "memcache-client.php";</span></code></p>
<p><code><span style="font-family: NSimsun;">文件下载地址: </span><a href="http://www.lifecrunch.biz/wp-content/uploads/2010/06/memcached-client-php-0.1.2.tar.gz"><span style="color: #2970a6; font-family: NSimsun;">memcached-client-php-0.1.2.tar</span></a><br />
<span style="font-family: NSimsun;"><br />
</span></code></p></blockquote>
<p>个人推荐使用第一种 memcached 扩展，毕竟性能不错，新手的话推荐使用 第三种 客户端类库，方便实用。第二种方式没有尝试成功，不知道是什么原因，编译安装出错，以后有空再解决吧。</p>
<p>有关Memcache , Memcached 和 MemcacheDB三者的区别:<a href="http://blog.haohtml.com/archives/8089" target="_blank">http://blog.haohtml.com/archives/8089</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="[精典教程]freebsd下安装mysql,apache,php,phpmyadmin记录" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F435&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092">
                        <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/4022261.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;">[精典教程]freebsd下安装mysql,apache,php,phpmyadmin记录</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?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F395&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092">
                        <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>
                <td width="102" valign="top" style="padding: 5px !important; margin: 0 !important; border-left: 1px solid #DDDDDD !important;">
                    <a target="_blank" title="freeBSD 安装php扩展：iconv" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F7001&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092">
                        <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/4022307.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;">freeBSD 安装php扩展：iconv</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="php memcache手册" style="text-decoration: none !important; cursor: pointer !important;" href="http://app.wumii.com/ext/redirect?url=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8109&from=http%3A%2F%2Fblog.haohtml.com%2Farchives%2F8092">
                        <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 memcache手册</font>
                    </a>
                </td>
        </tr>
    
    <tr>
        <td colspan="4" align="right">
            <a style="text-decoration: none !important;" href="http://www.wumii.com/widget/relatedItems" 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/8092/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

