Install PHPAPC and use an instance in yum in linux. Today, I want to implement the upload progress of a large file. later I said on the Internet that phpapc can be implemented. later I learned about phpapc, which provides two caching functions, that is, cache Opcode (the target file needs to be uploaded to a large file today. later, php apc can be implemented on the internet. later, Baidu gave the php apc information. 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: |
|
Yum install php-pear php-devel httpd-devel pcre-devel gcc make
|
Then install apc using pecl:
The code is as follows: |
|
Pecl install apc
|
Add apc extension to configuration file:
The code is as follows: |
|
Echo "extension = apc. so">/etc/php. d/apc. ini
|
Remember to restart the server:
The code is as follows: |
|
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: |
|
[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: |
|
/* 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: |
|
/*************************************** **************************************** ** * 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.
Apc can be implemented. later, Baidu gave the php apc information. php apc provides two caching functions: cache Opcode (target text...