search
HomeBackend DevelopmentPHP TutorialImplementation steps for PHP cache optimization using memcached and xcache

We know that eaccelerator is a free and open source PHP accelerator that optimizes and dynamic content caching, can improve PHP's script caching performance, and plays an optimization role in scripts, so that your PHP program code execution efficiency can be improved by 1-10 Times, today I will teach you how to optimize caching in PHP.

2. Caching principle

When a user requests a PHP program, the PHP engine will parse the program and compile it into a specific operation code (opcode), which is a Executable code in binary format. This opcode is then executed by the PHP engine and discarded. The opcode cache will save this compiled opcode and reuse it the next time the page is called, thus saving a lot of time on repeated compilation, saving resources, and optimizing performance.
3. Install eaccelerator

Adjust Character set

#echo 'LC_ALL=C' >> /etc/profile#source /etc/profile
[root@~]#tar jxf eaccelerator-0.9.6.tar.bz2[root@~]#cd eaccelerator-0.9.6
[root@~]#/usr/local/php/bin/phpize
phpize是用来扩展php模块的,通过phpize可以建立php的外挂模块[root@~]#./configure --enable-eaccelerator=shared \
--with-php-config=/usr/local/php-5.3.27/bin/php-config[root@~]#make && make install
[root@~]# ls /usr/local/php-5.3.27/lib/php/extensions/no-debug-zts-20090626/eaccelerator.so

4. Configure PHP to load eaccelerator

Create cache directory

mkdir /tmp/eaccelerator #The directory can be stored using tmpfs memoryFile system, SSD solid state drive

chown -R www.www /tmp/eaccelerator

Add the following content in php.ini:

eaccelerator cache config by Zhang DeJin 2017-12-1
[eaccelerator]
extension = eaccelerator.so
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.chech_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_perid="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
--End config
测试:[root@lnmp nginx]# /usr/local/php-5.3.27/bin/php -v
PHP 5.3.27 (cli) (built: Nov 30 2017 05:35:00)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies
with eAccelerator v0.9.6, Copyright (c) 2004-2010 eAccelerator, by eAccelerat

Configuration completed

Restart httpd or php-fpm to take effect

5. Version selection

php5.3.xx available eaccelerator-0.9.6 version
php5.2.xx available eaccelerator-0.9.5.2 version

2)Xcache

1Introduction

XCache is a fast and stable PHP opcode cache. After Well tested and runs stably on high traffic/high load production machines. Tested (on linux) and supports the latest releases of all current PHP branches, such as PHP_4_3 PHP_4_4 PHP_5_0 PHP_5_1 PHP_5_2 HEAD(6.x), and supports Thread-safe/Windows. Better than similar opcode caches, such as the ability to quickly follow PHP versions. The XCache project is led by mOo, who is also a member of the development team of Lighttpd. Lighttpd is one of the fastest web server applications , and outperforms Apache and many other web servers. #

tar jxf xcache-1.3.2.tar.bz2cd xcache-1.3.2/usr/local/php-5.3.27/bin/phpize./configure --enable-xcache --with-php-config=/usr/local/php-5.3.27/bin/php-configmake && make install
ls /usr/local/php-5.3.27/lib/php/extensions/no-debug-zts-20090626/

Modify xcache-1.3.2/xcache.ini, you can use the sed command to modify it

Comment the third line

cd /usr/local/php/lib/


Comment the sixth line

echo >> php.ini
echo "xcache config by ZhangDejin 2017-12-1"


Enable the eighth line

zend_extension = /usr/local/lib/php/extensions/non-debug-non-zts-xxx/xcache.so


Adjust the remaining parameters according to the server configuration

Then add to php.ini


zend_extension_ts = c:/php/extensions/php_xcache.dll

Test


;extension = xcache.so

Restart httpd or php-fpm to take effect

3) memcache client, PDO_mysql, imagick extension library


1. Introduction

Memcache is a high-performance distributed memory object

Cache system

. By maintaining a unified huge hash table in the memory, it can be used to store data in various formats, including Images, video files and the results of database retrieval, simply put, call the data into the memory and then read it from the memory, thus greatly improving the reading speed

PDO extension defines a database for PHP access A lightweight, consistent interface that provides a data access abstraction layer so that no matter what database is used, you can execute queries to obtain data through consistent functions

ImageMagick is a powerful and stable set of Moreover, the free working set and development kit can change the size, rotate, sharpen, reduce color, etc. of image types in more than 89 formats. Now its main focus is on performance, reducing bugs and providing stable API and ABI. Up

2. Installation

cat /home/king/xcache-1.3.2/xcache.ini >> php.ini

View nginx compilation parameters

nginx -V

View httpd compilation parameters

cat httpd/build/config.nice

View mysql Compilation parameters

grep "CONFIGURE_LINE" mysql/bin/mysqlbug

php Compilation parameters

php/bin/php -i|grep configure

Complete




I believe you have mastered the methods after reading these cases. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:


Answers to questions about camel case naming and JS

Boolean values, relational operators in JS, Detailed explanation and examples of logical operators


## Summary of the front-end js framework and explanation of its uses

The above is the detailed content of Implementation steps for PHP cache optimization using memcached and xcache. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment