Php apc cache details apc module installation

Source: Internet
Author: User
Tags apc apc configuration apc module key string
This article describes how to install the APC module in windows and linux.

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


Step 1: Download php_apc.dll in/ 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:
The 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.
The 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.


The code is as follows:
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

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.