Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Configuration and usage of PHP APC cache

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the configuration and use of PHP APC cache". In the daily operation, I believe that many people have doubts about the configuration and use of PHP APC cache. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "configuration and use of PHP APC cache". Next, please follow the editor to study!

I. introduction to APC caching

APC, whose full name is Alternative PHP Cache, is officially translated as "optional PHP cache". It provides a framework for caching and optimizing the intermediate code of PHP. The cache of APC is divided into two parts: system cache and user data cache.

System caching

It means that APC caches the compilation results of the source code of the PHP file, and then compares the time stamp with each call. If it does not expire, it runs using the cached intermediate code. Default cach

3600s (one hour). But it still wastes a lot of CPU time. So you can set the system cache to never expire (apc.ttl=0) in php.ini. However, if this is set, the WEB server needs to be restarted after changing the php code. At present, this kind of cache is widely used.

User data caching

Cache is read and written by users using apc_store and apc_fetch functions when writing PHP code. If the amount of data is small, you can give it a try. If the amount of data is large, it would be better to use a more monographed memory caching scheme such as memcache

Cache key generation rules

Each slot in APC's cache has a key,key that 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. The generation rules are different for user caches and system caches. The user cache generates key through the apc_cache_make_user_key function. Through the key string passed in by the user, the h value is generated by relying on the hash function in the PHP kernel (the hash function used by PHP's hashtable: zend_inline_hash_func).

The system cache generates key through the apc_cache_make_file_key function. Different schemes are treated differently through the switch of the configuration item apc.stat of APC. When open, that is,

When apc.stat= On, if updated, the compiled content is automatically recompiled and cached. The h value at this time is the value obtained by adding the device and inode of the file. In the case of shutdown, that is, apc.stat=off, when the file is modified, if you want the updated content to take effect, you must restart the Web server. In this case, 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 the relative path, you will look for the PG (include_path) location file to get the absolute path, so using the absolute path will skip the check and improve the efficiency of the code.

Add caching 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 in a string, APC will generate key from this string, and if the key parameter is an array, APC will traverse the entire array to generate key. Based on these key,APC, _ 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. It is the apc_cache_make_user_entry function that performs the write operation, which finally calls apc_cache_user_insert to perform the traversal query and write operation. In contrast, the system cache uses apc_cache_insert to perform write operations, which eventually calls _ apc_cache_insert.

Whether it is user cache or system cache, the execution process is similar, and the steps are as follows:

Locate the position of the current key in the slots array through the remainder operation: cache- > slots [key.h% cache- > num_slots]

After navigating to the location in the slots array, traverse the slot linked list corresponding to the current key, and clear the current slot if there is a match between the key of the slot and the key to be written or if the slot expires.

Insert a new slot after the last slot.

II. Installation of APC module

Install APC under A.WINDOWS

Step 1: download php_apc.dll in http://pecl.php.net/package/apc and put php_apc.dll in your ext directory corresponding to the php version.

Step 2: let php.ini support the apc extension module. Then open php.ini to join:

The copy code is 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 yourself

Step 3: check whether PHP APC apc_store apc_fetch is supported

Check to see if there are apc related items in phpinfo

Install APC under B.LIUNX

Step 1: download and install

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

Step 2: configure APC

Add the following configuration items to / usr/local/php/etc/php.ini:

The copy code is as follows:

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

Step 3: check whether the installation is successful

Restart apache or / usr/local/php/sbin/php-fpm restart

Check to see if there are apc related items in phpinfo

Third, detailed explanation of configuration parameters and summary of use

1). Detailed explanation of parameter configuration of APC module

The copy code is as follows:

Apc.enabled Boolean type

Apc.enabled can be set to 0 to disable APC. This is mainly useful when APC is statically compiled into PHP, because there is no other way to disable it (when compiled to DSO, you can comment out the extension line in php.ini).

Apc.shm_segments integer

Allocates the number of shared memory blocks to the compilation cache. If APC runs out of shared memory and you have set apc.shm_size to the maximum allowed by the system, you can try to increase the value of this parameter.

Apc.shm_size integer

The size of each shared memory block is measured in MB. By default, the size limit of shared memory blocks for some systems (including most BSD variants) is very low.

Apc.optimization integer

Optimization level. Set to 0 to disable optimization, and the higher the value, the more powerful the optimization. Expect a moderate improvement in speed. This is still experimental.

