PHP Memory Cache Implementation program Code _php tutorial

Source: Internet
Author: User
In PHP, the cache is divided into many types, such as memory cache, file cache, and page caching. This article will cover some of the methods of memory caching in PHP, and we'll show you 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 improves the speed of dynamic Web applications by caching database query results and reducing database access times. Memcached organizes data using the "Key=>value" approach. You can allow multiple users on different hosts to access the cache system at the same time, which is commonly used for large web sites. Memcached uses memory to cache data, so it is volatile, when the server restarts, or the memcached process aborts, the data is lost, so memcached cannot be used to persist the data.

People who have Php_memcache will feel that PHP memory caching is a very complex thing, not actually.
Memcached is an efficient and fast distributed memory object caching system that is primarily 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, please download from the attachment.

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!


Cases:

The code is as follows Copy Code

Contains the Memcached class file
Require_once (' memcached-client.php ');

Option settings
$options = Array (
' Servers ' = = Array (' www.hxsd.com:11211 '),//memcached service address, port
' Debug ' = true,//whether to open debug
' Compress_threshold ' = 10240,//more than how many bytes of data are compressed
' 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 memcached, the cached data is written 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
{
Use the cached data in memcached directly $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 page is 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 Copy Code

# pecl Install APC

Configuration: (/etc/php.inc)

The code is as follows Copy Code

Extension=apc.so

[APC]

The code is as follows Copy Code

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


http://www.bkjia.com/PHPjc/444642.html www.bkjia.com true http://www.bkjia.com/PHPjc/444642.html techarticle in PHP cache is divided into many types such as, memory cache, file cache, page cache this article to tell about the memory cache in PHP some methods, the following we introduce the memcached cache and ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.