????使用Xcache缓存器加速你的PHP网站
由于国内网站备案比较麻烦,所以笔者便把网站放到了香港。虽然网站免去了备案的困扰,但是从访问速度上来看,一般要比放于国内的网站慢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
<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>
注:--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>
然后添加如下内容
<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>
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的简单类:
程序代码
<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> ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
