Deep analysis of PHP apc_php example

Source: Internet
Author: User
Tags apc garbage collection mixed posix
APC Definition: APC is an open and free php opcode cache. Its goal is to provide a free, open and robust framework for caching and optimizing PHP intermediate code.
APC Common functions:
1.apc_clear_cache () Clear APC cache content
2.apc_define_constants (String Key,array constants,[,bool case_sensitive]) adds an array constants to the cache with constants
3.apc_load_constants (string key) to remove the constant cache
4.apc_store (String key,mixed var [, int TTL]) saves data in cache
5.apc_fetch (string key) Gets the cached data saved by Apc_store
6.apc_delete (string key) to delete Apc_store saved content
7.apc_add (String key,mixd var [, int TTL]) caches a variable to the data store (only if it is not stored before the variable)
8.apc_exists (mix keys) Check to see if one or more of the APC key names exist
9.apc_delete_file (mixed keys) deletes the cache of a given file from the opcode cache
10.apc_compile_file (string filename [, bool atmic=true]) bypasses filters restrictions, caching files
11.apc_cache_info (String cache_type [, bool Limited=false]) Get cache I-type milkshake
Note: Apc_clear_cache only clears opcode cache files, Apc_delete clear the variables in the cache, predefined variables, you can use the Apc_define_constants function, PHP variables can use the function Apc_store, Using APC is better than memcache, there is no need for a network transport protocol TCP;APC is not suitable for user data that is frequently changed via function Apc_store caching; The APC itself does not support distributed.
APC Common configuration:
1. How much memory will be assigned to Apc,ini option Apc.shm_size (integer) to control this setting. The default is 30M
2. Each request APC Check file modification, INI option Apc.stat Control This setting, the default value is 1, indicating that every time the script is requested to reduce the output of the script is updated, if the update will automatically recompile and cache the compiled content, the performance has a proportional impact, so this is set to 0
3. Apc.filters cache with fewer scripts via the INI option
APC Benefits:
1. Use Spinlocks (spin) lock mechanism to achieve optimal performance
2.APC provides apc.php for monitoring and managing the APC cache. (Note: Modify administrator name and password)
3.APC The default is to create shared memory by Mmap Anonymous mappings, which are stored in this large memory space. This shared memory is managed by the APC itself
4. Adjust the value of Apc.shm_size, Apc.num_files_hints, Apc.user_entires_hint to the best
5.php predefined constants, you can use the APC _define--constants () function. However, APC developers say PECL HiDef performance, discarding define, which is inefficient
6.apc_store, for PHP variables such as system settings, the lifecycle is the entire application (from the httpd daemon to the httpd daemon shutdown), and using APC is better than memcache. (No network transport protocol required)
7.APC does not apply to frequent user data changes via function Apc_store () caching, there are some strange phenomena.
Example:
<?php
$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 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)}
?>
Supplemental Detailed configuration instructions:
Apc.cache_by_default = On
; sys
; Whether to enable buffering for all files by default.
; When set to off and used with a apc.filters instruction that starts with a plus sign, the file is cached only when the filter is matched.

APC.ENABLE_CLI = Off
; sys
; Whether to enable the APC feature for the CLI version and open this directive only for testing and debugging purposes.

apc.enabled = On
; If APC is enabled, it is the only way to disable it if the APC is statically compiled into PHP.

Apc.file_update_protection = 2
; sys
; When you modify a file on a running server, you should perform an atomic operation.
; That is, first write in a temporary file, and then rename the file (MV) to the final name.
; Programs such as text editors and CP, TAR, and so on, do not operate this way, resulting in the possibility of buffering the defective files.
; The default value of 2 indicates that when the file is accessed, it is not buffered if it finds that the modified time distance is less than 2 seconds.
; The unfortunate visitor may get the incomplete content, but the bad effect will not be magnified by the cache.
; If you can make sure that all of the update operations are atomic, 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.

Apc.filters =
; sys
; A comma-delimited list of POSIX-extended regular expressions.
; If the source file name matches any one of the patterns, the file is not cached.
; Note that the filename 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 that matches the expression will be cached.
; If the first character is "-", no matches are cached. "-" is the default value and can be omitted.

