Today, I want to implement the upload progress of a large file. Later I said on the Internet that php apc can be implemented. Later I learned about php apc information, and PHP APC provides two caching functions, that is, cache Opcode (target file), which is called apc_compiler_cache. It also provides some interfaces for PHP developers to resident user data in the memory, which is called apc_user_cache.
Want to use APC cache in the end how the effect, but in windows can not find the corresponding version of The php-apc.dll extension file, had
Install yum directly without using the source code:
First install the apc dependency package:
The Code is as follows: |
Copy code |
Yum install php-pear php-devel httpd-devel pcre-devel gcc make
|
Then install apc using pecl:
The Code is as follows: |
Copy code |
Pecl install apc
|
Add apc extension to configuration file:
The Code is as follows: |
Copy code |
Echo "extension = apc. so">/etc/php. d/apc. ini
|
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 features of APC that cache the upload progress of large files. in php. ini, we need to set apc. rfc1867 to 1,
Add a hidden field APC_UPLOAD_PROGRESS to the form. The value of this field can generate a random hash to ensure uniqueness.
APC has many settings, which you can set in php. ini, such:
The Code is as follows: |
Copy code |
[APC] Apc. enabled = 1 Apc. shm_segments = 1 Apc. shm_size = 64 Apc. max_file_size = 10 M Apc. stat = 1
|
I tried the common APC functions in PHP:
The Code is as follows: |
Copy code |
<? Php
/* Add variables to Data Storage Bool apc_add (string $ key, mixed $ var [, int $ ttl = 0]) If the key exists, it is not overwritten, but false is returned. */ Apc_add ('url', 'HTTP: // www.111cn. net '); /* Retrieve the stored variables from the cache Mixed apc_fetch (mixed $ key [, bool & $ success]) */ Var_dump (apc_fetch ('url ')); /* The. key is unique for variables stored in apc_store (), so the two values use the same name and the original values will be overwritten by the new values. Bool apc_store (string $ key, mixed $ var [, int $ ttl = 0]) */ Apc_store ('var', 'new variable '); /* Delete 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 whether a key or some Key exists in APC. If the key exists, TRUE is returned; otherwise, FALSE is returned. Mixed apc_exists (mixed $ keys) */ If (apc_exsists ('url ')){ Echo "this key actually exists "; } Else { Echo "it seems that this key does not exist "; } /* Increments a stored number, returns the current value of the key when the operation succeeds, or returns FALSE if the operation fails. Int apc_inc (string $ key [, int $ step = 1 [, bool & $ success]) */ Apc_store ('number', 42 ); $ Ret = apc_inc ('number', 1, $ fail ); Var_dump ($ ret ); Var_dump ($ fail ); /* Decrease the number of a storage variable, return the current value of the key if the operation succeeds, or return FALSE if the operation fails. Int apc_dec (string $ key [, int $ step = 1 [, bool & $ success]) */ $ Ret = apc_dec ('astring', 1, $ fail ); Var_dump ($ ret ); Var_dump ($ fail ); |
In addition, a useful APC cache class is provided:
The Code is as follows: |
Copy code |
<? Php /*************************************** **************************************** ** * APC cache class, copyright (from InitPHP Framework) **************************************** **************************************** ***/ Class MyApc { /** * Apc cache-set Cache * Set the cache key, value, and cache time. * @ Param string $ key KEY value * @ Param string $ value * @ Param string $ time Cache time */ Public function set_cache ($ key, $ value, $ time = 0 ){ If ($ time = 0) $ time = null; // permanent cache when null Return apc_store ($ key, $ value, $ time );; } /** * Apc cache-Get Cache * Obtain cache data using the KEY * @ Param string $ key KEY value */ Public function get_cache ($ key ){ Return apc_fetch ($ key ); } /** * Apc cache-Clear a cache * Delete A cache from memcache * @ Param string $ key KEY value */ Public function clear ($ key ){ Return apc_delete ($ key ); } /** * Apc cache-clear all caches * This function is not recommended. * @ Return */ Public function clear_all (){ Apc_clear_cache ('user'); // clear the user Cache Return apc_clear_cache (); // clear Cache } /** * Check whether the APC cache exists. * @ Param string $ key KEY value */ Public function exists ($ key ){ Return apc_exists ($ key ); } /** * Field Auto-increment-used for counting * @ Param string $ key KEY value * @ Param int $ step the new step value */ Public function inc ($ key, $ step ){ Return apc_inc ($ key, (int) $ step ); } /** * Field Auto-subtraction-used for counting * @ Param string $ key KEY value * @ Param int $ step the 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 (); } } // Use the following method: $ Apc = new MyApc (); $ Apc-> set_cache ('key', 'test by www.phpddt.com '); Print_r ($ apc-> get_cache ('key ')); |
Summary
1. Use the Spinlocks lock mechanism to achieve optimal performance.
2. APC provides apc. php for monitoring and managing the APC cache. Do not forget to change the Administrator name and password
3. By default, APC creates shared memory through mmap anonymous ing, and cache objects are stored in this "large" memory space. Managed by APC
Manage the shared memory
4. We need to adjust the values of apc. shm_size, apc. num_files_hints, and apc. user_entries_hint through statistics. Until
Jia
5. Well, I admit that apc. stat = 0 can achieve better performance. Everything I want to do is acceptable.
6. PHP pre-defined constants. You can use the apc_define_constants () function. However, according to APC developers, the performance of pecl hidef is more
Good. It is inefficient.
7. Function apc_store (). For PHP variables such as system settings, the lifecycle is the whole application (from the httpd daemon process to the httpd daemon ).
). It is better to use APC than Memcached. It must not pass through the network transmission protocol tcp.
8. APC is not suitable for caching user data that frequently changes through the apc_store () function, and some strange phenomena may occur.