首頁  >  文章  >  php教程  >  ????使用Xcache缓存器加速你的PHP网站

????使用Xcache缓存器加速你的PHP网站

WBOY
WBOY原創
2016-06-06 19:53:451201瀏覽

由于国内网站备案比较麻烦,所以笔者便把网站放到了香港。虽然网站免去了备案的困扰,但是从访问速度上来看,一般要比放于国内的网站慢2-3倍,所以便想办法对网站做了一些简单的优化,比如使用缓存系统来提升网站页面访问速度。 目前用于Web的缓存系统很多,

  由于国内网站备案比较麻烦,所以笔者便把网站放到了香港。虽然网站免去了备案的困扰,但是从访问速度上来看,一般要比放于国内的网站慢2-3倍,所以便想办法对网站做了一些简单的优化,比如使用缓存系统来提升网站页面访问速度。

  目前用于Web的缓存系统很多,包括squid、varnish、Nginx自带的proxy_cache、FastCGI中的fastcgi_cache、APC、Xcache等。

  像squid、varnish、Nginx自带的proxy_cache这类系统,属于重量级产品,配置维护比较麻烦,不适合小型网站,而且一般用这类系统缓存静态内容,比如图片、css、JavaScript等;像FastCGI中的fastcgi_cache,它主要用于缓存动态内容,所以在访问使用fastcgi_cache的网站时速度极快,但是笔者使用时发现其维护比较麻烦,特别是每次网站有数据要更新后,如果不等到缓冲期过期后得需要手动清除缓存才能看到网站更新的内容;至于APC个人感觉性能就一般了,拿它和Xcache比较时发现访问使用Xcache网站的速度明显高于使用APC网站的速度(笔者没有具体测试),所以最终选择了使用Xcache。

  我们都知道PHP是一种动态语言,它在执行时是以解释的方式执行,所以PHP代码每次执行时都会被解析和转换成操作码(opcode)。而Xcache是一个开源的操作码缓存器/优化器,它通过把解析/转换PHP后的操作码缓存到文件(直到原始代码被修改)从而避免重复的解析过程,提高了代码的执行速度,通常能够提高页面生成速率2-5倍,降低了服务器负载,提高了用户访问网站的速度。

 

一、安装Xcache

????使用Xcache缓存器加速你的PHP网站

<span>1</span> # <span>wget</span> http:<span>//</span><span>xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gz</span>
<span>2</span> # <span>tar</span> zxvf xcache-<span>1.3</span>.<span>0</span>.<span>tar</span><span>.gz
</span><span>3</span> # cd xcache-<span>1.3</span>.<span>0</span>
<span>4</span> # /usr/local/php/bin/<span>phpize
</span><span>5</span> # ./configure --enable-xcache--enable-xcache-coverager --enable-xcache-optimizer--with-php-config=/usr/local/php/bin/php-<span>config
</span><span>6</span> # <span>make</span> && <span>make</span> <span>install</span>

????使用Xcache缓存器加速你的PHP网站

  注:--enable-xcache表示启用Xcache支持;--enable-xcache-coverager表示包含用于测量加速器功效的附加特性;--enable-xcache-optimizer表示启用操作码优化

  安装完毕后系统会提示xcache.so模块生成路径,本次生成路径为/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/,然后把xcache.so移动到/usr/local/php/include/php/ext目录下。

 

二、配置管理Xcache

1、修改php配置文件

  配置时我们可以参考xcache的配置模板xcache.ini,此文件位于Xcache安装程序中

<span># vi /usr/local/php/lib/php.ini</span>

  然后添加如下内容

????使用Xcache缓存器加速你的PHP网站

<span> 1</span> extension_dir=/usr/local/php/include/php/<span>ext
</span><span> 2</span> 
<span> 3</span> [xcache-<span>common]
</span><span> 4</span> extension =<span> xcache.so
</span><span> 5</span> <span>[xcache.admin]
</span><span> 6</span> xcache.admin.enable_auth =<span> On
</span><span> 7</span> xcache.admin.user = <span>"</span><span>xcache</span><span>"</span>
<span> 8</span> xcache.admin.pass = <span>""</span>
<span> 9</span> 
<span>10</span> <span>[xcache]
</span><span>11</span> xcache.shm_scheme =<span>"</span><span>mmap</span><span>"</span>
<span>12</span> xcache.size=<span>60M
</span><span>13</span> xcache.count =<span>1</span>
<span>14</span> xcache.slots =<span>8K
</span><span>15</span> xcache.ttl=<span>0</span>
<span>16</span> xcache.gc_interval =<span>0</span>
<span>17</span> xcache.var_size=<span>4M
</span><span>18</span> xcache.var_count =<span>1</span>
<span>19</span> xcache.var_slots =<span>8K
</span><span>20</span> xcache.var_ttl=<span>0</span>
<span>21</span> xcache.var_maxttl=<span>0</span>
<span>22</span> xcache.var_gc_interval =<span>300</span>
<span>23</span> xcache.test =<span>Off
</span><span>24</span> xcache.readonly_protection =<span> On
</span><span>25</span> xcache.mmap_path =<span>"</span><span>/tmp/xcache</span><span>"</span>
<span>26</span> xcache.coredump_directory =<span>""</span>
<span>27</span> xcache.cacher =<span>On
</span><span>28</span> xcache.<span>stat</span>=<span>On
</span><span>29</span> xcache.optimizer =<span>Off
</span><span>30</span> 
<span>31</span> <span>[xcache.coverager]
</span><span>32</span> xcache.coverager =<span>On
</span><span>33</span> xcache.coveragedump_directory =<span>""</span>

