<?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>服务 &#8211; LPC影子技术分享</title>
	<atom:link href="https://www.mudbest.com/tag/%E6%9C%8D%E5%8A%A1/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.mudbest.com</link>
	<description>行影不离,忘之却步.</description>
	<lastBuildDate>Mon, 29 Oct 2012 05:14:23 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.1</generator>
	<item>
		<title>ubuntu12.04下memcached安装与配置</title>
		<link>https://www.mudbest.com/ubuntu12-04%e4%b8%8bmemcached%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%ae/</link>
		
		<dc:creator><![CDATA[hkshadow]]></dc:creator>
		<pubDate>Mon, 29 Oct 2012 05:11:37 +0000</pubDate>
				<category><![CDATA[分享]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[安装]]></category>
		<category><![CDATA[服务]]></category>
		<guid isPermaLink="false">http://www.mudbest.com/?p=1625</guid>

					<description><![CDATA[其中包括memcache服务器端的安装和memcached客户端的安装。 所谓服务器端的安装就是在服务器（一般 ... <a title="ubuntu12.04下memcached安装与配置" class="read-more" href="https://www.mudbest.com/ubuntu12-04%e4%b8%8bmemcached%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%ae/" aria-label="阅读 ubuntu12.04下memcached安装与配置">阅读更多</a>]]></description>
										<content:encoded><![CDATA[<p>其中包括memcache服务器端的安装和memcached客户端的安装。<br />
所谓服务器端的安装就是在服务器（一般都是linux系统）上安装Memcache实现数据的存储</p>
<p>所谓客户端的安装就是指php（或者其他程序，memcache还有其他不错的api接口提供）去使用服务器端的memcache提供的函数，客户端需要php添加扩展。</p>
<p><strong>安装Memcache服务端</strong></p>
<pre class="brush: plain; title: ; notranslate">
sudo apt-get install memcached
</pre>
<p><span id="more-1625"></span><br />
安装完Memcache服务端以后，我们需要启动该服务：</p>
<pre class="brush: plain; title: ; notranslate">
memcached -d -m 128 -p 11111 -u root
</pre>
<p>这里需要说明一下memcached服务的启动参数：</p>
<pre class="brush: plain; title: ; notranslate">
-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用，单位MB。默认64MB
-M 内存耗尽时返回错误，而不是删除项
-c 最大同时连接数，默认是1024
-f 块大小增长因子，默认是1.25-n 最小分配空间，key+value+flags默认是48
-h 显示帮助
-P 是设置保存Memcache的pid文件
</pre>
<p>如果要杀掉memcached进程</p>
<pre class="brush: plain; title: ; notranslate">
kill 'cat /tmp/memcached.pid'
</pre>
<p>也可以启动多个守护进程，不过端口不能重复。<br />
重启web服务<br />
[/code lang=&#8221;text&#8221;]<br />
/etc/init.d/lighttpd -flighttpd.conf restart<br />
[/code]</p>
<p>Memcache环境测试：</p>
<pre class="brush: plain; title: ; notranslate">
telnet localhost 11211
//保存
set
good 32 0 10
helloworld
STORED
//取回
gets good
VALUE good 32 10 10
helloworld
END
//替换
replace good 32 0 10
worldhello
STORED
get good
VALUE good 32 10
worldhello
END
//尾部添加
append good 32 0 5
after
STORED
get good
VALUE good 32 15
worldhelloafter
END
//头部添加
prepend good 32 0 6
before
STORED
get good
VALUE good 32 21
beforeworldhelloafter
END
//删除
delete good
DELETED
get good
END
</pre>
<p><strong>安装memcache客户端（php5.3为示例）</strong></p>
<pre class="brush: plain; title: ; notranslate">
sudo apt-get install php5-memcache
</pre>
<p>安装完以后我们需要在php.ini里进行简单的配置,打开/etc/php5/apache2/php.ini文件在末尾添加如下内容：</p>
<pre class="brush: plain; title: ; notranslate">
cp /etc/php5/apache2/php.ini /etc/php5/apache2.php.ini.bak
vi /etc/php5/apache2/php.ini
</pre>
<p>在文件内容最下面增加以下配置信息</p>
<pre class="brush: plain; title: ; notranslate">
&#x5B;Memcache]
;是否在遇到错误时透明的向其他服务器进行故障转移
memcache.allow_failover = On
;接受和发送数据时最多尝试多少个服务器，只在打开memcache.allow_failover时生效。
memcache.max_failover_attempts = 20
;数据将俺按照此值设定的块大小进行转移。此值越小所需的额外网络传输越多。
;如果发现无法解释的速度降低，可以尝试将此值增加到32768。
memcache.chunk_size = 8192
;链接到memcached服务器时使用的默认TCP端口。
memcache.default.port = 11111
;控制将key映射到server的策略。默认值“standard”表示使用先前版本的老hash策略
;设为“consistent”可以允许在链接池中添加/删除服务器时不必重新计算key与server之间&gt;的映射关系。
;memcache.hash_strategy = &quot;standard&quot;
;控制将key映射到server的散列函数。默认值“crc32”使用CRC32算法，而“fnv”则表示使用FNV-1a算法。
;FNV-1a比CRC32速度稍低，但是散列效果更好。
;memcache.hash_function = &quot;crc32&quot;
</pre>
<p><a href="http://www.mudbest.com/ubuntu12-04%e4%b8%8bmemcached%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%ae/2012-10-26memcached/" rel="attachment wp-att-1627" target="_blank"><img fetchpriority="high" decoding="async" src="http://www.mudbest.com/wp-content/uploads/2012/10/2012-10-26memcached.png" alt="" title="2012-10-26memcached" width="1280" height="1024" class="alignnone size-full wp-image-1627" srcset="https://www.mudbest.com/wp-content/uploads/2012/10/2012-10-26memcached.png 1280w, https://www.mudbest.com/wp-content/uploads/2012/10/2012-10-26memcached-537x430.png 537w, https://www.mudbest.com/wp-content/uploads/2012/10/2012-10-26memcached-1024x819.png 1024w" sizes="(max-width: 1280px) 100vw, 1280px" /></a></p>
<p>最后，保存php.ini,执行sudo /etc/init.d/apache2 restart重启Apache。</p>
<p><strong>在PHP中使用memcache</strong></p>
<pre class="brush: sql; title: ; notranslate">
#mysql数据库部分：
CREATE TABLE `conn` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=gb2312 AUTO_INCREMENT=7 ;
</pre>
<pre class="brush: php; title: ; notranslate">
//PHP代码部分
&lt;form action=&quot;&quot; method=&quot;get&quot;&gt;
	&lt;input type=&quot;text&quot; name=&quot;keys&quot;&gt; 
	&lt;input type=&quot;submit&quot; value=&quot;提交&quot;&gt;
&lt;/form&gt;
&lt;?php
if (! empty ( $_GET &#x5B;'keys'] )) {
	$arr = mem_db ( $_GET &#x5B;'keys'] );
	print_r ( $arr );
}
function mem_db($str) {
	$m = new memcache ();
	$m-&gt;connect ( &quot;127.0.0.1&quot;, 11211 ) or die ( &quot;连接错误&quot; );
	$mi = base64_encode ( $str ); // 加密进来的数据
	$exp = $m-&gt;get ( $mi );
	if (empty ( $exp )) {
		$row = sql_db ( $str ); // 数据库php100
		$m-&gt;set ( $mi, $row, 0, 3600 );
	}
	
	return $m-&gt;get ( $mi );
}
function sql_db($str) {
	mysql_connect ( &quot;localhost&quot;, &quot;root&quot;, &quot;2222&quot; );
	mysql_select_db ( &quot;news&quot; );
	mysql_query ( &quot;set names 'GBK'&quot; );
	$sql = &quot;select * from conn where title like '%&quot; . $str . &quot;%'&quot;;
	$query = mysql_query ( $sql );
	while ( $rs = mysql_fetch_array ( $query ) ) {
		$ru &#x5B;] = $rs;
	}
	return $ru;
}
?&gt;
</pre>
<p>到此为止，通过上面的步骤，我们就完成了memcache的配置和基本使用…</p>
<p><strong>php5-memcache扩展提供的方法</strong></p>
<pre class="brush: plain; title: ; notranslate">
Memcache::add — 添加一个值，如果已经存在，则返回false
Memcache::addServer — 添加一个可供使用的服务器地址
Memcache::close — 关闭一个Memcache对象
Memcache::connect — 创建一个Memcache对象
memcache_debug — 控制调试功能
Memcache::decrement — 对保存的某个key中的值进行减法操作
Memcache::delete — 删除一个key值
Memcache::flush — 清除所有缓存的数据
Memcache::get — 获取一个key值
Memcache::getExtendedStats — 获取进程池中所有进程的运行系统统计
Memcache::getServerStatus — 获取运行服务器的参数
Memcache::getStats — 返回服务器的一些运行统计信息
Memcache::getVersion — 返回运行的Memcache的版本信息
Memcache::increment — 对保存的某个key中的值进行加法操作
Memcache::pconnect — 创建一个Memcache的持久连接对象
Memcache::replace — R对一个已有的key进行覆写操作
Memcache::set — 添加一个值，如果已经存在，则覆写
Memcache::setCompressThreshold — 对大于某一大小的数据进行压缩
Memcache::setServerParams — 在运行时修改服务器的参数
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
