1, APC Cache introduction
APC, the full name is alternative PHP cache, the official translation is called "Optional PHP Caching". It provides us with a framework for caching and optimizing PHP's intermediate code. The APC cache is divided into two parts:System CacheAndUser Data caching。
1.1 System Cache
It means that APC caches the compiled results of the PHP file's source code and then compares the time stamp with each call. If it does not expire, it is run with the cached intermediate code. Default cache
3600s (one hour). However, this can still waste a lot of CPU time. Therefore, you can set the system cache to never expire (apc.ttl=0) in php.ini. However, if you do this, you will need to restart the Web server after changing the PHP code. This type of cache is currently used more often.
1.2 User Data cache
Caching is read and written by the user using the Apc_store and Apc_fetch functions when writing PHP code. If you have a small amount of data, you can try it. If the amount of data is large, it would be better to use a more monograph memory caching scheme similar to memcache this class
2,cache key Generation rules
Each slot in the APC cache will have a key,key that is the apc_cache_key_t struct type, except for key-related properties, the key is the generation of the H field. The H field determines which position of this element falls in the slots array. There are different generation rules for the user cache and the system cache. The user cache generates a key through the Apc_cache_make_user_key function. The key string passed in by the user relies on the hash function in the PHP kernel (PHP'sthe hash function used by the Hashtable: Zend_inline_hash_func), generates an H value.
The system cache generates a key through the Apc_cache_make_file_key function. Different scenarios are differentiated by the switch of the APC configuration item Apc.stat.
In the case of open, apc.stat= on, the compiled content is automatically recompiled and cached if it is updated. The H value at this point is the added value of the file's device and Inode.
In the case of Apc.stat=off, when the file is modified, you must restart the Web server if you want the updated content to take effect. At this point the H value is generated based on the path address of the file, and the path here is the absolute path. Even if you are using a relative path, you will find the PG (include_path) location file to obtain an absolute path, so using an absolute path skips the check and can improve the efficiency of your code.
3, Adding the caching process
In the case of user caches, the Apc_add function is used to add content to the APC cache. If the key parameter is in a string , APC generates a key based on this string, and if the key parameter is an array , APC traverses the entire array, generating the key. Based on these key,apc, the _apc_store is called to store the values in the cache. Because this is a user cache, the cache currently in use is Apc_user_cache. The write operation is performed by the Apc_cache_make_user_entry function, whose final call Apc_cache_user_insert performs a traversal query and write operation.
In response, the system cache uses Apc_cache_insert to perform write operations, which eventually call _apc_cache_insert.
APC Cache description in PHP core code base 123