A detailed introduction to the APC cache in PHP (Learn to organize)

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

Introduction of APC

The alternative PHP cache (APC) is a free and open opcode cache for PHP. Its goal are to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

APC Official website: http://www.php.net/manual/en/book.apc.php

Win under the latest version of: Http://downloads.php.net/pierre/

Download Connection: Http://downloads.php.net/pierre/php_apc-3.1.4-5.3-VC6-x86.zip use the corresponding DLL according to your PHP build version

1, APC Cache introduction APC, the full name is alternative PHP cache, the official translation is called "Optional PHP cache." It provides us with a framework for caching and optimizing PHP's intermediate code. The APC cache is divided into two parts: the system cache and the user data cache. System cache It means that APC caches the compiled results of the PHP file's source code, and then compares the time stamp with each call. If it does not expire, it is run with the cached intermediate code. The default cache is 3600s (one hour). However, this can still waste a lot of CPU time. Therefore, you can set the system cache to never expire (apc.ttl=0) in php.ini. However, if you do this, you will need to restart the Web server after changing the PHP code. This type of cache is currently used more often. User data cache caching is read and written by the user using the Apc_store and Apc_fetch functions when writing PHP code. If you have a small amount of data, you can try it. If the amount of data is large, a more monograph memory caching scheme similar to that of Memcache would better cache key generation rules each slot in the APC cache has a key,key that is a apc_cache_key_t struct type, except for key-related properties, The key is the generation of the H field. The H field determines which position of this element falls in the slots array. There are different generation rules for the user cache and the system cache. The user cache generates a key through the Apc_cache_make_user_key function. Using the key string passed in by the user, depending on the hash function in the PHP kernel (the hash function used by PHP's Hashtable: Zend_inline_hash_func), the H value is generated. The system cache generates a key through the Apc_cache_make_file_key function. Different scenarios are differentiated by the switch of the APC configuration item Apc.stat. In the case of open, apc.stat= on, the compiled content is automatically recompiled and cached if it is updated. The H value at this point is the added value of the file's device and Inode. In the case of Apc.stat=off, when the file is modified, you must restart the Web server if you want the updated content to take effect. At this point the H value is generated based on the path address of the file, and the path here is the absolute path. Even if you are using a relative path, you will find the PG (include_path) location file to obtain an absolute path, so using an absolute path skips the check and can improve the efficiency of your code. Common APC Settings
Apc.cache_by_default Caching is enabled by default. 1 means "enabled" and 0 means "disabled"
Apc.filters The comma-delimited POSIX regular expression determines whether the file needs to be cached or not. A regular that starts with a +
An expression will force APC not to cache any files that match this regular expression. A regular expression that begins with a-a will force
APC caches any files that match this regular expression
Apc.stat Enables or disables APC checks for all requests for PHP scripts for changes. This is performed every time the script is invoked.
Process. If you disable this setting, you will need to restart the Web server after making any changes to the PHP script to
Clears the cache and changes the contents of the script. 0= disabled, 1 = enabled, default 1
Apc.enabled Enables or disables the APC cache. 0= disabled, 1 = enabled, default 1
Apc.shm_size Sets the shared memory size that is allowed by APC, which is measured in megabytes
Apc.shm_segments To set the total number of shared memory segments available
Apc.include_once_override Enables or disables optimizations for include_once and require_once. When you enable this setting, you can reduce the additional system calls made by PHP intrinsics. 0= disabled, 1 = enabled, default 0
Apc.optimization Set the optimization level. 0= Disabling the optimization feature
Apc.num_files_hint Set the number of files you think you need to cache. The default value is 1000, and if you are unsure of the number of files, you can set 0
Apc.ttl Sets the expiration time, in seconds, that the file is stored in the cache.
Apc.write_lock Turning on this setting forces a single process to cache a specific footstep. For high-traffic Web servers or applications that must cache multiple files
The Add cache procedure takes the user cache as an example, and the Apc_add function is used to add content to the APC cache. If the key parameter is in a string, APC generates a key based on this string, and if the key parameter is an array, the APC traverses the entire array, generating the key. Based on these key,apc, the _apc_store is called to store the values in the cache. Because this is a user cache, the cache currently in use is Apc_user_cache. The write operation is performed by the Apc_cache_make_user_entry function, whose final call Apc_cache_user_insert performs a traversal query and write operation. In response, the system cache uses Apc_cache_insert to perform write operations, which eventually call _apc_cache_insert.

Whether the user cache or the system cache, the general process is similar, the steps are as follows:

    1. Position the current key in the slots array by finding the remainder operation: cache->slots[key.h% cache->num_slots];
    2. After locating the position in the slots array, iterate through the slot list of the current key, clearing the current slot if there is a key in the slot and a key match to write or the slot expires.
    3. Insert a new slot after the last slot.
2. APC Module installation Windows

First step: Download Php_apc.dll in HTTP://PECL.PHP.NET/PACKAGE/APC to correspond with PHP version Php_apc.dll into your ext directory

Step two: Let PHP.ini support the APC expansion module. Then open php.ini join:

Extension=php_apc.dll

apc.rfc1867 = On

Apc.max_file_size = 100M

Upload_max_filesize = 100M

Post_max_size = 100M

The above parameters can be defined by themselves

Step three: Check if PHP APC apc_store apc_fetch PHP APC configuration parameters are supported to interpret the relevant configuration.

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, also comment out the extension=apc.so (which saves memory resources). Once the APC feature is enabled, opcodes to shared memory is cached.

Apc.shm_segments = 1

In summary 1, the best performance can be achieved using the spinlocks lock mechanism.

The 2,APC provides apc.php for monitoring and managing the APC cache. Don't forget to change your administrator name and password

3,APC By default, the shared memory is created by Mmap Anonymous mappings, and the cache objects are stored in this "large" memory space. The shared memory is managed by APC itself

4, we need to adjust the value of Apc.shm_size, Apc.num_files_hints, apc.user_entries_hint by statistics. Until the best

5, okay, I admit that Apc.stat = 0 can achieve better performance. Anything I can do is acceptable.

6,php pre-defined constants, you can use the apc_define_constants () function. However, according to APC developers said PECL HiDef better performance, define it, it is inefficient.

7, Function Apc_store (), for PHP variables such as system settings, the lifecycle is the entire application (from the httpd daemon until the httpd daemon shuts down), using APC is better than memcached. Must not go through the Network Transport Protocol TCP.

8,APC is not suitable for caching frequently changed user data through the function Apc_store (), there are some strange phenomena.

Liunx

wget http://pecl.php.net/get/APC-3.1.8.tgz

TAR-ZXVF apc-3.1.8.tgz CD APC-3.1.8

/usr/local/php/bin/phpize

./configure--ENABLE-APC--enable-mmap--enable-apc-spinlocks--disable-apc-pthreadmutex--with-php-config=/usr/ Local/php/bin/php-config

Make

sudo make install

Join in/usr/local/php/etc/php.ini

Extension = "apc.so";

; APC setting

apc.enabled = 1

Apc.shm_segments = 1

Apc.shm_size = 64M

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 is an APC cache class that references the APC cache class initphp framework of the www.initphp.com framework
<?php if (!defined (' is_initphp ')) exit (' Access denied! ');  /********************************************************************************* * InitPHP 2.0 domestic PHP development Framework DAO-APC cache data not suitable for frequent write *-------------------------------------------------------------------------------* All rights reserved: CopyRight by initphp.com * You are free to use the source code, but in the course of use, please keep the author information. Respect for the fruits of others ' work is respect for yourself *-------------------------------------------------------------------------------* $Author: Zhuli * $ Dtime:2011-10-09 ***********************************************************************************/Class Apcinit {/** * APC Cache-Set Cache * Set Cache Key,value and Cache time * @param string $key key value * @param stri           ng $value Value * @param string $time Cache time */Public function Set_cache ($key, $value, $time = 0) { if ($time = = 0) $time = null;      Null in the case of permanent cache return Apc_store ($key, $value, $time);;  }/** * APC Cache-Get Cache * Get cached data via key * @param string $key Key value */Public Function Get_cache ($key) {return apc_fetch ($key); /** * APC Cache-Clears a cache * Deletes a cache from Memcache * @param string $key key value */public functio      N Clear ($key) {return apc_delete ($key); }/** * APC Cache-Empty all caches * This feature is not recommended * @return */Public Function Clear_all () {AP C_clear_cache (' user '); Clear User Cache return Apc_clear_cache (); Clear Cache}/** * Check if APC cache exists * @param string $key key value */Public function exists ($key      ) {return apc_exists ($key); }/** * Field self-increment-used to count * @param string $key key value * @param int $step New Step value */public      Function Inc ($key, $step) {return apc_inc ($key, (int) $step); }/** * Field decrement-Used to count * @param string $key key value * @param int $step New Step value */public function Dec ($key, $step) {return Apc_dec ($key, (int) $step);      }/** * Returns APC cache information */Public Function info () {return apc_cache_info ();   }  }

A detailed introduction to the APC cache in PHP (Learn to organize)

Related Article

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.