Php apc cache details (learning and organizing)

Source: Internet
Author: User
Tags apc apc configuration apc module key string

1. APC cache Overview
APC, full name: Alternative PHP Cache, officially translated as "Optional PHP Cache ". It provides us with a framework for caching and optimizing PHP intermediate code. APC Cache consists of system cache and user data cache.
System cache
It indicates that APC caches the compilation results of the PHP file source code and compares the time mark for each call. If it does not expire, it runs with the cached intermediate code. Default Cache
3600 s (one hour ). However, this will still waste a lot of CPU time. Therefore, you can set the system cache in php. ini to never expire (apc. ttl = 0 ). However, you need to restart the WEB server after the php code is modified. This type of cache is usually used.
User Data Cache
The cache is read and written by users using the apc_store and apc_fetch functions when writing PHP code. If the data volume is small, you can try it. If the data volume is large, it would be better to use a more specific memory cache solution similar to memcache.
Cache key generation rules
Each slot in the APC cache has a key, and the key is
Apc_cache_key_t struct type. Apart from key-related attributes, the key is the generation of the h field. The h field determines where the element falls in the slots array. User cache and system cache have different generation rules. The user cache uses the apc_cache_make_user_key function to generate a key. The key string passed by the user depends on the hash function in the PHP kernel (the hash function used by PHP's hashtable: zend_inline_hash_func) to generate the h value.
The system cache uses the apc_cache_make_file_key function to generate a key. Different solutions are treated differently through the APC. stat switch, which is the configuration item of apc. When enabled, that is
If apc. stat = On is updated, the compiled content is automatically re-compiled and cached. The h value is the value obtained by adding the device and inode of the file. When apc. stat = off is disabled, the Web server must be restarted to make the updated content take effect after the file is modified. In this case, the h value is generated based on the file path address, and the path here is the absolute path. Even if you are using a relative path, you will also find the PG (shortde_path) location file to get the absolute path. Therefore, using the absolute path will skip the check, which can improve the code efficiency.
Add cache Process
Taking the user cache as an example, the apc_add function is used to add content to the APC cache. If the key parameter is a string, APC generates a key based on the string. If the key parameter is an array, APC traverses the entire array to generate a key. Based on these keys, APC will call _ apc_store to store values in the cache. Because this is the user cache, the current cache is apc_user_cache. The write operation is the apc_cache_make_user_entry function, which finally calls apc_cache_user_insert to perform traversal query and write operations. Correspondingly, when the system cache uses apc_cache_insert to execute the write operation, it will eventually call _ apc_cache_insert.
The execution process is similar to that of the user cache or system cache. The steps are as follows:

Perform the remainder operation to locate the location of the current key in the slots array: cache-> slots [key. h % cache-> num_slots];
After locating the slots array, traverse the slot linked list corresponding to the current key. If the slot key matches the key to be written or the slot expires, clear the current slot.
Insert a new slot after the last slot.
2. Installation of APC Module
Step 1: Download php_apc.dll in the to correspond to the php version to put php_apc.dll into your ext directory

Step 2: Let php. ini support the apc extension module. Then open php. ini and add:

Extension = php_apc.dll

Apc. rfc1867 = on

Apc. max_file_size = 100 M

Upload_max_filesize = 100 M

Post_max_size = 100 M

// You can customize the preceding parameters.

Step 3: Check whether php apc apc_store apc_fetch php apc configuration parameters are supported and put the relevant configurations together for explanation.

Apc. enabled = 1 apc. enabled: The default value is 1. You can set it to 0 to disable APC. If you set it to 0, you can also comment out extension = apc. so (this can save memory resources ). Once the APC function is enabled, the Opcodes will be cached to the shared memory.

Apc. shm_segments = 1

Conclusion 1: the use of the Spinlocks lock mechanism can achieve the best performance.

2. APC provides apc. php for monitoring and managing the APC cache. Do not forget to change the Administrator name and password

3. By default, APC creates shared memory through mmap anonymous ing, and cache objects are stored in this "large" memory space. APC manages the shared memory

4. We need to adjust the values of apc. shm_size, apc. num_files_hints, and apc. user_entries_hint through statistics. Until the best

5. Well, I admit that apc. stat = 0 can achieve better performance. Everything I want to do is acceptable.

6. PHP pre-defined constants. You can use the apc_define_constants () function. However, according to APC developers, the performance of pecl hidef is better, and define is inefficient.

7. The apc_store () function. For PHP variables such as system settings, the life cycle is the whole application (from the httpd daemon process to the httpd daemon). APC is better than Memcached. It must not pass through the network transmission protocol tcp.

8. APC is not suitable for caching user data that frequently changes through the apc_store () function, and some strange phenomena may occur.


Tar-zxvf APC-3.1.8.tgz cd APC-3.1.8


. /Configure -- enable-apc -- enable-mmap -- enable-apc-spinlocks -- disable-apc-pthreadmutex -- with-php-config =/usr/local/php/bin/php- config


Sudo make install

Add/usr/local/php/etc/php. ini

Extension = "apc. so ";

; APC setting

Apc. enabled = 1

Apc. shm_segments = 1

Apc. shm_size = 64 M

Apc. optimization = 1

Apc. num_files_hint = 0

Apc. ttl = 0

Apc. gc_ttl = 3600

Apc. cache_by_default = on

Restart apache or/usr/local/php/sbin/php-fpm restart

View phpinfo apc

The following reference is the APC cache class of framework.
APC cache in initphp framework
<? Php
If (! Defined ('is _ initphp') exit ('Access Denied! ');
/*************************************** **************************************** **
* InitPHP 2.0 Chinese PHP development framework Dao-APC cache is not suitable for frequently written cache data
* CopyRight: CopyRight By
* You can use the source code freely, but keep the author information during use. Respecting others means respecting yourself.
* $ Author: zhuli
* $ Dtime: 2011-10-09
**************************************** **************************************** ***/
Class apcInit {

* Apc cache-set Cache
* Set the cache key, value, and cache time.
* @ Param string $ key KEY value
* @ Param string $ value
* @ Param string $ time Cache time
Public function set_cache ($ key, $ value, $ time = 0 ){
If ($ time = 0) $ time = null; // permanent cache when null
Return apc_store ($ key, $ value, $ time );;

* Apc cache-Get Cache
* Obtain cache data using the KEY
* @ Param string $ key KEY value
Public function get_cache ($ key ){
Return apc_fetch ($ key );

* Apc cache-Clear a cache
* Delete A cache from memcache
* @ Param string $ key KEY value
Public function clear ($ key ){
Return apc_delete ($ key );

* Apc cache-clear all caches
* This function is not recommended.
* @ Return
Public function clear_all (){
Apc_clear_cache ('user'); // clear the user Cache
Return apc_clear_cache (); // clear Cache

* Check whether the APC cache exists.
* @ Param string $ key KEY value
Public function exists ($ key ){
Return apc_exists ($ key );

* Field Auto-increment-used for counting
* @ Param string $ key KEY value
* @ Param int $ step the new step value
Public function inc ($ key, $ step ){
Return apc_inc ($ key, (int) $ step );

* Field Auto-subtraction-used for counting
* @ Param string $ key KEY value
* @ Param int $ step the new step value
Public function dec ($ key, $ step ){
Return apc_dec ($ key, (int) $ step );

* Return APC Cache Information
Public function info (){
Return apc_cache_info ();

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: 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.