Php memory cache implementation code _ PHP Tutorial

Source: Internet
Author: User
Tags apc
Php memory cache implementation program code. In php, cache is divided into many types, such as memory cache, file cache, and page cache. This article will introduce some methods of memory cache in php, next we will introduce the Memcached cache and the cache in php into many types, such as memory cache, file cache, and page cache. This article will introduce some methods for memory cache in php, next 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. Multiple users on different hosts can access the cache system at the same time, which is generally used for large websites. Memcached uses memory to cache data, so it is easy to lose. when the server is restarted or the memcached process is suspended, data will be lost. Therefore, memcached cannot be used to persistently store data.

People who pass through php_memcache will think that the PHP memory cache is a very complicated thing, but it is not.
Memcached is an efficient and fast distributed memory object cache system, 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 from the attachment.

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!


Example:

The code is as follows:

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

// Option settings
$ Options = array (
'Servers' => array ('www .hxsd.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: Alternative PHP Cache, officially translated as "optional PHP Cache ".

Home is http://pecl.php.net/package/apc

Php help manual 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.

Installation:

The code is as follows:

# Pecl install APC

Configuration: (/etc/php. inc)

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


...

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.