PHP Memory Cache Implementation program code

Source: Internet
Author: User

1.Memcached Cache.
Memcached is a high-performance distributed memory cache server, which reduces the number of database visits by caching database query results to improve the speed of dynamic Web applications. Memcached uses the "Key=>value" method to organize the data. You can allow multiple users on different hosts to access the caching system at the same time, typically for use with large web sites. Memcached uses memory to cache data, so it is volatile, and 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 feel that the PHP memory cache is a very complex thing, but it is 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, please download from the attachment.

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!


Cases:

The code is as follows Copy Code

<?php
Contains memcached class files
Require_once (' memcached-client.php ');

Option settings
$options = Array (
' Servers ' => array (' www.hxsd.com: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 and Zend Accelerator

First, APC

APC, the full name is alternative PHP cache, official translation is called "Optional PHP caching."

Homepage is HTTP://PECL.PHP.NET/PACKAGE/APC

PHP help book 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.

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]

  code is as follows copy code

apc.enabled = 1

Apc.shm_segments = 1

Apc.shm_size = +

Apc.optimization = 0

Apc.ttl = 7200

Apc.use R_ttl = 7200

Apc.num_files_hint = 1000

Apc.mmap_file_mask =/tmp/apc. XXXXXX

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.