PHP Memory Cache Implementation method, PHP memory cache
This article describes the implementation of the PHP memory cache method. Share to everyone for your reference. 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:
Copy the code code 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 how many bytes of data to compress
' persistant ' + false//whether to use persistent connections
);
//Instantiate 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;
}
//To save the result set data obtained in the database to memcached for use on the next visit.
$memcached->add ($key, $datas);
}
Else
{
//direct use of cached data in memcached $datas
}
?
Memory Cache Two, APC, EC, Zend Accelerator comparison
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.
Install the code as follows: copy The code as follows: # pecl Install APC
Configuration:/etc/php.inc, the code is as follows:
Copy the Code code as follows: extension=apc.so
[APC]
Copy the Code code as follows: apc.enabled = 1
Apc.shm_segments = 1
Apc.shm_size = 30
apc.optimization = 0
Apc.ttl = 7200
Apc.user_ttl = 7200
Apc.num_files_hint = 1000
Apc.mmap_file_mask =/tmp/apc. Xxxxxx
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/947219.html www.bkjia.com true http://www.bkjia.com/PHPjc/947219.html techarticle PHP Memory Cache Implementation method, PHP memory Cache This example describes the PHP memory cache implementation method. Share to everyone for your reference. The following: In PHP, the cache is divided into many types ...