1,APCu
APCu is APC stripped of opcode caching.
這是apcu的官方文檔解釋,簡單的說APCU是從APC剝離出來的用戶數據緩存功能,而去掉了apc的opcode cache。
所以後來:APCu = APC User
APC的主要用途有兩項:
- 將PHP代碼編譯之後所產生的bytecode暫存在共享內存內供重複使用,以提升應用的運行效率。(Opcode Cache)
- 提供用戶數據緩存功能,需要顯示的調用,和redis/memcache類似。(User Data Cache)
其中第一點是其主要功能,因爲PHP的運行機制——每次接受一個請求時都要初始化所有的資源(將源代碼編譯成bytecode),執行代碼,然後釋放資源;但其實代碼99%的情況下並不會改變,所以每次請求都編譯執行十分的消耗時間。所以啓用Opcache Cache後,可以在初始化資源階段減少CPU和內存的消耗。
但是PHP從PHP 5.5開始,使用ZendOptimizerPlus作爲內置的Opcode Cache實現。所以現在APCU的主要功能便不再有意義了,而且其官方也隨後表示不再維護APC了。
apcu也提供了緩存常見的一些操作,如:
- apc_add — 緩存一個變量到數據存儲
- apc_bin_dump — 獲取給定文件和變量的二進制文件轉儲。
- apc_bin_dumpfile — Output a binary dump of cached files and user variables to a file
- apc_bin_load — Load a binary dump into the APC file/user cache
- apc_bin_loadfile — Load a binary dump from a file into the APC file/user cache
- apc_cache_info — Retrieves cached information from APC's data store
- apc_cas — 用新值更新舊值
- apc_clear_cache — 清除APC緩存
- apc_compile_file — Stores a file in the bytecode cache, bypassing all filters
- apc_dec — Decrease a stored number
- apc_define_constants — Defines a set of constants for retrieval and mass-definition
- apc_delete_file — Deletes files from the opcode cache
- apc_delete — 從用戶緩存中刪除某個變量
- apc_exists — 檢查APC中是否存在某個或者某些key
- apc_fetch — 從緩存中取出存儲的變量
- apc_inc — 遞增一個儲存的數字
- 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
但apcu也有一個明顯的缺點:這意味着如果您將PHP用作FastCGI進程(例如Nginx和php-fpm),則每個PHP進程都會擁有自己的緩存。在FastCGI模式下重啓後將會清除緩存。
2, Opcache
opcache就是APC剝離出的第一個緩存字節碼的功能緩存。
OPCache is a special caching mechanism that stores precompiled versions of the PHP files. When executed, a PHP file is compiled to bytecode and once this process is done, the bytecode is executed.
OPCache是一種特殊的緩存機制,用於存儲PHP文件的預編譯版本。執行後,一個PHP文件被編譯爲字節碼,一旦完成此過程,字節碼便被執行。
自PHP 5.5起,它已捆綁在內核中,只需安裝它即可使我們獲得極大的速度改進,而不必像apcu那樣去顯式的調用它。