Php memory cache Implementation Method

Source: Internet
Author: User
Tags apc
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, see

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, see

This article describes how to implement the php memory cache. 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

2. Run the command to run memcached.exe-d start.

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 .jb51.net: 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, the full name is Alternative PHP Cache, which is officially translated as "Optional PHP Cache". The home page is, the php help manual page:

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.

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.