PHP的apcu是什么,opcache又是什么?
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那样去显式的调用它。