1. APC cache Overview
APC, short for Alternative PHP Cache, is 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. The default cache is 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 dedicated memory cache solution similar to memcache.
Cache key generation rules
Each slot in the APC cache has a key. The key is of the apc_cache_key_t struct type. In addition to 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 the key. The key string passed by the user depends on the hash function in the PHP kernel (the hash function used by PHP 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. The APC. stat switch of the apc configuration item treats different solutions differently. When apc. stat = On is enabled, the compiled content is automatically re-compiled and cached if it is updated. 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 performed by 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
WINDOWS
Step 1: Download php_apc.dll in/pecl.php.net/package/apc and put php_apc.dll in your ext directory.
Step 2: Let php. ini support the apc extension module. Then open php. ini and add:
Copy codeThe Code is as follows:
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.
Copy codeThe Code is as follows:
Apc. enabled = 1 apc. the default value of enabled is 1. You can set it to 0 to disable APC. if you set it to 0, extend = apc. so is also commented out (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.
LIUNX
Copy codeThe Code is 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
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