Home > Article > Backend Development > Optimize PHP programs using APC cache_PHP tutorial
Alternative PHP Cache(APC)是 PHP 的一个免费公开的优化代码缓存。它用来提供免费,公开并且强健的架构来缓存和优化 PHP 的中间代码。 APC 官方网站为http://pecl.php.net/package/apc Windows下的拓展模块下载地址http://www.sitebuddy.com/php/accelerators/apc_windows_binaries_builds 在 Windows 下,APC 要求有 c: mp 目录,并且该目录要对 Web 服务器进程可写。 1、安装 以PHP extension 形式安装 2、配置 apc.enabled boolean apc.optimization optimization 选项在脚本中可以改变 APC PHP.ini配置选项详解 [APC] ; Alternative PHP Cache 用于缓存和优化PHP中间代码 apc.cache_by_default = On ;SYS ; 是否默认对所有文件启用缓冲。 ; 若设为Off并与以加号开头的apc.filters指令一起用,则文件仅在匹配过滤器时才被缓存。 apc.enable_cli = Off ;SYS ; 是否为CLI版本启用APC功能,仅用于测试和调试目的才打开此指令。 apc.enabled = On ; 是否启用APC,如果APC被静态编译进PHP又想禁用它,这是唯一的办法。 apc.file_update_protection = 2 ;SYS ; 当你在一个运行中的服务器上修改文件时,你应当执行原子操作。 ; 也就是先写进一个临时文件,然后将该文件重命名(mv)到最终的名字。 ; 文本编辑器以及 cp, tar 等程序却并不是这样操作的,从而导致有可能缓冲了残缺的文件。 ; 默认值 2 表示在访问文件时如果发现修改时间距离访问时间小于 2 秒则不做缓冲。 ; 那个不幸的访问者可能得到残缺的内容,但是这种坏影响却不会通过缓存扩大化。 ; 如果你能确保所有的更新操作都是原子操作,那么可以用 0 关闭此特性。 ; 如果你的系统由于大量的IO操作导致更新缓慢,你就需要增大此值。 apc.filters = ;SYS ; 一个以逗号分隔的POSIX扩展正则表达式列表。 ; 如果源文件名与任意一个模式匹配,则该文件不被缓存。 ; 注意,用来匹配的文件名是传递给include/require的文件名,而不是绝对路径。 ; 如果正则表达式的第一个字符是"+"则意味着任何匹配表达式的文件会被缓存, ; 如果第一个字符是"-"则任何匹配项都不会被缓存。"-"是默认值,可以省略掉。 apc.ttl = 0 ;SYS ; 缓存条目在缓冲区中允许逗留的秒数。0 表示永不超时。建议值为7200~36000。 ; 设为 0 意味着缓冲区有可能被旧的缓存条目填满,从而导致无法缓存新条目。 apc.user_ttl = 0 ;SYS ; 类似于apc.ttl,只是针对每个用户而言,建议值为7200~36000。 ; 设为 0 意味着缓冲区有可能被旧的缓存条目填满,从而导致无法缓存新条目。 apc.gc_ttl = 3600 ;SYS ; 缓存条目在垃圾回收表中能够存在的秒数。 ; 此值提供了一个安全措施,即使一个服务器进程在执行缓存的源文件时崩溃, ; 而且该源文件已经被修改,为旧版本分配的内存也不会被回收,直到达到此TTL值为止。 ; 设为零将禁用此特性。 apc.include_once_override = Off ;SYS ; 请保持为Off,否则可能导致意想不到的结果。 apc.max_file_size = 1M ;SYS ; 禁止大于此尺寸的文件被缓存。 apc.mmap_file_mask = ;SYS ; 如果使用–enable-mmap(默认启用)为APC编译了MMAP支持, ; 这里的值就是传递给mmap模块的mktemp风格的文件掩码(建议值为"/tmp/apc.XXXXXX")。 ; 该掩码用于决定内存映射区域是否要被file-backed或者shared memory backed。 ; 对于直接的file-backed内存映射,要设置成"/tmp/apc.XXXXXX"的样子(恰好6个X)。 ; 要使用POSIX风格的shm_open/mmap就需要设置成"/apc.shm.XXXXXX"的样子。 ; 你还可以设为"/dev/zero"来为匿名映射的内存使用内核的"/dev/zero"接口。 ; 不定义此指令则表示强制使用匿名映射。 apc.num_files_hint = 1000 ;SYS ; Web服务器上可能被包含或被请求的不同源文件的大致数量(建议值为1024~4096)。 ; 如果你不能确定,则设为 0 ;此设定主要用于拥有数千个源文件的站点。 apc.optimization = 0 ; 优化级别(建议值为 0 ) 。 ; 正整数值表示启用优化器,值越高则使用越激进的优化。 ; 更高的值可能有非常有限的速度提升,但目前尚在试验中。 apc.report_autofilter = Off ;SYS ; Whether to record 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 (recommended value is 1). ; If APC runs out of shared memory and the apc.shm_size directive has been set to the maximum allowed by the system, ; You can try increasing 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 (It is against the use of this command, it is recommended to use the apc.write_lock command) ; On a very busy server, whether starting a service or modifying a file, ; can cause a race condition due to multiple processes trying to cache a file at the same time. ; This directive is used to set the percentage of the process that skips the caching step when processing uncached files. ; For example, setting it to 75 means that there is a 75% probability of not caching when an uncached file is encountered, thereby reducing the chance of collision. ; It is encouraged to set to 0 to disable this feature. apc.stat = On ;SYS ; Whether to enable script update checking. ; Be very careful when changing this command value. ; The default value On means that APC checks whether the script has been updated every time it is requested, ; Automatically recompile and cache the compiled content if updated. However, doing so has a negative impact on performance. ; If set to Off, no checking is performed, thus 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 use relative paths, APC must check to locate the file every include/require. ; Using absolute paths 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, but for each different user. ; Set to 0 if you are not sure. apc.write_lock = On ;SYS ; Whether to enable write lock. ; On a very busy server, whether starting a service or modifying a file, ; can cause a race condition due to multiple processes trying to cache a file at the same time. ; Enabling this directive can avoid race conditions. apc.rfc1867 = Off ;SYS ; After turning on this command, for each uploaded 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). 3. Function apc_cache_info - Retrieves cached information (and meta-data) from APCs data store The usage of apc is relatively simple, with only a few functions, listed below. apc_cache_info () returns cache information apc_clear_cache() clears the apc cache content. By default (no parameters), only the system cache is cleared. To clear the user cache, the ‘user’ parameter is required. apc_define_constants (string key, array constants [, bool case_sensitive]) Add array constants to the cache as constants. apc_load_constants (string Key). Remove constant cache. apc_store ( string key, mixed var [, int ttl] ). Save data in cache. apc_fetch (string key). Get the cache content saved by apc_store apc_delete (string key). Delete the content saved by apc_store. apc management: Go to pecl.php.net to download the apc source code package. There is apc.php, copy it to a place where your web server can access it, and browse to access it. Management interface functions include: 1. Refresh Data 2. View Host Stats 3. System Cache Entries 4. User Cache Entries 5. Version Check
apc_clear_cache - Clears the APC cache
apc_define_constants - Defines a set of constants for later retrieval and mass-definition
apc_delete - Removes a stored variable from the cache
apc_fetch - Fetch a stored variable from the cache
apc_load_constants - Loads a set of constants from the cache
apc_sma_info - Retrieves APCs Shared Memory Allocation information
apc_store - Cache a variable in the data store