How much do you know about PHP APC?

Source: Internet
Author: User
Tags apc apc 4 apc configuration posix
For you to introduce the relevant knowledge of PHP APC, including APC commonly used functions, APC configuration, APC advantages, and so on, a good example, the need for friends, you can refer to the next.

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.

First,APC common functions : 1.apc_clear_cache () Clear APC Cache content 2.apc_define_constants (string Key,array constants,[,bool case_ Sensitive]) Remove the constant cache 4.apc_store (string key,mixed var [, int TTL]) by adding the array constants to the cache 3.apc_load_constants (string key) Save data in cache 5.apc_fetch (string key) gets Apc_store saved cache data 6.apc_delete (string key) 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 check if one or more APC key names exist 9.apc_delete_file (mixed keys Deletes the cache of the given file from the opcode cache 10.apc_compile_file (string filename [, bool Atmic=true]) bypasses the filters limit, caches the file 11.apc_cache_info ( String Cache_type [, bool Limited=false]) Get cache Type I milkshake note: apc_clear_cache only clears the opcode cache file, Apc_delete clearly the variables in the cache, predefined variables, you can use APC_ define_constants function, PHP variable can use the function Apc_store, using APC is better than memcache, do not need to go through the Network transport protocol TCP;APC not be used in the function Apc_store cache frequently changed user data, There will be some strange phenomena; The APC itself does not support distribution.

Second,APC Common configuration : 1. How much memory will be assigned to the Apc,ini option Apc.shm_size (integer) to control this setting. The default is 30m2. Each time the APC is requested to check for file modifications, the INI option Apc.stat Control This setting, the default value is 1, which indicates whether the script is updated every time the script is requested, and if the update automatically recompile and cache the compiled content, the performance has a proportional effect, so this is set to 03. ini Option APC.F Ilters fewer scripts to cache

Iii.APC Advantages : 1. Use the spinlocks (spin) lock mechanism to achieve optimal performance 2.APC provides apc.php for monitoring and managing the APC cache. (Note: Modify the administrator name and password) 3.APC by default, the shared memory is created by Mmap Anonymous mappings, and the cache objects are stored in this large memory space. The shared memory is managed by APC 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 APC _define-- The constants () function. However, APC developers say PECL hidef performance, discard define, it is inefficient 6.apc_store, for system settings and other PHP variables, the life cycle is the entire application (from the httpd daemon to the httpd daemon shutdown), Using APC is better than memcache. (No Network Transfer Protocol required) 7.APC does not apply to frequent changes to user data through the function Apc_store () cache, and there are some strange phenomena.

Cases:

!--? php/***php APC Example * by http://bbs.it-home.org*/$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)}?> 

Attached, detailed configuration instructions: Apc.cache_by_default = On;sys; Whether buffering is enabled by default for all files.; If set to off and used with the apc.filters instruction that starts with a plus sign, the file is cached only when matching the filter. APC.ENABLE_CLI = Off;sys; Whether APC functionality is enabled for the CLI version, which is only opened for testing and debugging purposes. apc.enabled = on; If APC is enabled, it is the only option if APC is statically compiled into PHP and wants to disable it. 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 and CP, tar and other programs do not operate this way, resulting in the possibility of buffering the incomplete files. The default value of 2 indicates that the modification time is not buffered if the access time is less than 2 seconds when the file is accessed.; The unfortunate visitor may get incomplete content, but the bad effect is not amplified by the cache.; If you can ensure that all update operations are atomic, you can turn this feature off 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 a regular expression is "+" it means that any file matching the expression is cached; If the first character is "-" none of the matches will be cached. "-" is the default value and can be omitted. Apc.ttl = 0;sys; The number of seconds that the cache entry is allowed to stay in the buffer. 0 means never time out. The recommended value is 7200~36000.; A setting of 0 means that the buffer may be filled with old cache entries, causing the new entries to be cached. 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, causing the new entries to be cached. Apc.gc_ttl = 3600;sys; The number of seconds that a 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 file; And the source file has been modified, the memory allocated for the old version is alsoWill not be recycled until this TTL value is reached.; Set to zero disables this attribute. Apc.include_once_override = Off;sys; There is no documentation for this directive, see: http://pecl.php.net/bugs/bug.php?id=8754; Please remain off, otherwise it may result in unexpected results. Apc.max_file_size = 1m;sys; Prohibit files larger than this size from being cached. Apc.mmap_file_mask =;sys; If you have compiled MMAP support for APC using –ENABLE-MMAP (enabled by default); The value here is the Mktemp-style file mask that is passed to the Mmap module (the recommended value is "/tmp/apc.xxxxxx").; This mask is used to determine if the memory-mapped area is to be file-backed or shared memory backed. For the direct file-backed memory mapping, set the "/tmp/apc.xxxxxx" Look (exactly 6 x).; To use POSIX-style shm_open/mmap, you need to set the "/apc.shm.xxxxxx" look.; You can also set the "/dev/zero" to use the kernel's "/dev/zero" interface for anonymous mapped memory. Undefined this directive means that anonymous mappings are enforced. Apc.num_files_hint = 1000;sys; Approximate number of different source files that may be included or requested on the Web server (recommended value is 1024~4096).; If you are unsure, set to 0; This setting is primarily 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 the optimization is used.; Higher values may have a very limited speed boost, but are still being tested. Apc.report_autofilter = Off;sys; Whether to log all scripts that are not cached automatically because of the early/late binding reason. Apc.shm_segments = 1;sys; The number of shared memory blocks allocated for the compiler buffer (recommended value is 1).; If the APC runs out of shared memory and the apc.shm_size instruction is set to the maximum allowable value of the system; You can try to increase this value. Apc.shm_size = 30;sys; The size of each shared memory block (in megabytes, the recommended value is 128~256).; Some systems (including most BSD variants) default shared internalThe size of the storage block is very small. Apc.slam_defense = 0;sys (against the use of the directive, it is recommended to use the Apc.write_lock Directive); On a very busy server, whether it's starting a service or modifying a file; May result in a race condition due to multiple processes attempting to cache a file at the same time.; This instruction is used to set the percentage of skipped cache steps when the process is processing files that are not cached. For example, set to 75 indicates that there is a 75% probability of not being cached when encountering files that are not cached, thus reducing the chance of collisions. Encourage set to zero to disable this feature. Apc.stat = On;sys; Whether to enable the script update check.; Be very careful about changing this command value. The default value on indicates that APC checks that the script is updated every time the script is requested; If updated, the compiled content is automatically recompiled and cached. However, this has an adverse effect on performance. If set to OFF, no check is performed, resulting in a significant performance gain.; However, in order for the updated content to take effect, you must restart the Web server.; This command is also valid for Include/require files. However, it is important to note that; If you are using a relative path, APC must check each time 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, only for each different user.; If you are not sure, set to 0. Apc.write_lock = On;sys; Whether write locks are enabled.; On a very busy server, whether it's starting a service or modifying a file; May result in a race condition due to multiple processes attempting to cache a file at the same time.; Enabling this directive avoids the appearance of competitive conditions. apc.rfc1867 = Off;sys; After opening the instruction, for each upload file that contains the Apc_upload_progress field immediately 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.