????使用Xcache缓存器加速你的PHP网站

 

2、生成Xcache缓存文件

<span># touch /tmp/xcache# chmod 777 /tmp/xcache</span>

3、生成Xcache管理员的秘密(MD5密文)

# echo -n "123456" | 

md5sume10adc3949ba59abbe56e057f20f883e

  然后将上述生成的MD5密文粘贴到php.ini文件中xcache.admin.pass = ""选项,xcache.admin.pass= "e10adc3949ba59abbe56e057f20f883e"

4、拷贝Xcache管理程序到网站根目录下

<span># cp -a /tmp/xcache-1.3.0/admin//usr/local/nginx/html/</span>

  然后重新启动PHP,然后访问http://localhost/admin ,用户名为xcache 密码为123456;另外,还可以通过phpinfo来验证PHP是否支持Xcache

这里要注意的一点就是Xcache只能缓存默认的一些对象,如int, string, array等,不能缓存对象,否则读取的时候就会报错。

  如果你非要缓存对象的话也有办法就是将对象序列化,读取的时候再反序列化一次。

  下面我写的一个Xcache的简单类:

 

程序代码

????使用Xcache缓存器加速你的PHP网站

<span> 1</span> <span>php
</span><span> 2</span> <span>/*</span><span>*
</span><span> 3</span> <span>* Xcache moudle
</span><span> 4</span> <span>*/</span>
<span> 5</span> <span>class</span><span> cacheHelper{
</span><span> 6</span>   <span>public</span> <span>$prefix</span><span>;
</span><span> 7</span>   <span>function</span><span> __construct(){
</span><span> 8</span>     <span>if</span>(!<span>function_exists</span>('xcache_get'<span>)){
</span><span> 9</span>       <span>exit</span>("This application must required XCache module."<span>);
</span><span>10</span> <span>    }
</span><span>11</span> <span>  }
</span><span>12</span>   <span>/*</span><span>*
</span><span>13</span> <span>   * __set
</span><span>14</span> <span>   *
</span><span>15</span> <span>   * @param mixed $name
</span><span>16</span> <span>   * @param mixed $value
</span><span>17</span> <span>   * @access public
</span><span>18</span> <span>   * @return void
</span><span>19</span>    <span>*/</span>
<span>20</span>   <span>public</span> <span>function</span> __set(<span>$name</span>, <span>$value</span><span>){
</span><span>21</span>     xcache_set(<span>$this</span>->prefix.<span>$name</span>, <span>$value</span><span>);
</span><span>22</span> <span>  }
</span><span>23</span>   <span>/*</span><span>*
</span><span>24</span> <span>   * __get
</span><span>25</span> <span>   *
</span><span>26</span> <span>   * @param mixed $name
</span><span>27</span> <span>   * @access public
</span><span>28</span> <span>   * @return mixed
</span><span>29</span>    <span>*/</span>
<span>30</span>   <span>public</span> <span>function</span> __get(<span>$name</span><span>){
</span><span>31</span>     <span>return</span> xcache_get(<span>$this</span>->prefix.<span>$name</span><span>);
</span><span>32</span> <span>  }
</span><span>33</span>   <span>/*</span><span>*
</span><span>34</span> <span>   * __isset
</span><span>35</span> <span>   *
</span><span>36</span> <span>   * @param mixed $name
</span><span>37</span> <span>   * @access public
</span><span>38</span> <span>   * @return bool
</span><span>39</span>    <span>*/</span>
<span>40</span>   <span>public</span> <span>function</span> __isset(<span>$name</span><span>){
</span><span>41</span>     <span>return</span> xcache_isset(<span>$this</span>->prefix.<span>$name</span><span>);
</span><span>42</span> <span>  }
</span><span>43</span>   <span>/*</span><span>*
</span><span>44</span> <span>   * __unset
</span><span>45</span> <span>   *
</span><span>46</span> <span>   * @param mixed $name
</span><span>47</span> <span>   * @access public
</span><span>48</span> <span>   * @return void
</span><span>49</span>    <span>*/</span>
<span>50</span>   <span>public</span> <span>function</span> __unset(<span>$name</span><span>){
</span><span>51</span>     xcache_unset(<span>$this</span>->prefix.<span>$name</span><span>);
</span><span>52</span> <span>  }
</span><span>53</span> <span>}
</span><span>54</span> ?>

????使用Xcache缓存器加速你的PHP网站

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn