今天简单的查看了下APC的源码,搞清楚了它对php中间代码做缓存的机制,其实很简单,就是在模块启动时,替换了zend的编译函数为APC自己的编译函数。
源代码如下:
int apc_module_init(int module_number TSRMLS_DC)
{
/* apc initialization */
#if APC_MMAP
apc_sma_init(APCG(shm_segments), APCG(shm_size)*1024*1024, APCG(mmap_file_mask));
#else
apc_sma_init(APCG(shm_segments), APCG(shm_size)*1024*1024, NULL);
#endif
apc_cache = apc_cache_create(APCG(num_files_hint), APCG(gc_ttl), APCG(ttl));
apc_user_cache = apc_cache_create(APCG(user_entries_hint), APCG(gc_ttl), APCG(user_ttl));
apc_compiled_filters = apc_regex_compile_array(APCG(filters));
/* override compilation */
old_compile_file = zend_compile_file;
zend_compile_file = my_compile_file; //替换编译函数
REGISTER_LONG_CONSTANT("/000apc_magic", (long)&set_compile_hook, CONST_PERSISTENT | CONST_CS);
APCG(initialized) = 1;
return 0;
}
本文介绍了APC如何通过替换PHP的编译函数实现中间代码缓存。APC在启动时会替换zend的编译函数,以此来缓存编译后的中间代码,提高PHP应用程序的性能。
3472

被折叠的 条评论
为什么被折叠?



