This article describes the PHP memory cache implementation method. Share to everyone for your reference. Specifically as follows:
Caching in PHP is divided into many types such as memory caching, file caching, and page caching. This article is about some of the methods for memory caching in PHP, where we'll cover the memcached cache and PHP's own APC caching method.
1.Memcached Cache.
Memcached is a high-performance distributed memory cache server, by caching database query results, reducing the number of database visits to improve the speed of dynamic Web applications, memcached use the "Key=>value" way to organize data, You can allow multiple users on different hosts to access this caching system at the same time, generally used for large web sites, memcached use memory cache data, so it is volatile, when the server restarts, or memcached process aborted, the data will be lost, so Memcached cannot be used to persist data.
People who use Php_memcache think PHP memory caching is a very complicated thing, but it's not. Memcached is an efficient and fast distributed memory object caching system, which is mainly used to speed up WEB dynamic applications.
This article describes the configuration of memcached under WIN32 and its use.
One, PHP memory cache configuration, WIN32 environment
1, download Php_memcache.rar
Decompression Compression Package: Php_memcache.rar
The main files contained in the Php_memcache.rar compression package are:
/memcached-1.2.1-win32/memcached.exe
/php_memcache/php_memcache.dll
2, open the command prompt, point to the path to Memcached.exe, run memcached.exe-d start.
3, copy the Php_memcache.dll file to the PHP Dynamic File Library folder.
4. Add a line of Extension=php_memcache.dll to the php.ini file.
5, restart Apache, and then look at the phpinfo, if there is memcache, then the installation is successful!
example, the code is as follows:
Copy Code code as follows:
<?php
Contains memcached class files
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 to compress
' Persistant ' => false//whether to use persistent connections
);
Instantiating a Memcached object
$memcached = new memcached ($options);
$sql = ' SELECT * from table1 ';
$key = MD5 ($sql);
If no data is cached in the memcached, the cached data is written to the 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
{
Direct use of cached data in memcached $datas
}
?>
Memory Cache Two, comparison of APC, EC, Zend Accelerator
First, APC
APC, the full name is alternative PHP cache, official translation is called "Optional PHP Caching", the home page is the http://pecl.php.net/package/apc,php Help manual page: HTTP://CN.PHP.NET/APC
APC is an optimizer, from the date of installation, silently in the background for your PHP application services. All your PHP code will be cached for PHP opcode.
In addition, APC provides a certain amount of memory caching. But this feature is not perfect, and there are reports that if you use the APC cache write feature frequently, it can cause unpredictable errors. If you want to use this feature, you may look at the APC_FETCH,APC_ A few functions associated with the APC cache, such as the store.
Install the code as follows:
Copy Code code as follows:
Configuration:/etc/php.inc, the code is as follows:
Copy Code code as follows:
[APC]
Copy 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 will help you with your PHP program design.