<?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>memcached插件 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/memcached%E6%8F%92%E4%BB%B6/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Sun, 03 May 2020 12:23:20 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]使用memcached为WordPress站点提速</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%bd%bf%e7%94%a8memcached%e4%b8%bawordpress%e7%ab%99%e7%82%b9%e6%8f%90%e9%80%9f/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%bd%bf%e7%94%a8memcached%e4%b8%bawordpress%e7%ab%99%e7%82%b9%e6%8f%90%e9%80%9f/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sat, 02 Feb 2013 15:38:45 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[memcached插件]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress memcached plugin]]></category>
		<category><![CDATA[安装memcached插件]]></category>
		<category><![CDATA[配置memcached插件]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=5658</guid>

					<description><![CDATA[<p>
本文的前提条件：你对你的WordPress站点服务器有控制权，例如，你使用的是VPS搭建的WordPress站点，而不是虚拟主机。如果不符合条件就不用往下看了，因为后面的很多操作是需要root权限来安装、配置软件的。另外，我使用的是32位的Linux系统，如果你是64位的系统，可能有些许不一样，或者出现一些文中没有遇到的问题，请注意。</p>
<p>首先说一下使用memcached为WordPress站点提速的意义。由于我不是做前端开发的，所以下面的部分内容说的不一定正确、深刻，您就凑合着看吧。<br />
如果你的WordPress站点访问量很大，那么VPS负载可能就很高，而这其中，可能有很大一部分就是MySQL的负载&#8212;&#8212;它要将大量内容读取出来，并返回给访客。其实这部分工作可以通过缓存来提高性能，<a href="http://memcached.org/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">memcached</span></a>就是这样一个软件，可以帮助我们实现这一点。<br />
<span id="more-5658"></span><br />
从<a href="http://memcached.org/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">memcached</span></a>的官方网站介绍中，我们知道它是：</p>
<blockquote>
<p>
		Free &#38; open source, high-performance, distributed memory object caching system.</p>
</blockquote>
<p>说得直白简单一点，它就是把最常访问的那些内容缓存在内存中，有新访客到来的时候，先从内存中找，找到了就直接返回，否则再去查数据库，这样就极大地提高了性能。<br />
有人可能会使用<a href="http://wordpress.org/extend/plugins/wp-super-cache/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">WP Super Cache</span></a>这个WordPress插件来为WP提速（而且它的效果似乎更好），但此插件会生成大量缓存文件，占用大量磁盘空间，从而影响WordPress备份文件的大小，这对我来说并不是一个好结果，因此，我没有用它。<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
那么，在WordPress中，我们怎么使用memcached呢？这不是仅仅安装memcached软件就可以做到的，还要和PHP结合起来，所以涉及到好几个部分，下面分别阐述：<br />
<span style="background-color:#00ff00;">【1】</span>安装memcached<br />
首先我们要从<a href="http://memcached.org/" target="_blank" rel="noopener noreferrer">memcached的官方网站</a>下载一个稳定版，例如，我下载的是 1.4.15 版，然后准备安装它。<br />
但是memcached的根基是一个网络开发库 <a href="http://libevent.org/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">libevent</span></a>，所以，你要先安装 libevent。我下载的是 2.0.21（稳定版），解压出来，然后编译安装：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./configure --prefix=/usr/local/libevent
make
make install
</pre>
<p>注意我安装到了 /usr/local/libevent&#160;目录下。<br />
然后编译安装memcached：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./configure</pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%bd%bf%e7%94%a8memcached%e4%b8%bawordpress%e7%ab%99%e7%82%b9%e6%8f%90%e9%80%9f/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
本文的前提条件：你对你的WordPress站点服务器有控制权，例如，你使用的是VPS搭建的WordPress站点，而不是虚拟主机。如果不符合条件就不用往下看了，因为后面的很多操作是需要root权限来安装、配置软件的。另外，我使用的是32位的Linux系统，如果你是64位的系统，可能有些许不一样，或者出现一些文中没有遇到的问题，请注意。</p>
<p>首先说一下使用memcached为WordPress站点提速的意义。由于我不是做前端开发的，所以下面的部分内容说的不一定正确、深刻，您就凑合着看吧。<br />
如果你的WordPress站点访问量很大，那么VPS负载可能就很高，而这其中，可能有很大一部分就是MySQL的负载&mdash;&mdash;它要将大量内容读取出来，并返回给访客。其实这部分工作可以通过缓存来提高性能，<a href="http://memcached.org/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">memcached</span></a>就是这样一个软件，可以帮助我们实现这一点。<br />
<span id="more-5658"></span><br />
从<a href="http://memcached.org/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">memcached</span></a>的官方网站介绍中，我们知道它是：</p>
<blockquote>
<p>
		Free &amp; open source, high-performance, distributed memory object caching system.</p>
</blockquote>
<p>说得直白简单一点，它就是把最常访问的那些内容缓存在内存中，有新访客到来的时候，先从内存中找，找到了就直接返回，否则再去查数据库，这样就极大地提高了性能。<br />
有人可能会使用<a href="http://wordpress.org/extend/plugins/wp-super-cache/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">WP Super Cache</span></a>这个WordPress插件来为WP提速（而且它的效果似乎更好），但此插件会生成大量缓存文件，占用大量磁盘空间，从而影响WordPress备份文件的大小，这对我来说并不是一个好结果，因此，我没有用它。<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
那么，在WordPress中，我们怎么使用memcached呢？这不是仅仅安装memcached软件就可以做到的，还要和PHP结合起来，所以涉及到好几个部分，下面分别阐述：<br />
<span style="background-color:#00ff00;">【1】</span>安装memcached<br />
首先我们要从<a href="http://memcached.org/" target="_blank" rel="noopener noreferrer">memcached的官方网站</a>下载一个稳定版，例如，我下载的是 1.4.15 版，然后准备安装它。<br />
但是memcached的根基是一个网络开发库 <a href="http://libevent.org/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">libevent</span></a>，所以，你要先安装 libevent。我下载的是 2.0.21（稳定版），解压出来，然后编译安装：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./configure --prefix=/usr/local/libevent
make
make install
</pre>
<p>注意我安装到了 /usr/local/libevent&nbsp;目录下。<br />
然后编译安装memcached：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./configure --with-libevent=/usr/local/libevent/ --prefix=/usr/local/memcached
make
make install
</pre>
<p>注意我指定了libevent的安装目录，并且把memcached安装到了 /usr/local/memcached 目录下。<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0); ">【2】</span>启动memcached<br />
安装了memcached软件之后，我们就要启动它了：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
cd /usr/local/memcached/bin/
./memcached -d -m 256 -u root -t 64 -r
</pre>
<p>-d 表示以守护进程的方式启动memcached程序，-m 256 表示最大可使用256M的内存，-u root 我不太明白有什么用（网上的某些教程里有这样用的），-t 64 表示使用64个线程，-r 表示最大化core文件的限制。<br />
这样启动之后，memcached就开始在默认端口 11211 上监听了。<br />
现在再看看进程中是否已经有了memcached：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
[root@localhost]# ps -ef | grep memcached
root      2180     1  0 22:29 ?        00:00:00 ./memcached -d -m 256 -u root -t 64 -r
</pre>
<p>最后再把它加入开机启动项，编辑 /etc/rc.local 文件，添加一句：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
/usr/local/memcached/bin/memcached -d -m 256 -u root -t 64 -r
</pre>
<p><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0); ">【3】</span>安装PHP的memcached扩展<br />
为了能让WordPress用上我们刚安装的memcached，需要先为PHP安装一个memcached扩展。<br />
到<a href="http://pecl.php.net/package/memcache" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">这个页面</span></a>去下载一个稳定版的memcached扩展，例如，我下载的是2.2.7版，然后解压：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar zxf memcache-2.2.7.tgz
cd memcache-2.2.7
</pre>
<p>如果你没这样装过PHP扩展的话，你会很奇怪地发现，解压出来的目录下竟然没有 configure 文件！没有 configure 文件如何能生成Makefile，如何能编译安装呢？<br />
这个时候，我们需要用PHP安装时附带的 phpize 程序来帮助我们生成configure：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
cd memcache-2.2.7
/usr/local/php/bin/phpize
</pre>
<p>注意，我的PHP是安装在 /usr/local/php 目录下的，如果你的目录不同，请作相应的修改。顺利的话，就会发现生成了configure文件；如果遇到了以下错误：</p>
<blockquote>
<p>
		Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script.</p>
</blockquote>
<div>
	<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
	那么，就需要安装以下两个package（如果是Ubuntu，就apt-get install，这里就不废话了）：</div>
<div>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
yum install m4
yum install autoconf
</pre>
</div>
<div>
	然后再重新执行phpize命令，再编译安装：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
make
make install
</pre>
</div>
<div>
	会提示：</div>
<blockquote>
<div>
		Installing shared extensions: &nbsp; &nbsp; /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/</div>
</blockquote>
<div>
	然后修改你的php.ini配置文件，在最后添加：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
[memcache]
extension=memcache.so
</pre>
</div>
<p>最后重启你的web service，如果你用的是nginx，那么需要重启 php-fpm 和 nginx：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
/etc/init.d/php-fpm restart
/etc/init.d/nginx restart
</pre>
<p><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0); ">【4】</span>安装、配置WordPress的memcached插件<br />
上面的步骤已经够麻烦了，但是到了这一步，你就快成功了。<br />
在WordPress后台的插件安装页面中，搜索&ldquo;memcached&rdquo;，会找到&ldquo;Memcached Object Cache&rdquo;插件，安装之，然后这里要注意了，与平常安装插件不一样的是，安装完这个插件之后，<strong><span style="color:#ff0000;">不要&ldquo;启用&rdquo;它</span></strong>，否则会报错。<br />
我们需要手工把安装插件得到的 object-cache.php&nbsp;文件拷贝到WordPress的 wp-content&nbsp;目录下，它就可以开始工作了（有点奇怪吧）：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
cp wp-content/plugins/memcached/object-cache.php wp-content/
</pre>
<p>然后memcached就开始为你的WordPress默默地&ldquo;做奉献&rdquo;了，尽管你看不到它。如果你不想再管它，那么到这一步就可以收手了，不过，你可能还想知道它工作得怎么样，那么你就要继续看下去。<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0); ">【5】</span>安装memcached的管理插件<br />
同样在WordPress的插件安装界面中搜索&ldquo;WP Memcached Manager&rdquo;，安装此插件，启用它，即可在WordPress管理后台的左侧菜单中，看到多出了一项&ldquo;Memcached&rdquo;：</p>
<div style="text-align: center; ">
	<a href="http://www.codelast.com" target="_blank" rel="noopener noreferrer"><img decoding="async" alt="WordPress memcached menu" src="http://www.codelast.com/wp-content/uploads/2013/02/wp_menu_memcached.jpg" style="width: 153px; height: 82px; " /></a></div>
