This article mainly introduces the php memory cache implementation method, analyzes the usage of Memcached cache, and compares the usage of APC, EC, and Zend accelerators, for more information about how to implement the php memory cache, see the example in this article. Share it with you for your reference. The details are as follows:
In php, cache is divided into many types, such as memory cache, file cache, and page cache. This article will introduce some methods for caching memory in php. here we will introduce the Memcached cache and the APC cache method that comes with php.
1. Memcached cache.
Memcached is a high-performance distributed memory cache server that caches database query results to reduce database access times and increase the speed of dynamic Web applications, memcached uses the "Key => Value" method to organize data. it allows multiple users on different hosts to access the cache system at the same time. it is generally used for large websites. memcached uses the memory to cache data, therefore, it is easy to lose. when the server is restarted or the memcached process is terminated, data will be lost. Therefore, memcached cannot be used to persistently store data.
People who have used php_memcache will think that PHP memory cache is a very complicated thing. In fact, memcached is an efficient and fast distributed memory object cache system, which is mainly used to accelerate dynamic WEB applications.
Here we will introduce the configuration and usage of memcached in WIN32.
I. PHP memory cache configuration, WIN32 environment
1. download php_memcache.rar
Decompress the package: php_memcache.rar
The main files in the php_memcache.rar package include:
/Memcached-1.2.1-win32/memcached.exe
/Php_memcache/php_memcache.dll
2xx indicates running memcached.exe-d start by logging on to memcached.exe.
3. copy the php_memcache.dll file to the dynamic file library folder of PHP.
4. add a line of extension = php_memcache.dll to the php. ini file.
5. restart Apache and check phpinfo. if memcache exists, the installation is successful!
For example, the code is as follows:
The code is as follows:
<? Php
// Contains the memcached class file
Require_once ('memcached-client. php ');
// Option settings
$ Options = array (
'Servers' => array ('www .bitsCN.com: 100'), // address and port of the memcached service
'Debug' => true, // whether to enable debug
'Compress _ threshold '=> 10240, // when the number of bytes of data exceeds
'Persistant' => false // whether to use persistent connection
);
// Instantiate the memcached object
$ Memcached = new memcached ($ options );
$ SQL = 'select * FROM table1 ';
$ Key = md5 ($ SQL );
// If no cached data exists in memcached, write the cached data to memcached.
If (! ($ Datas = $ memcached-> get ($ key )))
{
$ Conn = mysql_connect ('localhost', 'hxsd', '123 ');
Mysql_select_db ('hxsd ');
$ Result = mysql_query ($ SQL );
While ($ row = mysql_fetch_object ($ result ))
{
$ Datas [] = $ row;
}
// Save the result set data obtained from the database to memcached for the next access.
$ Memcached-> add ($ key, $ datas );
}
Else
{
// Directly use the cached data in memcached $ datas
}
?>
Memory cache 2: Comparison of APC, EC, and Zend accelerators
I. APC
APC, full name is Alternative PHP Cache, the official translation is called "optional PHP Cache", the home page is http://pecl.php.net/package/apc, PHP help album page: http://cn.php.net/apc
APC is an optimizer. since its installation, it silently serves your PHP application in the background. all your PHP code will be cached for php opcode.
In addition, APC can provide certain memory cache functions. however, this function is not perfect. it is reported that frequent use of the write function of APC cache will lead to unexpected errors. if you want to use this function, you can look at several functions related to apc cache, such as apc_fetch and apc_store.
The installation code is as follows:
The code is as follows:
# Pecl install APC
Configuration:/etc/php. inc. the code is as follows:
The code is as follows:
Extension = apc. so
[Apc]
The code is 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 will help you with php programming.