PHP APC cache describes apc module installation in detail

  • 2020-12-13 18:55:27
  • OfStack

1. Introduction to APC cache

APC, full name Alternative PHP Cache, is officially translated as "Optional PHP cache ". It provides us with a framework for caching and optimizing PHP's intermediate code. APC's cache has two parts: system cache and user data cache.
System cache
It means that APC caches the compiled results of the PHP file source code, and then compares the time tags on each invocation. If it is not expired, it is run using the cached intermediate code. Default cache 3600s (1 hour). But that would still waste a lot of CPU time. So you can set the system cache in php.ini to never expire (apc.ttl =0). However, if you do this, you will need to restart the WEB server after rerunning the php code. The most commonly used cache is of this type.
User data cache
The cache is read and written by the user using the apc_store and apc_fetch functions while writing PHP code. If the amount of data is small, try 1. If the data volume is large, it is better to use a more specialized memory caching scheme like memcache
Cache key generation rules
For every slot in APC's cache, there will be one key. key is the apc_cache_key_t structure type. In addition to the key-related attributes, the key is the generation of the h field. The h field determines where this element falls in the slots array. For user cache and system cache, the rules of generation are different. The user cache generates key through the apc_cache_make_user_key function. The h value is generated by the key string passed in by the user, relying on the hash function in the PHP kernel (hash used by hashtable of PHP: zend_inline_hash_func).
The system cache generates key using the apc_cache_make_file_key function. Use the switch on apc.stat configuration item to discriminate between different schemes. In the case of open, apc. stat= On, if updated, the compiled content is automatically recompiled and cached. The value of h at this time is the value obtained by adding device and inode of the file. In the case of shutdown, apc. stat=off, when the file has been modified, the Web server must be restarted for 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, look for the PG (include_path) location file to get the absolute path, so using an absolute path skips the check and makes your code more efficient.
Add caching process
In the case of the user cache, the apc_add function is used to add content to the APC cache. If the key parameter is in a string, APC will generate key based on that string, and if the key parameter is an array, APC will traverse the entire array to generate key. Based on these key,APC will call _apc_store to store the value in the cache. Since this is the 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 calls apc_cache_user_insert to perform the traversal query and write operation. In contrast, the system cache uses apc_cache_insert to perform the write operation, which ends up calling _apc_cache_insert.
Whether the user cache or the system cache, the general implementation process is similar, and the steps are as follows:
Locate the current position of key in the slots array by a remainder operation: cache- > slots[key.h % cache- > num_slots];
After locating the position in the slots array, traverse the linked list of slot corresponding to the current key, and clear the current slot if there is a match between key of slot and the key to be written or slot expires.
Insert the new slot after the last slot.

2. Installation of APC module

WINDOWS

Step 1: download php_apc dll in/pecl php. net package/apc with php version will php_apc. The corresponding dll into your ext directory

Step 2: Get ES165en. ini to support apc extension modules. Then open php.ini to add:


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 3: Check to see if PHP APC apc_store apc_fetch PHP APC configuration parameter puts the relevant configuration at 1 point of interpretation.


apc.enabled=1 apc.enabled The default value is 1, You can set 0 disable APC. If you set it to 0 When, also put extension=apc.so Also comment it out (to save memory). 1 Denier is enabled APC Function, will cache Opcodes To shared memory. 
apc.shm_segments = 1

In summary 1, the Spinlocks locking mechanism achieves optimal performance.
2. APC provides ES189en.php for monitoring and managing APC cache. Don't forget to change the administrator name and password
3. By default,APC creates shared memory through mmap anonymous mapping, and cache objects are stored in this "large" memory space. APC managed the shared memory itself
4. We need to adjust the value of ES197en.shm_size, ES200en.num_files_ES203en and apc.user_ES206en_ES207en by statistics. Until the best
5. Well, I admit that apc.stat = 0 gets better performance. I can do anything.
6,PHP predefined constants, you can use apc_define_constants () function. However, pecl hidef performs better, according to the APC developers. Throw out define, which is inefficient.
7, apc_store (), for system Settings such as PHP variables, the life cycle is the entire application (from httpd daemons until httpd daemons are shut down), using APC is better than Memcached. Do not use the network transport protocol tcp.
8,APC is not suitable for caching frequently changing user data through the function apc_store (), a few oddies occur.

LIUNX


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
 in /usr/local/php/etc/php.ini  join 
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


Related articles: