1, APC Cache introduction
APC, the full name is alternative PHP cache, official translation is called "Optional PHP Caching." 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 the APC caches the compiled results of the PHP file source code, and then compares the time tag with each call. If it is not expired, it is run using the cached intermediate code. The default cache is 3600s (one hour). But it still wastes 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 need to restart the Web server after you change the PHP code. This type of caching is currently used more often.
User Data caching
Caching is read and written by the user when writing PHP code using the Apc_store and Apc_fetch function operations. If the amount of data is small, you can try. If the volume of data is large, it is better to use a more specialized memory caching scheme similar to Memcache
Cache key Generation Rule
Each slot in the APC cache has a key,key that is a apc_cache_key_t struct type, except for key-related attributes, which is the creation of the H field. The H field determines which position this element falls in the slots array. The generation rules for user caching and system caching are different. User caching generates keys through the Apc_cache_make_user_key function. A key string passed in by the user relies on the hash function in the PHP kernel (the hash function used by the PHP Hashtable: Zend_inline_hash_ Func) to generate H values.
The system cache generates key through the Apc_cache_make_file_key function. The apc.stat switch of the APC's configuration item treats different schemes differently. When opened, that is, apc.stat= on, the compiled content is automatically recompiled and cached if updated. The H value at this time is the value of the device of the file and the addition of the inode. In the case of shutdown, that is, when the file is modified, the Web server must be restarted if you want the updated content to take effect Apc.stat=off. At this point the H value is generated from the path address of the file, and the path here is an absolute path. Even if you are using a relative path, you will find the PG (include_path) location file to get an absolute path, so skipping the check using an absolute path can improve the efficiency of your code.
Adding caching procedures
In user caching, for example, the Apc_add function is used to add content to the APC cache. If the key argument is a string, the APC generates a key based on this string, and if the key argument is an array, the APC traverses the entire array and generates the key. According to these KEY,APC, the _apc_store is called to store the value 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, which ultimately invokes the Apc_cache_user_insert to perform traversal queries and write operations. In this correspondence, the system cache performs a write operation using Apc_cache_insert, which eventually invokes _apc_cache_insert.
Whether it's a user cache or a system cache, the general execution process is similar, and the steps are as follows:
Locates the position of the current key in the slots array by means of a remainder operation: cache->slots[key.h% cache->num_slots];
After locating the position in the slots array, traverse the slot list of the current key, and clear the current slot if there is a slot key and a key match to write or slot expires.
Insert the new slot after the last slot.
2. APC Module installation
Windows
The first step: Download Php_apc.dll in/PECL.PHP.NET/PACKAGE/APC to correspond with the PHP version will php_apc.dll into your ext directory
Step two: Let php.ini support the APC extension module. Then open php.ini and join:
Copy Code code as follows:
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 to see if support for PHP APC apc_store apc_fetch PHP APC configuration parameters to explain the related configuration.
Copy Code code as follows:
Apc.enabled=1 apc.enabled The default value is 1, you can set the 0 disable APC. If you set it to 0, you can also annotate extension=apc.so (this saves memory resources). Once the APC feature is enabled, opcodes to shared memory is cached.
Apc.shm_segments = 1
Summary 1, using spinlocks lock mechanism, to achieve the best performance.
The 2,APC provides apc.php for monitoring and managing the APC cache. Don't forget to change the administrator name and password
3,APC By default creates shared memory by Mmap Anonymous mappings, which are stored in this "large" memory space. This shared memory is managed by the APC itself
4, we need to adjust the values of apc.shm_size, Apc.num_files_hints, apc.user_entries_hint by statistics. Until the best
5, OK, I admit Apc.stat = 0 can achieve better performance. Anything I can do is acceptable.
6,php predefined constants, you can use the apc_define_constants () function. However, according to the APC developers said PECL HiDef performance better, throw a different define it, it is inefficient.
7, Function Apc_store (), for the system settings such as PHP variables, the lifecycle is the entire application (from the httpd daemon until the httpd daemon shutdown), the use of APC than memcached will be better. Must not go through the Network Transport Protocol TCP.
8,APC is not suitable for user data that is frequently changed through function Apc_store () caching, there are some strange phenomena.
Liunx
Copy Code code as follows:
wget 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