<p>点击&ldquo;Edit servers&rdquo;，如下图所示的默认server：</p>
<div style="text-align: center; ">
	<a href="http://www.codelast.com" target="_blank" rel="noopener noreferrer"><img decoding="async" alt="WordPress memcached manager add server" src="http://www.codelast.com/wp-content/uploads/2013/02/wp_memcached_manager_add_server.jpg" style="width: 371px; height: 195px; " /></a></div>
<p>然后再回到&ldquo;Memcached&rdquo;菜单，就可以看到下拉列表中多出了一项，即我们刚添加的server，点击&ldquo;Manage Server&rdquo;，就会看到关于memcached的一些统计信息啦：</p>
<div style="text-align: center; ">
	<a href="http://www.codelast.com" target="_blank" rel="noopener noreferrer"><img decoding="async" alt="WordPress memcached manager manage server" src="http://www.codelast.com/wp-content/uploads/2013/02/wp_memcached_manager_manage_server.jpg" style="width: 600px; height: 376px; " /></a></div>
<p>这下踏实了，我们知道memcached确实在工作了，并且hit和miss的数据都知道了。<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0); ">【6】</span>进一步优化<br />
有一个叫做<a href="http://wordpress.org/extend/plugins/batcache/" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">Batcache</span></a>的WordPress插件，能为我们上面的工作锦上添花，建议在WordPress后台安装它（不需要配置）。它的原理是：<br />
在没有Batcache、有memcached的情况下，访问WordPress页面时，需要从内存中获取多个缓存对象，而Batcache可以把整个页面作为一个对象缓存，从而从内存中只需要获取一个缓存对象，这样就提高了速度。</p>
<p>至此，我们整个安装、配置memcached的过程就结束了，在访问量很大的时候，你的WordPress站点的性能一定会得到质的提升。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushPlain.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e4%bd%bf%e7%94%a8memcached%e4%b8%bawordpress%e7%ab%99%e7%82%b9%e6%8f%90%e9%80%9f/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
