Today to do a large file upload progress effect, later on the internet said there is PHP APC can be achieved, and then Baidu a bit PHP APC information, PHP APC provides two kinds of cache function, that is, cache opcode (target file), we call Apc_compiler_cache. It also provides interfaces for PHP developers to host user data in memory, which we call Apc_user_cache
Want to use the APC cache exactly how the effect, helpless in Windows can not find the corresponding version of the Php-apc.dll extension file, had to be in the Linux
Installed under the installation, without the source code installation, direct Yum on the line:
To install the APC dependency package first:
The code is as follows |
Copy Code |
Yum install php-pear php-devel httpd-devel pcre-devel gcc make
|
Then use PECL to install the APC:
The code is as follows |
Copy Code |
PECL Install APC
|
To add an APC extension to a configuration file:
The code is as follows |
Copy Code |
echo "extension=apc.so" >/etc/php.d/apc.ini
|
Finally, remember to restart the server:
The code is as follows |
Copy Code |
Service httpd Restart |
, and then use PHP's Phpinfo () function to detect:
APC Cache function Instance
In APC we can also enjoy the ability of APC to cache large file upload progress, we need to set the apc.rfc1867 to 1 in php.ini.
and add a hidden field in the form apc_upload_progress, the value of this field can randomly generate a hash, to ensure the unique.
APC has many settings that you can set in php.ini, such as:
The code is as follows |
Copy Code |
[APC] apc.enabled = 1 Apc.shm_segments = 1 Apc.shm_size = 64 Apc.max_file_size = 10M Apc.stat=1
|
I tried the following APC functions that are common in PHP:
The code is as follows |
Copy Code |
/* Add variable to data store BOOL Apc_add (String $key, mixed $var [, int $ttl = 0]) If key is present, it will not overwrite but return false */ Apc_add (' url ', ' http://www.111cn.Net '); /* Remove stored variables from the cache Mixed Apc_fetch (mixed $key [, BOOL & $success]) */ Var_dump (apc_fetch (' url ')); /* Use Apc_store () stored variables,. Key is unique, so two values use the same name, the original will be overwritten by the new value BOOL Apc_store (String $key, mixed $var [, int $ttl = 0]) */ Apc_store (' var ', ' new variable '); /* Remove a variable from the user cache Mixed Apc_delete (String $key) */ Apc_delete (' url '); /* Clear APC Cache bool Apc_clear_cache ([string $cache _type]) */ Apc_clear_cache (' user '); /* Check if there is some or some key in APC and return TRUE if key exists, otherwise FALSE Mixed apc_exists (mixed $keys) */ if (apc_exsists (' url ')) { echo "This key really exists"; }else{ echo "Looks like this key does not exist"; } /* Increments a stored number, returns the current value of key when successful, or returns FALSE on failure int Apc_inc (string $key [, int $step = 1 [, BOOL & $success]]) */ Apc_store (' Anumber ', 42); $ret = Apc_inc (' Anumber ', 1, $fail); Var_dump ($ret); Var_dump ($fail); /* decrements the number of a stored variable, returns the current value of key when successful, or returns FALSE on failure int Apc_dec (string $key [, int $step = 1 [, BOOL & $success]]) */ $ret = Apc_dec (' astring ', 1, $fail); Var_dump ($ret); Var_dump ($fail); |
Also provides a good use of the APC cache class:
The code is as follows |
Copy Code |
/********************************************************************************* * APC Cache class, Copyright (excerpt from initphp framework) ***********************************************************************************/ Class MYAPC { /** * APC Cache-Set cache * Set cache Key,value and cache time * @param string $key key value * @param string $value value * @param string $time cache time */ Public Function Set_cache ($key, $value, $time = 0) { if ($time = = 0) $time = null; Null in the case of a permanent cache Return Apc_store ($key, $value, $time);; } /** * APC Cache-Get Cache * Get cached data from key * @param string $key key value */ Public Function Get_cache ($key) { Return Apc_fetch ($key); } /** * APC Cache-Clears a cache * Delete a cache from Memcache * @param string $key key value */ Public function Clear ($key) { Return Apc_delete ($key); } /** * APC Cache-Clears all caches * It is not recommended to use this feature * @return */ Public Function Clear_all () { Apc_clear_cache (' user '); Clear User Cache return Apc_clear_cache (); Clear Cache } /** * Check if APC cache exists * @param string $key key value */ Public function exists ($key) { Return apc_exists ($key); } /** * Field self-increment-for counting * @param string $key key value * @param int $step New Step value */ Public Function Inc ($key, $step) { Return Apc_inc ($key, (int) $step); } /** * Field auto minus-for counting * @param string $key key value * @param int $step New Step value */ Public function Dec ($key, $step) { Return Apc_dec ($key, (int) $step); } /** * Return APC Cache information */ Public Function info () { return Apc_cache_info (); } } Here's how to use it: $APC = new MYAPC (); $APC->set_cache (' key ', ' Test by www.phpddt.com '); Print_r ($APC->get_cache (' key ')); |
Summarize
1, using spinlocks lock mechanism, can achieve the best performance.
The 2,APC provides apc.php for monitoring and managing the APC cache. Don't forget to change your administrator name and password
3,APC By default, the shared memory is created by Mmap Anonymous mappings, and the cache objects are stored in this "large" memory space. Self-control by APC
Deserve shared memory
4, we need to adjust the value of Apc.shm_size, Apc.num_files_hints, apc.user_entries_hint by statistics. Until the most
Good
5, okay, I admit that Apc.stat = 0 can achieve better performance. Anything I can do is acceptable.
6,php pre-defined constants, you can use the apc_define_constants () function. However, according to APC developers, PECL HiDef performance is more
Good, define, it is inefficient.
7, Function Apc_store (), for PHP variables such as system settings, the lifecycle is the entire application (from the httpd daemon until the httpd daemon
Using APC is better than memcached. Must not go through the Network Transport Protocol TCP.
8,APC is not suitable for caching frequently changed user data through the function Apc_store (), there are some strange phenomena.
http://www.bkjia.com/PHPjc/629903.html www.bkjia.com true http://www.bkjia.com/PHPjc/629903.html techarticle today to do a large file upload progress effect, later on the web said that there is PHP APC can be achieved, and then Baidu a bit PHP APC information, PHP APC provides two kinds of caching functions, that is, cache opcode (target text ...)