Using the xcache cache to accelerate your PHP website

Source: Internet
Author: User
Tags apc php website varnish

Because the domestic website ICP filing is troublesome, the author places the website in Hong Kong. Although the website is not subject to record filing, the access speed is generally 2-3 times slower than that of websites in China. Therefore, we have made some simple improvements to the website, for example, the cache system is used to speed up website page access.

Currently, many Web cache systems are used, including squid, varnish, proxy_cache provided by nginx, fastcgi_cache, APC, and xcache in FastCGI.

Systems such as squid, varnish, and nginx's proxy_cache are heavyweight products. Configuration and maintenance are troublesome and are not suitable for small websites. In addition, these systems are generally used to cache static content, such as fastcgi_cache in FastCGI, which is mainly used to cache dynamic content. Therefore, it is extremely fast to access websites using fastcgi_cache, however, when I use it, I find it difficult to maintain it. In particular, after each website has data to be updated, if you need to manually clear the cache after the expiration of the buffer period to see the updated content of the website; as for APC, I personally feel that the performance is normal. When comparing it with xcache, I found that the speed of accessing the xcache website is significantly higher than that of using the APC website (I have not specifically tested it ), therefore, xcache is selected.

We all know that PHP is a dynamic language that is executed in an interpreted manner. Therefore, every execution of PHP code is parsed and converted into an opcode ). Xcache is an open-source operation code Cache/optimizer. It caches the parsed/converted PHP operation code to a file (until the original code is modified) to avoid repeated parsing processes, the code execution speed is improved. Generally, the page generation rate is increased by 2-5 times, server load is reduced, and the website access speed is improved.

 

1. Install xcache

1 # wget http://xcache.lighttpd.net/pub/Releases/1.3.0/xcache-1.3.0.tar.gz2 # tar zxvf xcache-1.3.0.tar.gz3 # cd xcache-1.3.04 # /usr/local/php/bin/phpize5 # ./configure --enable-xcache--enable-xcache-coverager --enable-xcache-optimizer--with-php-config=/usr/local/php/bin/php-config6 # make && make install

Note: -- enable-xcache indicates that xcache support is enabled; -- enable-xcache-coverager indicates that additional features are included for measuring accelerator efficacy; -- enable-xcache-optimizer indicates that operation code optimization is enabled.

After the installation is complete, the system prompts xcache. so module generated path, this generated path for/usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20060613/, and then xcache. so move to the/usr/local/PHP/include/PHP/EXT directory.

 

Ii. configure and manage xcache

1. Modify the PHP configuration file

During configuration, refer to the xcache configuration template xcache. ini, which is located in the xcache installer.

# vi /usr/local/php/lib/php.ini

Then add the following content:

 1 extension_dir=/usr/local/php/include/php/ext 2  3 [xcache-common] 4 extension = xcache.so 5 [xcache.admin] 6 xcache.admin.enable_auth = On 7 xcache.admin.user = "xcache" 8 xcache.admin.pass = "" 9 10 [xcache]11 xcache.shm_scheme ="mmap"12 xcache.size=60M13 xcache.count =114 xcache.slots =8K15 xcache.ttl=016 xcache.gc_interval =017 xcache.var_size=4M18 xcache.var_count =119 xcache.var_slots =8K20 xcache.var_ttl=021 xcache.var_maxttl=022 xcache.var_gc_interval =30023 xcache.test =Off24 xcache.readonly_protection = On25 xcache.mmap_path ="/tmp/xcache"26 xcache.coredump_directory =""27 xcache.cacher =On28 xcache.stat=On29 xcache.optimizer =Off30 31 [xcache.coverager]32 xcache.coverager =On33 xcache.coveragedump_directory =""

 

2. Generate xcache cache files

# touch /tmp/xcache# chmod 777 /tmp/xcache

3. Generate the xcache administrator secret (MD5 ciphertext)

# Echo-n "123456" |

Md5sume10adc3949ba59abbe56e057f20f883e

Then paste the generated MD5 ciphertext into the php. ini file xcache. admin. Pass = "" option, xcache. admin. Pass = "e10adc3949ba59abbe56e057f20f883e"

4. Copy the xcache hypervisor to the root directory of the website.

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

Restart PHP and access http: // localhost/admin. the user name is xcache and the password is 123456. In addition, you can verify whether PHP supports xcache through phpinfo.

Note that xcache can only cache some default objects, such as int, string, and array, but cannot cache objects. Otherwise, an error will be reported during reading.

If you want to cache an object, you can also serialize the object and deserialize it again during reading.

The following is a simple xcache class I wrote:

 

Program code

 1 <?php 2 /** 3 * Xcache moudle 4 */ 5 class cacheHelper{ 6   public $prefix; 7   function __construct(){ 8     if(!function_exists('xcache_get')){ 9       exit("This application must required XCache module.");10     }11   }12   /**13    * __set14    *15    * @param mixed $name16    * @param mixed $value17    * @access public18    * @return void19    */20   public function __set($name, $value){21     xcache_set($this->prefix.$name, $value);22   }23   /**24    * __get25    *26    * @param mixed $name27    * @access public28    * @return mixed29    */30   public function __get($name){31     return xcache_get($this->prefix.$name);32   }33   /**34    * __isset35    *36    * @param mixed $name37    * @access public38    * @return bool39    */40   public function __isset($name){41     return xcache_isset($this->prefix.$name);42   }43   /**44    * __unset45    *46    * @param mixed $name47    * @access public48    * @return void49    */50   public function __unset($name){51     xcache_unset($this->prefix.$name);52   }53 }54 ?>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.