Caching file_get_contents output inside APC.
By Basant Kukreja on Oct 26, 2009
APC creates a mmap which is used for caching compiled php response. Besides this APC provides API to cache user data into this mmap. There are many php apps which uses file_get_contents to read the static files. If APC can cache these files in it's mmap, it will improve file_get_contents performance.
Based on the above idea, I developed a patch which stores the file_get_content output in APC's cache. In this patch, APC intercepts the calls of file_get_contents and replaces it with apc_file_get_contents function. If file is a full path and entire file is requested then APC caches the content.
To enable the caching use the following in php.ini :
apc.user_ttl = 30
apc.user_ttl will decide how long the entry will remain inside the cache. If apc.user_ttl is set to 0 then the entry will never be removed from cache.
Here is the performance data collected using Studio 12 on Solaris sparc for ecommerce php benchmark. (3500 simultaneous users)
||Excl. User CPU sec.
||Incl. User CPU sec.
||Excl. Total LWP sec.
||Incl. Total LWP sec.
||Excl. Sys. CPU sec.
||Incl. Sys. CPU sec.
User + System time before Caching = 372 seconds
User + System time after Caching = 193 seconds
Total time reduction in file_cache_contents = 48%
When static file is inside the cache then server will not do any stat calls. Which means if file changes then cache will still serve the old cached file during apc.user_ttl interval.