In-depth analysis of php apc. Apc definition: apc is an open and free phpopcode cache. It aims to provide a free, open, and sound framework for caching and optimizing php intermediate code. Apc common functions: 1 apc definition: apc is an open and free php opcode cache. It aims to provide a free, open, and sound framework for caching and optimizing php intermediate code.
Common apc functions:
1. apc_clear_cache () clear apc cache content
2. apc_define_constants (string key, array constants, [, bool case_sensitive]) adds the array constants to the cache as constants.
3. apc_load_constants (string key) to retrieve the constant cache
4. apc_store (string key, mixed var [, int ttl]) stores data in the cache
5. apc_fetch (string key) obtains the cache data stored in apc_store.
6. apc_delete (string key) deletes the stored content of apc_store.
7. apc_add (string key, mixd var [, int ttl]) caches a variable to data storage (only when it is not stored before the variable)
8. check whether one or more apc key names exist in apc_exists (mix keys ).
9. apc_delete_file (mixed keys) deletes the cache of the specified file from the opcode cache
10. apc_compile_file (string filename [, bool atmic = true]) bypasses filters restrictions and caches files.
11. apc_cache_info (string cache_type [, bool limited = false ])
Note: apc_clear_cache only clears the opcode cache file, and apc_delete is clear about the variables in the cache. you can use the apc_define_constants function for pre-defined variables, and apc_store for php variables. apc is better than memcache, the Network Transmission Protocol (tcp) is not required. apc is not suitable for caching user data that frequently changes through the apc_store function. apc itself does not support distribution.
Common configurations of apc:
1. how much memory will be allocated to apc. ini options apc. shm_size (integer) control this setting. The default value is 30 MB.
2. check whether the apc file is modified in each request. The ini option is apc. stat controls this setting. the default value is 1, indicating whether the script is updated when the script is requested. if the script is updated, the compiled content is automatically re-compiled and cached, which has a proportional impact on the performance, therefore, this parameter is set to 0.
3. use the ini option apc. filters to cache fewer scripts.
Advantages of apc:
1. use the spin lock mechanism to achieve the best performance
2. apc provides apc. php for monitoring and managing apc cache. (Note: modify 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. adjust the values of apc. shm_size, apc. num_files_hints, and apc. user_entires_hint to the optimal value.
5. php pre-defined constants. you can use the apc _ define -- constants () function. However, apc developers say that pecl hidef is more efficient than define.
6. apc_store: for PHP variables such as system settings, the life cycle is the entire application (from the httpd daemon to the httpd daemon). it is better to use apc than memcache. (No network transmission protocol is required)
7. apc is not suitable for frequently changing user data through the apc_store () function, which may lead to some strange phenomena.
Example:
$ Constants = array ('apc _ file' => 'apc. php', 'author' => 'TIM ');
Apc_define_constants ('memb', $ constants );
Apc_load_constants ('memb ');
// Echo APC_FILE;
// Echo AUTHOR;
If (! Apc_fetch ('time1') apc_store ('time1', time ());
If (! Apc_fetch ('time2') apc_store ('time2', time (), 2 );
// Echo apc_fetch ('time1 ');
// Echo apc_fetch ('time2 ');
Class {
Public function B () {echo 'success ';}
}
Apc_store ('obj ', new ());
$ A = apc_fetch ('obj ');
// $ A-B ();
$ Ret = apc_exists (array ('foo', 'donotexist', 'bar '));
// Array (2) {["foo"] => bool (true) ["bar"] => bool (true )}
?>
Detailed configuration instructions:
Apc. cache_by_default = on
; Sys
Whether to enable caching for all files by default.
If it is set to off and used together with the apc. filters command starting with the plus sign, the file is cached only when the filter is matched.
Apc. enable_cli = off
; Sys
Whether to enable the apc function for the cli version. This command is enabled only for testing and debugging purposes.
Apc. enabled = on
Whether to enable apc. if apc is statically compiled into php and you want to disable it, this is the only way.
Apc. file_update_protection = 2
; Sys
When you modify a file on a running server, you should perform atomic operations.
That is, first write a temporary file, and then rename the file (mv) to the final name.
The text editor, cp, tar, and other programs do not perform this operation, which may buffer incomplete files.
The default value is 2, indicating that the file is not buffered if the modification time is found to be less than 2 seconds away from the access time.
The unfortunate visitor may get incomplete content, but this bad effect will not be extended through the cache.
If you can ensure that all update operations are atomic operations, you can disable this feature with 0.
; If your system updates slowly due to a large number of io operations, you need to increase this value.
Apc. filters =
; Sys
; A list of posix extension regular expressions separated by commas.
If the source file name matches any mode, the file is not cached.
Note: the file name used for matching is the file name passed to include/require, rather than the absolute path.
If the first character of the regular expression is "+", it means that any file that matches the expression will be cached,
If the first character is "-", no match will be cached. "-" Is the default value, which can be omitted.
Apc. ttl = 0
; Sys
The number of seconds that cache entries can stay in the buffer. 0 indicates never timeout. Recommended value: 7200 ~ 36000.
If it is set to 0, the buffer may be filled with old cache entries, resulting in the inability to cache new entries.
Apc. user_ttl = 0
; Sys
; Similar to apc. ttl, the recommended value is 7200 ~ for each user ~ 36000.
If it is set to 0, the buffer may be filled with old cache entries, resulting in the inability to cache new entries.
Apc. gc_ttl = 3600
; Sys
The number of seconds that cache entries can exist in the garbage collection table.
This value provides a security measure, even if a server process crashes when the cached source file is executed,
And the source file has been modified, and the memory allocated for the old version will not be recycled until the ttl value is reached.
If it is set to zero, this feature is disabled.
Apc. include_once_override = off
; Sys
; There is no instruction document for this directive, see: http://pecl.php.net/bugs/bug.php? Id = 8754
; Keep it off. otherwise, unexpected results may occur.
Apc. max_file_size = 1 m
; Sys
; Files larger than this size cannot be cached.
Apc. mmap_file_mask =
; Sys
If you use-enable-mmap (enabled by default) to compile mmap support for apc,
The value here is the mktemp file mask passed to the mmap module (recommended value: "/tmp/apc. xxxxxx ").
The mask is used to determine whether the memory ing area is file-backed or shared memory backed.
For direct file-backed memory ING, set it to "/tmp/apc. xxxxxx" (exactly 6 x ).
To use the posix shm_open/mmap style, you must set it to "/apc. shm. xxxxxx.
You can also set it to "/dev/zero" to use the kernel "/dev/zero" interface for anonymous ing memory.
If this command is not defined, anonymous ING is mandatory.
Apc. num_files_hint = 1000
; Sys
The approximate number of different source files that may be contained or requested on the web server (recommended value: 1024 ~ 4096 ).
If you are not sure, set it to 0. this setting is mainly used for websites with thousands of source files.
Apc. optimization = 0
; Optimization level (recommended value: 0 ).
The positive integer value indicates that the optimizer is enabled. the higher the value, the more radical optimization is used.
; A higher value may have a very limited speed increase, but it is still being tested.
Apc. report_autofilter = off
; Sys
; Whether to record all scripts that are automatically not cached due to early/late binding.
Apc. shm_segments = 1
; Sys
; The number of shared memory blocks allocated to the compiler buffer (recommended value: 1 ).
If apc consumes shared memory and the apc. shm_size command is set to the maximum value allowed by the system,
; You can try to increase this value.
Apc. shm_size = 30
; Sys
; The size of each shared memory block (in mb, recommended value: 128 ~ 256 ).
Some systems (including most bsd variants) have very few default shared memory blocks.
Apc. slam_defense = 0
; Sys (opposed to using this command, we recommend using the apc. write_lock command)
On very busy servers, whether it is to start the service or modify files,
; May be caused by multiple processes attempting to cache a file at the same time.
This command is used to set the percentage of cached steps skipped by a process when processing files that are not cached.
For example, if it is set to 75, it indicates that no cache is performed for files that are not cached, reducing the chance of collision.
We recommend that you set it to 0 to disable this feature.
Apc. stat = on
; Sys
; Whether to enable the script update check.
Be very careful when changing the command value.
The default value on indicates that apc checks whether the script is updated every time it requests the script,
If updated, the compiled content is automatically re-compiled and cached. However, this has a negative impact on performance.
If it is set to off, the check is not performed, which greatly improves the performance.
To make the updated content take effect, you must restart the web server.
This command is also valid for include/require files. However, you must note that,
If you are using a relative path, apc must check each include/require operation to locate the file.
You can skip the check using the absolute path. Therefore, you are encouraged to use the absolute path for the include/require operation.
Apc. user_entries_hint = 100
; Sys
; Similar to the num_files_hint command, only for different users.
If you are not sure, set it to 0.
Apc. write_lock = on
; Sys
Whether to enable the write lock.
On very busy servers, whether it is to start the service or modify files,
; May be caused by multiple processes attempting to cache a file at the same time.
; Enable this command to avoid competition conditions.
Apc. rfc1867 = off
; Sys
After this command is enabled, for each Upload file that contains the apc_upload_progress field just before the file field,
Apc will automatically create an upload _ User cache entry (that is, the value of the apc_upload_progress field ).
Opcode cache. It aims to provide a free, open, and sound framework for caching and optimizing php intermediate code. Common apc functions: 1...