<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[缓存集群]php memcached缓存集群--转]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Mon, 01 Dec 2014 06:50:22 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：cache有多台，addServer，这块里有一个算法不是太好，在腾讯时在做云存储时有兄弟改成了环形算法，更能最大限度的在添加机器后影响小，这块我目前没几台机器没有这个必要，自己写还不如这位兄弟写的，特转。<br/><br/>一、需求描述<br/>一linode xen vps 1G RAM，40+sites，IO频繁，openfiles ulimit已65535<br/>US时间访问量大增,nginx不定时502<br/>二、解决办法<br/>pages cache化,但这一台vps内存不够用，于是把闲置的几台服务器都装上memcached做缓存集群<br/>再根据网络延迟设定权重<br/>memcached server官方: http://memcached.org/<br/>三、代码<br/>访问memcached server需要php支持，可以选择memcached库和memcache库，注意2者名字的区别<br/>详细对比：https://code.google.com/p/memcached/wiki/PHPClientComparison<br/>我采用的是php memcache: http://php.net/manual/en/book.memcache.php 因为memcache不依赖libmemcached<br/>//集群数组<br/><textarea name="code" class="php" rows="15" cols="100">
$memserver = array(
&nbsp;&nbsp;&nbsp;&nbsp;array( &#039;host&#039; =&gt; &#039;fast1.xxx.com&#039; , &#039;port&#039; =&gt; 13579 , &#039;weight&#039; =&gt; 40 ),
&nbsp;&nbsp;&nbsp;&nbsp;array( &#039;host&#039; =&gt; &#039;fast2.xxx.com&#039; , &#039;port&#039; =&gt; 13579 , &#039;weight&#039; =&gt; 20 ),
&nbsp;&nbsp;&nbsp;&nbsp;array( &#039;host&#039; =&gt; &#039;fast3.xxx.com&#039; , &#039;port&#039; =&gt; 13579 , &#039;weight&#039; =&gt; 20 ),
 
&nbsp;&nbsp;&nbsp;&nbsp;array( &#039;host&#039; =&gt; &#039;slow1.xxx.com&#039; , &#039;port&#039; =&gt; 24680 , &#039;weight&#039; =&gt; 5 ),
&nbsp;&nbsp;&nbsp;&nbsp;array( &#039;host&#039; =&gt; &#039;slow2.xxx.com&#039; , &#039;port&#039; =&gt; 24680 , &#039;weight&#039; =&gt; 5 ),
 
&nbsp;&nbsp;&nbsp;&nbsp;array( &#039;host&#039; =&gt; &#039;local.xxx.com&#039; , &#039;port&#039; =&gt; 11211 , &#039;weight&#039; =&gt; 10 )
);
</textarea><br/>键值读、写、删就没什么说的了直接按文档sample写即可<br/>考虑到以后的移植问题，记得首先检测环境是否支持memcache: class_exists(‘Memcache’)<br/>下面是我的实现，因为特定需求，几个函数写的相对独立，memcache也没做全局实例化。<br/>你可以根据自己需求把memcache在全局实例化，优化操作。<br/><textarea name="code" class="php" rows="15" cols="100">
//---get content from memcache---
function getCache( $key )
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;Global $memserver;
&nbsp;&nbsp;&nbsp;&nbsp;$ret = false;
&nbsp;&nbsp;&nbsp;&nbsp;$key = md5( $key );
&nbsp;&nbsp;&nbsp;&nbsp;$mc&nbsp;&nbsp;= new Memcache();
&nbsp;&nbsp;&nbsp;&nbsp;foreach ( $memserver as $s )
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mc-&gt;addServer( $s[&#039;host&#039;], $s[&#039;port&#039;], FALSE, $s[&#039;weight&#039;] );
&nbsp;&nbsp;&nbsp;&nbsp;$ret = $mc-&gt;get( $key );
&nbsp;&nbsp;&nbsp;&nbsp;$mc-&gt;close();
&nbsp;&nbsp;&nbsp;&nbsp;unset( $mc );
&nbsp;&nbsp;&nbsp;&nbsp;return $ret;
&#125;
 
//---set content from memcache---
function setCache( $key, $val )
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;Global $memserver;
&nbsp;&nbsp;&nbsp;&nbsp;$ret = false;
&nbsp;&nbsp;&nbsp;&nbsp;$key = md5( $key );
&nbsp;&nbsp;&nbsp;&nbsp;$mc&nbsp;&nbsp;= new Memcache();
&nbsp;&nbsp;&nbsp;&nbsp;foreach ( $memserver as $s )
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mc-&gt;addServer( $s[&#039;host&#039;], $s[&#039;port&#039;], FALSE, $s[&#039;weight&#039;] );
&nbsp;&nbsp;&nbsp;&nbsp;if( isset($val[&#039;cached&#039;]) )
&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$val[&#039;cached&#039;] = 1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ret = $mc-&gt;set( $key , $val);
&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;$mc-&gt;close();
&nbsp;&nbsp;&nbsp;&nbsp;unset( $mc );
&nbsp;&nbsp;&nbsp;&nbsp;return $ret;
&#125;
 
//---delete content from memcache---
function delCache( $key )
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;Global $memserver;
&nbsp;&nbsp;&nbsp;&nbsp;$ret = false;
&nbsp;&nbsp;&nbsp;&nbsp;$key = md5( $key );
&nbsp;&nbsp;&nbsp;&nbsp;$mc&nbsp;&nbsp;= new Memcache();
&nbsp;&nbsp;&nbsp;&nbsp;foreach ( $memserver as $s )
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mc-&gt;addServer( $s[&#039;host&#039;], $s[&#039;port&#039;], FALSE, $s[&#039;weight&#039;] );
&nbsp;&nbsp;&nbsp;&nbsp;$ret = $mc-&gt;delete( $key , 0);
&nbsp;&nbsp;&nbsp;&nbsp;$mc-&gt;close();
&nbsp;&nbsp;&nbsp;&nbsp;unset( $mc );
&nbsp;&nbsp;&nbsp;&nbsp;return $ret;
&#125;
</textarea><br/><br/>摘自：http://www.6zou.net/tech/php-memcached-group.html
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [缓存集群]php memcached缓存集群--转]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>