Apc.ttl = 0
; sys
; The number of seconds the cache entry allowed to linger in the buffer. 0 means never times out. The recommended value is 7200~36000.
; A setting of 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
; Similar to Apc.ttl, the recommended value is 7200~36000 for each user.
; A setting of 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 the cached source files.
; And the source file has been modified, and the memory allocated for the old version will not be reclaimed until this TTL value is reached.
; Setting to zero disables this attribute.

Apc.include_once_override = Off
; sys
; There is no documented documentation for this directive, see: http://pecl.php.net/bugs/bug.php?id=8754
; Keep off, or you may cause unexpected results.

Apc.max_file_size = 1m
; sys
; Prevents files larger than this size from being cached.

Apc.mmap_file_mask =
; sys
; If you use –enable-mmap (default enabled) to compile Mmap support for APC,
; The value here is the Mktemp-style file mask passed to the Mmap module (the recommended value is "/tmp/apc.xxxxxx").
; The mask is used to determine whether the memory-mapped area is to be file-backed or shared memory backed.
; For a direct file-backed memory mapping, set to "/tmp/apc.xxxxxx" (exactly 6 x).
; To use POSIX-style shm_open/mmap, you need to set it to "/apc.shm.xxxxxx."
; You can also set the "/dev/zero" to use the kernel's "/dev/zero" interface for anonymous mapped memory.
; Not defining this directive means enforcing the use of anonymous mappings.

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 unsure, set to 0; This setting is primarily used for sites that have thousands of source files.

apc.optimization = 0
; Optimization level (recommended value is 0).
; A positive integer value indicates that the optimizer is enabled, and the higher the value, the more aggressive optimization is used.
; Higher values may have very limited speed increases, but are still being tested.

Apc.report_autofilter = Off
; sys
; Whether to log all scripts that are automatically not cached due to early/late binding reasons.

Apc.shm_segments = 1
; sys
; The number of shared memory blocks allocated for the compiler buffer (the recommended value is 1).
; If the APC runs out of shared memory and the Apc.shm_size directive is set to the maximum 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, the recommended value is 128~256).
; Some systems, including most BSD variants, have a very small default shared memory block size.

Apc.slam_defense = 0
; Sys (against using the directive, recommended to use the Apc.write_lock Directive)
; On a very busy server, whether you start a service or modify a file,
; can result in competitive conditions because multiple processes attempt to cache a single file at the same time.
; This directive is used to set the percentage of cache steps that the process skips when processing files that are not cached.
; For example, set to 75 means that when you encounter a file that is not cached, 75% of the probability is not cached, thereby reducing the collision probability.
; Encourage set to 0来 disable this feature.

Apc.stat = On
; sys
; Whether to enable script update checking.
; Change this instruction value to be very careful.
; The default value on means that the APC checks whether the script is updated every time the script is requested.
; If updated, the compiled content is automatically recompiled and cached. But doing so has a detrimental effect on performance.
; If set to OFF, the performance is significantly improved by not checking.
; But for the updated content to take effect, you must restart the Web server.
; This instruction is also valid for Include/require files. But it should be noted that
; If you are using a relative path, the APC must check every time it include/require to locate the file.
; Using an absolute path allows you to skip the check, so you are encouraged to use an absolute path for include/require operations.

Apc.user_entries_hint = 100
; sys
; Similar to the Num_files_hint directive, just for each individual user.
; If you are unsure, set to 0.

Apc.write_lock = On
; sys
; Whether write locks are enabled.
; On a very busy server, whether you start a service or modify a file,
; can result in competitive conditions because multiple processes attempt to cache a single file at the same time.
; Enabling this directive avoids the appearance of competitive conditions.

apc.rfc1867 = Off
; sys
; After you open the directive, for each upload file that contains the Apc_upload_progress field just before the file field,
; APC will automatically create a Upload_ user cache entry (that is, the Apc_upload_progress field value).

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: info-contact@alibabacloud.com 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.