SoFunction
Updated on 2025-04-07

In-depth analysis of php apc

apc definition: apc is an open and free php opcode cache. Its goal is to provide a free, open and sound framework for caching and optimizing php intermediate code.
Common functions for apc:
1.apc_clear_cache() to clarify the content of the apc cache
2.apc_define_constants(string key,array constants,[,bool case_sensitive])   Add array constants to cache as constants
3.apc_load_constants(string key) Get out the constant cache
4.apc_store(string key,mixed var [, int ttl])  Save data in cache
5.apc_fetch(string key) Get the cached data saved by apc_store
6.apc_delete(string key) Delete content saved by apc_store
7.apc_add(string key,mixd var [, int ttl]) caches a variable to the data storage (only if the variable is not stored before it)
8.apc_exists(mix keys) Check whether one or more apc key names exist
9.apc_delete_file(mixed keys) Delete the cache of a given file from the opcode cache
10.apc_compile_file(string filename [,bool atmic=true]) Bypass filters' restrictions and cache files
11.apc_cache_info(string cache_type [,bool limited=false]) Get cache
Note: apc_clear_cache only clears the opcode cache file, apc_delete clearly understands the variables in the cache; predefined variables, you can use the apc_define_constants function; php variables can use the function apc_store, and it is better to use apc than memcache, and does not require the network transmission protocol tcp; apc is not suitable for cache frequently changed user data through the function apc_store, and some strange phenomena will occur; apc itself does not support distributed.
Common configurations for apc:
1. How much memory will be allocated to the apc, the ini option apc.shm_size (integer) controls this setting. Default is 30M
2. Whether the apc checks file modification every time, the ini option controls this setting. The default value is 1, indicating whether the production reduction script is updated every time the script is requested. If it is updated, the compiled content will be automatically recompiled and cached, which has a proportional impact on performance, so this is set to 0
3. Cache fewer scripts through the ini option
Advantages of apc:
1. Use spinlocks (spin) lock mechanism to achieve optimal performance
Provided for monitoring and managing apc cache. (Note: Change the administrator name and password)
By default, shared memory is created through mmap anonymous mapping, and cache objects are stored in this large memory space. The shared memory is managed by the apc
4. Adjust the values ​​of apc.shm_size, apc.num_files_hints, apc.user_entires_hint to the best
For predefined constants, you can use the apc _define-constants() function. However, the apc developer said that pec hidef has more performance and abandons definition, it is inefficient
6.apc_store. For system settings and other PHP variables, the life cycle is the entire application (from httpd daemon to httpd daemon closing). Using apc is better than memcache. (No need to go through the network transmission protocol)
It is not suitable for frequently changing user data through the function apc_store() cache, and some strange phenomena will occur.
example:
<?php
$constants = array('APC_FILE'=>'','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 a{
     public function b(){echo 'success';}
}
apc_store('obj',new a());
$a = apc_fetch('obj');
//$a-b();
 
$ret = apc_exists(array('foo', 'donotexist', 'bar'));
//array(2) { ["foo"]=> bool(true) ["bar"]=> bool(true) }
?>
Additional detailed configuration instructions:
apc.cache_by_default = on
;sys
; Whether to enable buffering for all files by default.
; If set to off and used with instructions starting with a plus sign, the file is cached only when matching the filter.
 
apc.enable_cli = off
;sys
; Whether to enable the apc function for the cli version, open this directive only for testing and debugging purposes.
 
= on
; Whether to enable apc, if apc is statically compiled into php and wants 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 its final name.
; Text editors and programs such as cp, tar do not operate in this way, resulting in the possibility of buffering of broken files.
; Default value 2 means that if the modification time is found to be less than 2 seconds when accessing the file, no buffering will be done.
; That unfortunate visitor may get the broken content, but this bad effect will not be expanded through cache.
; If you can make sure that all update operations are atomic operations, you can turn off this feature with 0.
; If your system is slow to update due to a large number of io operations, you need to increase this value.
 
=
;sys
; A comma-separated list of posix extension regular expressions.
; If the source file name matches any pattern, the file is not 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 the regular expression is "+", it means that any file matching the expression will be cached.
; If the first character is "-", no match will be cached. "-" is the default value and can be omitted.
 
