III. APC 可選 PHP 緩存

簡介

Alternative PHP Cache(APC)是 PHP 的一個免費公開的優化代碼緩存。它用來提供免費,公開並且強健的架構來緩存和優化 PHP 的中間代碼。

安裝

PECL 擴展未綁定於 PHP 中。

安裝此 PECL 擴展庫的信息可在手冊中標題為 PECL 擴展庫安裝的一章中找到。 更多信息如新版本,下載,源文件,維護者信息以及更新日誌等可以在這裡找到:http://pecl.php.net/package/apc

可以從 PHP 下載頁面或者 http://snaps.php.net/ 下載此 PECL 擴展的 DLL 文件。

注意: 在 Windows 下,APC 要求有 c:\tmp 目錄,並且該目錄要對 Web 服務器進程可寫。

注意: 更多深入且高度專業的文檔,見開發者提供的 TECHNOTES 文件。

運行時配置

這些函數的行為受 php.ini 的影響。

儘管默認的 APC 設定對於大多數安裝已經沒問題,但專業人員應考慮調整以下參數。

表 1. APC configuration options

名稱默認值可修改範圍更新記錄
apc.enabled"1"PHP_INI_ALL 
apc.shm_segments"1"PHP_INI_SYSTEM 
apc.shm_size"30"PHP_INI_SYSTEM 
apc.optimization"0"PHP_INI_ALL 
apc.num_files_hint"1000"PHP_INI_SYSTEM 
apc.ttl"0"PHP_INI_SYSTEM 
apc.gc_ttl"3600"PHP_INI_SYSTEM 
apc.cache_by_default"1"PHP_INI_SYSTEM 
apc.filtersNULLPHP_INI_SYSTEM 
apc.mmap_file_maskNULLPHP_INI_SYSTEM 
apc.slam_defense"0"PHP_INI_SYSTEM 
apc.file_update_protection"2"PHP_INI_SYSTEM 
apc.enable_cli"0"PHP_INI_SYSTEM> APC 3.0.6
有關 PHP_INI_* 常量進一步的細節與定義參見附錄 H

以下是配置選項的簡要解釋。

apc.enabled boolean

apc.enabled 可以設成 0 來禁用 APC。這主要是用在當 APC 被靜態編譯入 PHP 時,因為沒有其它方法來禁用了(編譯為 DSO 的時候,可以將 php.ini 中的 extension 行註釋掉)。

apc.shm_segments integer

對編譯器緩存要分配的共享內存塊的數目。如果 APC 用光了共享內存但是已經將 apc.shm_size 設為了系統所能允許的最大值,可以嘗試增大此值。

apc.shm_size integer

以 MB 為單位的每個共享內存塊的大小。默認時,有些系統(包括大多數 BSD 變種)的共享內存塊大小非常低。

apc.optimization integer

優化級別。設為 0 則禁用優化器,更高的值則使用更主動的優化。期望非常有限的速度提升。尚在試驗中。

apc.num_files_hint integer

Web 服務器上的被包含或被請求的不同源文件的數目的大概估計。如果不確定則設為 0 或去掉此項;此設定主要用在有數千個源文件的站點。

apc.ttl integer

緩存條目在緩衝區所允許的空閒時間的秒數。將此設為零意味著緩衝區有可能被阻賽了的緩存充滿而導致新條目不被緩存。

apc.gc_ttl integer

緩存條目在垃圾回收表中能夠存在的秒數。此值提供了一個安全措施,即在服務器進程在執行緩存的源文件時,如果該文件被修改則舊版本將不會被回收,直到達到此 TTL 為止。設為零將禁用此特性。

apc.cache_by_default boolean

默認為 on,但可以設為 off 並和加號開頭的 apc.filters 一起用,則文件僅在匹配過濾器時被緩存。

apc.filters string

一個以逗號分隔的 POSIX 擴展正則表達式的列表。如果任一個模式匹配源文件名,則該文件不被緩存。注意用來匹配的文件名是傳遞給 include/require 的文件名,而不是絕對路徑。如果正則表達式的第一個字符是 + 則意味著任何匹配表達式的文件會被緩存,如果第一個字符是 - 則任何匹配項都不會被緩存。- 是默認值,可以省略掉。

apc.mmap_file_mask string

If compiled with MMAP support by using --enable-mmap this is the mktemp-style file_mask to pass to the mmap module for determing whether your mmap'ed memory region is going to be file-backed or shared memory backed. For straight file-backed mmap, set it to something like /tmp/apc.XXXXXX (exactly 6 Xs). To use POSIX-style shm_open/mmap put a .shm somewhere in your mask. e.g. /apc.shm.XXXXXX You can also set it to /dev/zero to use your kernel's /dev/zero interface to anonymous mmap'ed memory. Leaving it undefined will force an anonymous mmap.

apc.slam_defense integer

On very busy servers whenever you start the server or modify files you can create a race of many processes all trying to cache the same file at the same time. This option sets the percentage of processes that will skip trying to cache an uncached file. Or think of it as the probability of a single process to skip caching. For example, setting apc.slam_defense to 75 would mean that there is a 75% chance that the process will not cache an uncached file. So, the higher the setting the greater the defense against cache slams. Setting this to 0 disables this feature.

apc.file_update_protection integer

When you modify a file on a live web server you really should do so in an atomic manner. That is, write to a temporary file and rename (mv) the file into its permanent position when it is ready. Many text editors, cp, tar and other such programs don't do this. This means that there is a chance that a file is accessed (and cached) while it is still being written to. This apc.file_update_protection setting puts a delay on caching brand new files. The default is 2 seconds which means that if the modification timestamp (mtime) on a file shows that it is less than 2 seconds old when it is accessed, it will not be cached. The unfortunate person who accessed this half-written file will still see weirdness, but at least it won't persist. If you are certain you always atomically update your files by using something like rsync which does this correctly, you can turn this protection off by setting it to 0. If you have a system that is flooded with io causing some update procedure to take longer than 2 seconds, you may want to increase this a bit.

apc.enable_cli integer

Mostly for testing and debugging. Setting this enables APC for the CLI version of PHP. Normally you wouldn't want to create, populate and tear down the APC cache on every CLI request, but for various test scenarios it is handy to be able to enable APC for the CLI version of APC easily.

資源類型

本擴展模塊未定義任何資源類型。

預定義常量

本擴展模塊未定義任何常量。

目錄
apc_add --  Cache a variable in the data store (only if it's not stored)
apc_cache_info --  Retrieves cached information (and meta-data) from APC's data store
apc_clear_cache --  Clears the APC cache
apc_define_constants --  Defines a set of constants for 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 APC's Shared Memory Allocation information
apc_store --  Cache a variable in the data store