Apc.num_files_hint integer

Prompt for the number of different source files that are included and requested on your Web server. If you are not sure, set it to 0 or omit it; this setting may be used mainly for sites with thousands of source files.

Apc.ttl integer

When a cache entry in the cache location is required by another entry, we need to consider the number of seconds that the cache entry is allowed to be idle in the cache location. Setting this parameter to 0 means that your cache may be full of stale entries, resulting in new entries that cannot be cached.

Apc.gc_ttl integer

The number of seconds the cache entry survived in the garbage collection list. This value provides error protection in events where a cache source file is executed while the server process dies. If that source file is modified, cache entries allocated to the old version will not be reclaimed until the TTL value set by this parameter arrives. Setting to 0 disables this feature.

Apc.cache_by_default Boolean type

The default is On, but it can be set to Off and used with apc.filters that starts with a plus sign, and files are cached only when the filter is matched.

Apc.filters string

A list of POSIX extended regular expressions separated by commas. If any pattern matches the source file name, the file will not be cached. Note that the file name used to match is the file name passed to include/require, not the absolute path. If the first character of a regular expression is +, the expression means that any file that matches the expression will be cached, and if the first character is -, no matches will be cached. -is the default value, so it can be omitted.

Apc.mmap_file_mask string (this paragraph is really not well understood, so there is no translation)

If compiled with MMAP support by using-- enable-mmap this is the mktemp-style file_mask to pass to the mmap module for determing whether your mmap'ed memory region is going to be file-backed or shared memory backed. For straight file-backed mmap, set it to something like/tmp/apc.XXXXXX (exactly 6 Xs). To use POSIX-style shm_open/mmap put a .shm somewhere in your mask. E.g. / apc.shm.XXXXXX You can also set it to / dev/zero to use your kernel's/dev/zero interface to anonymous mmap'ed memory. Leaving it undefined will force an anonymous mmap.

Apc.slam_defense integer

On a very busy server, whether you start the service or modify the file, you will lead to a competition in which multiple processes try to cache the same file at the same time. This option sets the percentage that the process skips trying to cache a file that has not been cached. Or think of this as the probability that a single process will skip the cache. For example, setting apc.slam_defense to 75 means that the process has a 75% chance of not caching files that are not cached. Therefore, the higher the setting, the more you can reduce the collision probability of the cache. Set to 0 to disable this feature.

Apc.file_update_protection integer

When you modify files on a running server, you should perform atomic operations. That is, write a temporary file first, and then rename (mv) the file to its final location when it is finished. Many text editors, cp,tar, and other similar programs do not operate in this way. This means that there is a chance to access the and (cache) file while the file is still being written. The setting of apc.file_update_protection causes the cache to mark the delay of new files. The default value is 2, which means that if it is found that the modification time of the file is less than 2 seconds from the access time, the file will not be cached. Unfortunate users who access files halfway through writing will see a bizarre situation, but at least it is not continuous. If you are sure that you often use atomic operations to update your files, you can turn off this protection by setting this parameter to 0. If your system is full of io operations and causes the update to take more than 2 seconds, you may need to increase this value.

Apc.enable-cli integer

Mostly for testing and debugging. Enable the APC feature for the CLI version of PHP. In general, you wouldn't want to create, migrate, and discard APC caches for every CLI request, but for various test cases, it's easy to turn on APC for the CLI version.

2)。 Usage summary

1. The best performance can be achieved by using Spinlocks locking mechanism.

2Magee APC provides apc.php for monitoring and managing APC caches. Don't forget to change the administrator name and password

3Gravity APC creates shared memory by default through mmap anonymous mapping, and cache objects are stored in this "large" memory space. APC manages the shared memory on its own

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 get better performance. Whatever you want me to do is acceptable.

6 PHP predefined constants, you can use the apc_define_constants () function. However, according to APC developers, pecl hidef has better performance, so instead of define, it is inefficient.

7, function apc_store (), for PHP variables such as system settings, the life cycle is the entire application (from the httpd daemon to the httpd daemon shuts down), using APC is better than Memcached. Must not go through the network transport protocol tcp.

8Jing APC is not suitable for caching frequently changed user data through the function apc_store (), and some strange phenomena will occur.

IV. Use examples

The following references the APC cache class of the initphp framework

The copy code is as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report