= 0
;sys
; Number of seconds allowed by cache entries in the buffer. 0 means that the timeout will never be exceeded. The recommended value is 7200~36000.
; Set to 0 means that the buffer may be filled with old cache entries, resulting in the inability to cache new entries.
 
apc.user_ttl = 0
;sys
; Similarly, only for each user, the recommended value is 7200~36000.
; Set to 0 means that 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 the cache entry can exist in the garbage collection table.
; This value provides a security measure even if a server process crashes while executing cached source files,
; And the source file has been modified, and the memory allocated for the old version will not be recycled until this ttl value is reached.
; Set to zero will disable this feature.
 
apc.include_once_override = off
;sys
; There is currently no documentation on this directive, see: /bugs/?id=8754
; Please keep it off, otherwise it may lead to unexpected results.
 
apc.max_file_size = 1m
;sys
; Prohibit files larger than this size being cached.
 
apc.mmap_file_mask =
;sys
; If mmap support is compiled for apc using --enable-mmap (enabled by default),
; The value here is the mktemp-style file mask passed to the mmap module (the recommended value is "/tmp/").
; This mask is used to determine whether the memory mapped area is to be file-backed or shared memory backed.
; For direct file-backed memory maps, set to "/tmp/" look like (just 6 x).
; To use the posix style shm_open/mmap, you need to set it to "/" look.
; You can also set to "/dev/zero" to use the kernel's "/dev/zero" interface for anonymously mapped memory.
; If this directive is not defined, it means that anonymous mapping is used forcing it.
 
apc.num_files_hint = 1000
;sys
; The approximate number of different source files that may be included or requested on the web server (the recommended value is 1024~4096).
; If you are not sure, set to 0; This setting is mainly used for sites with thousands of source files.
 
= 0
; Optimization level (recommended value is 0).
; Positive integer values ​​indicate that the optimizer is enabled, and the higher the value, the more aggressive optimization is used.
; Higher values ​​may have very limited speed improvements, but are currently under trial.
 
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
; Number of shared memory blocks allocated for the compiler buffer (recommended value is 1).
; If the apc exhausts shared memory and the apc.shm_size directive has been set to the maximum allowed value of the system,
; You can try to increase this value.
 
apc.shm_size = 30
;sys
; The size of each shared memory block (in mb, the recommended value is 128~256).
; Some systems (including most BSD variants) have very small default shared memory block sizes.
 
apc.slam_defense = 0
;sys (oppose the use of this directive, it is recommended to use the apc.write_lock directive)
; On a very busy server, whether it is starting a service or modifying a file,
; All may lead to race conditions due to multiple processes attempting to cache a file at the same time.
; This directive is used to set the percentage of the process skipping the cache step when processing uncached files.
; For example, setting it to 75 means that there is a 75% chance of not being cached when encountering uncached files, thereby reducing the chance of collision.
; Set to 0 to disable this feature.
 
= on
;sys
; Whether to enable script update check.
; Be very careful when changing this instruction value.
; Default value on means that the apc checks whether the script is updated every time it requests the script.
; If updated, the compiled content will be recompiled and cached automatically. But doing so has a negative impact on performance.
; If set to off, it means no checking is performed, thereby greatly improving performance.
; But in order for the updated content to take effect, you must restart the web server.
; This directive is also valid for include/require files. But it should be noted that
; If you are using relative paths, apc must check every time include/require to locate the file.
; When using absolute paths, you can skip the check, so you are encouraged to use absolute paths for include/require operations.
 
apc.user_entries_hint = 100
;sys
; Similar to the num_files_hint directive, just for each different user.
; If you are not sure, set to 0.
 
apc.write_lock = on
;sys
; Whether to enable write lock.
; On a very busy server, whether it is starting a service or modifying a file,
; All may lead to race conditions due to multiple processes attempting to cache a file at the same time.
; Enable this directive to avoid the occurrence of race conditions.
 
apc.rfc1867 = off
;sys
; After opening this directive, for each upload file that contains the apc_upload_progress field just before the file field,
; apc will automatically create a user cache entry for upload_ (that is, the apc_upload_progress field value).