This article mainly introduces how PHP uses memory as a cache implementation method, analyzes the usage of memcached cache and compares the usage of APC, EC, Zend Accelerator, the need for friends can refer to.
Specific as follows:
In PHP the cache is divided into many types such as memory cache, file cache, and page cache. This article will cover some of the methods of memory caching in PHP, where we'll introduce the memcached cache and the APC caching method that comes with PHP.
1.Memcached Cache.
Memcached is a high-performance distributed memory cache server that uses the "key=>value" approach to organize data by caching database query results and reducing database access times to improve the speed of dynamic Web applications memcached Can allow multiple users on different hosts to access the cache system at the same time, generally used for large web sites, Memcached uses memory cache data, so it is volatile, when the server restarts, or the memcached process aborts, the data will be lost, so Memcached cannot be used to persist data.
People who have used Php_memcache will feel that PHP memory caching is a complex thing, but memcached is an efficient and fast distributed memory object caching system, mainly used to accelerate WEB dynamic applications.
The configuration and usage of memcached under WIN32 are described here.
First, the configuration of PHP memory cache, WIN32 environment
1. Download Php_memcache.rar
Decompression Pack: Php_memcache.rar
The main files included in the Php_memcache.rar package are:
/memcached-1.2.1-win32/memcached.exe
/php_memcache/php_memcache.dll
2. Open a command prompt, point to the path to Memcached.exe, and run memcached.exe-d start.
3. Copy the Php_memcache.dll file to the folder in PHP's dynamic file library.
4. Add a row of Extension=php_memcache.dll to the php.ini file.
5, restart Apache, and then check the phpinfo, if there is memcache, then the installation is successful!
example, the code is as follows:
<?php//contains memcached class file require_once (' memcached-client.php '); Option settings $options = Array (' servers ' = = Array (' www.jb51.net:11211 '),//memcached service address, Port ' debug ' = = true,/ /whether to turn on debug ' compress_threshold ' + 10240,//more than the number of bytes of data when compressing ' persistant ' = false//whether to use persistent connection ); Instantiate the Memcached object $memcached = new memcached ($options); $sql = ' SELECT * from table1 '; $key = MD5 ($sql); If there is no cached data in memcached, write the cached data to memcached if (!) ( $datas = $memcached->get ($key))) { $conn = mysql_connect (' localhost ', ' hxsd ', ' 123456 '); mysql_select_db (' hxsd '); $result = mysql_query ($sql); while ($row = Mysql_fetch_object ($result)) { $datas [] = $row; } Saves the result set data obtained in the database to memcached for use on the next visit. $memcached->add ($key, $datas);} else { //directly using cached data in memcached $datas}?>
Memory Cache Two, comparison of APC, EC, Zend Accelerators
First, APC
APC, the full name is alternative PHP cache, the official translation is called "Optional PHP Caching", home is the http://pecl.php.net/package/apc,php Help manual page: HTTP://CN.PHP.NET/APC
APC is an optimizer that silently serves your PHP application in the background from the date of installation. All your PHP code will be cached for PHP opcode.
In addition, APC provides a certain amount of memory caching. However, this feature is not perfect, and there are reports of unpredictable errors that can occur if the use of the APC cache write function is frequent. If you want to use this feature, look at the APC_FETCH,APC_ Store and several other functions related to APC cache.
Installation, the code is as follows:
# pecl Install APC
Configuration:/etc/php.inc, the code is as follows:
Extension=apc.so
[APC]
apc.enabled = 1 Apc.shm_segments = 1 apc.shm_size = apc.optimization = 0 Apc.ttl = 7200 Apc.user_ttl = 7200 Apc.num_fil Es_hint = Apc.mmap_file_mask =/tmp/apc. Xxxxxx
Related recommendations:
Application examples of PHP memcache on public platforms
PHP caching mechanism
PHP Caching Technology Example _php tutorial