Installation and Use of PHP APC

Source: Internet
Author: User
Tags apc

1. Install PHPAPC
Download and unzip the installation package:

Copy codeThe Code is as follows: wget-c http://pecl.php.net/get/APC-3.1.13.tgz
Tar-zvxf APC-3.1.13.tgz
CdAPC-3.1.13

Compile and install:
Copy codeThe Code is as follows: wget-c http://pecl.php.net/get/APC-3.1.13.tgz
/Usr/local/php/bin/phpize
. /Configure -- enable-apc -- enable-mmap -- enable-apc-spinlocks -- disable-apc-pthreadmutex -- with-php-config =/usr/local/php/bin/php- config
Make
Sudomake install

Modify php. ini to add:
Copy codeThe Code is as follows: extension = "apc. so ";
; APCsetting
Apc. enabled = 1
Apc. shm_segments = 1
Apc. shm_size = 64 M
Apc. optimization = 1
Apc. num_files_hint = 0
Apc. ttl = 0
Apc. gc_ttl = 3600
Apc. cache_by_default = on

Restart apache or/usr/local/php/sbin/php-fpmrestart

Ii. PHPAPC configuration options
[APC]
; Alternative PHP Cache for caching and optimizing PHP intermediate code
Apc. cache_by_default = On
Whether to enable caching for all files by default.
If it is set to Off and used together with the apc. filters command starting with the plus sign, the file is cached only when the filter is matched.
Apc. enable_cli = Off
Whether to enable the APC function for the CLI version. This command is enabled only for testing and debugging purposes.
Apc. enabled = On
Whether to enable APC. If APC is statically compiled into PHP and you want to disable it, this is the only way.
Apc. file_update_protection = 2
When you modify a file on a running server, you should perform atomic operations.
That is, first write a temporary file, and then rename the file (mv) to the final name.
The text editor, cp, tar, and other programs do not perform this operation, which may buffer incomplete files.
The default value is 2, indicating that the file is not buffered if the modification time is found to be less than 2 seconds away from the Access time.
The unfortunate visitor may get incomplete content, but this bad effect will not be extended through the cache.
If you can ensure that all update operations are atomic operations, you can disable this feature with 0.
; If your system updates slowly due to a large number of IO operations, you need to increase this value.
Apc. filters =
; A list of POSIX extension Regular Expressions separated by commas.
If the source file name matches any mode, the file is not cached.
Note: The file name used for matching is the file name passed to include/require, rather than the absolute path.
If the first character of the regular expression is "+", it means that any file that matches the expression will be cached,
If the first character is "-", no match will be cached. "-" Is the default value, which can be omitted.
Apc. ttl = 0
The number of seconds that cache entries can stay in the buffer. 0 indicates never timeout. Recommended Value: 7200 ~ 36000.
If it is set to 0, the buffer may be filled with old cache entries, resulting in the inability to cache new entries.
Apc. user_ttl = 0
; Similar to apc. ttl, the recommended value is 7200 ~ for each user ~ 36000.
If it is set to 0, the buffer may be filled with old cache entries, resulting in the inability to cache new entries.
Apc. gc_ttl = 3600
The number of seconds that cache entries can exist in the garbage collection table.
This value provides a security measure, even if a server process crashes when the cached source file is executed,
And the source file has been modified, and the memory allocated for the old version will not be recycled until the TTL value is reached.
If it is set to zero, this feature is disabled.
Apc. include_once_override = Off
; Keep it Off. Otherwise, unexpected results may occur.
Apc. max_file_size = 1 M
; Files larger than this size cannot be cached.
Apc. mmap_file_mask =
If you use-enable-mmap (enabled by default) to compile MMAP support for APC,
The value here is the mktemp file mask passed to the mmap module (Recommended Value: "/tmp/apc. XXXXXX ").
The mask is used to determine whether the memory ing area is file-backed or sharedmemory backed.
For direct file-backed memory ing, set it to "/tmp/apc. XXXXXX" (exactly 6 X ).
To use the POSIX shm_open/mmap style, you must set it to "/apc. shm. XXXXXX.
You can also set it to "/dev/zero" to use the kernel "/dev/zero" interface for anonymous ing memory.
If this command is not defined, anonymous ing is mandatory.
Apc. num_files_hint = 1000
The approximate number of different source files that may be contained or requested on the Web server (Recommended Value: 1024 ~ 4096 ).
If you are not sure, set it to 0. This setting is mainly used for websites with thousands of source files.
Apc. optimization = 0
; Optimization level (Recommended Value: 0 ).
The positive integer value indicates that the optimizer is enabled. The higher the value, the more radical optimization is used.
; A higher value may have a very limited speed increase, but it is still being tested.
Apc. report_autofilter = Off
; Whether to record all scripts that are automatically not cached due to early/latebinding.
Apc. shm_segments = 1
; The number of shared memory blocks allocated to the compiler buffer (Recommended Value: 1 ).
If APC consumes shared memory and the apc. shm_size command is set to the maximum value allowed by the system,
; You can try to increase this value.
Apc. shm_size = 30
; The size of each shared memory block (in MB, recommended value: 128 ~ 256 ).
Some systems (including most BSD variants) have very few default shared memory blocks.
Apc. slam_defense = 0
; (Opposed to using this command. We recommend that you use the apc. write_lock command)
On very busy servers, whether it is to start the service or modify files,
; May be caused by multiple processes attempting to cache a file at the same time.
This command is used to set the percentage of cached steps skipped by a process when processing files that are not cached.
For example, if it is set to 75, it indicates that no cache is performed for files that are not cached, reducing the chance of collision.
We recommend that you set it to 0 to disable this feature.
Apc. stat = On
; Whether to enable the script Update check.
Be very careful when changing the command value.
The default value On indicates that APC checks whether the script is updated every time it requests the script,
If updated, the compiled content is automatically re-compiled and cached. However, this has a negative impact on performance.
If it is set to Off, the check is not performed, which greatly improves the performance.
To make the updated content take effect, you must restart the Web server.
This command is also valid for include/require files. However, you must note that,
If you are using a relative path, APC must check each include/require operation to locate the file.
You can skip the check using the absolute path. Therefore, you are encouraged to use the absolute path for the include/require operation.
Apc. user_entries_hint = 100
; Similar to the num_files_hint command, only for different users.
If you are not sure, set it to 0.
Apc. write_lock = On
Whether to enable the write lock.
On very busy servers, whether it is to start the service or modify files,
; May be caused by multiple processes attempting to cache a file at the same time.
; Enable this command to avoid competition conditions.
Apc. rfc1867 = Off
When this command is enabled

3. PHP_APC Function
Apc_add
-Cache a variable to Data Storage
Apc_bin_dump-Get a binary dump of the given files and user variables
Apc_bin_dumpfile-Output a binary dump of cached files and user variables to a file
Apc_bin_load-Load a binary dump into the APC file/user cache
Apc_bin_loadfile-Load a binary dump from a file into the APC file/user cache
Apc_cache_info-Retrieves cached information from APC's data store
Apc_cas-Updates an old value with a new value
Apc_clear_cache-Clear APC Cache
Apc_compile_file-Stores a file in the bytecode cache, bypassing all filters.
Apc_dec-Decrease a stored number
Apc_define_constants-Defines a set of constants for retrieval and mass-definition
Apc_delete_file-Deletes files from the opcode cache
Apc_delete-Delete a variable from the user cache.
Apc_exists-Check whether a key or some key exists in APC.
Apc_fetch-Retrieve the stored variables from the cache.
Apc_inc-Increment a stored number
Apc_load_constants-Loads a set of constants from the cache
Apc_sma_info-Retrieves APC's Shared Memory Allocation information
Apc_store-Cache a variable in the data store

4. Use PHPAPC
Example of using apc cache (test_apc_cache.php ):

Copy codeThe Code is as follows: <? Php
Error_reporting (E_ALL );
ClassapcInit {
/**
* 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
*/
Publicstatic function set_cache ($ key, $ value, $ time = 0 ){
If ($ time = 0) $ time = null; // permanent cache when null
Returnapc_store ($ key, $ value, $ time );;
}
/**
* Apc cache-Get Cache
* Obtain cache data using the KEY
* @ Param string $ key KEY value
*/
Publicstatic function get_cache ($ key ){
Returnapc_fetch ($ key );
}
/**
* Apc cache-Clear a cache
* Delete A cache from memcache
* @ Param string $ key KEY value
*/
Publicstatic function clear ($ key ){
Returnapc_delete ($ key );
}
/**
* Apc cache-clear all caches
* This function is not recommended.
* @ Return
*/
Publicstatic function clear_all (){
Returnapc_clear_cache (); // clear the cache
}
/**
* Check whether the APC cache exists.
* @ Param string $ key KEY value
*/
Publicstatic function exists ($ key ){
Returnapc_exists ($ key );
}
/**
* Field Auto-increment-used for counting
* @ Param string $ key KEY value
* @ Param int $ step the new step value
*/
Publicstatic function inc ($ key, $ step ){
Returnapc_inc ($ key, (int) $ step );
}
/**
* Field Auto-subtraction-used for counting
* @ Param string $ key KEY value
* @ Param int $ step the new step value
*/
Publicstatic function dec ($ key, $ step ){
Returnapc_dec ($ key, (int) $ step );
}
/**
* Cache files
*/
Publicstatic function cache_file ($ file ){
Returnapc_compile_file ($ file );
}
/**
* Return APC Cache Information
*/
Publicstatic function info (){
Returnapc_cache_info ();
}
}
Apc_clear_cache ();
ApcInit: cache_file ("HessianUtils. php ");
ApcInit: cache_file ("Hessian1Parser. php ");
ApcInit: cache_file ("Hessian1Writer. php ");
ApcInit: cache_file ("HessianStream. php ");
ApcInit: cache_file ("HessianParsing. php ");
ApcInit: cache_file ("HessianExceptions. php ");
Include_once "./HessianUtils. php ";
Include_once "./Hessian1Parser. php ";
Include_once "./Hessian1Writer. php ";
Include_once "./HessianStream. php ";
Include_once "./HessianParsing. php ";
Include_once "./HessianExceptions. php ";
Header ("Content-type: text/html; charset = UTF-8 ");
Header ("Cache-Control: private, max-age = 0, no-cache ");
$ HessianWriter = new Hessian1Writer ();
$ Array = array (1, 2, 3, array ('sff', 'zhang san '));
// Parser
$ Resolver = new HessianRuleResolver ('./hessian1rules. php ');
// Parse the array
$ Parser_array = new Hessian1Parser ($ resolver, newHessianStream ($ HessianWriter-> writeValue ($ array )));
Print_r ($ parser_array-> parse ());
?>

V. Monitoring and Management of PHPAPC
APC provides apc. php for monitoring and managing APC cache. Place apc. php In the website directory, modify the account and password, and monitor and manage the APC cache through the browser.
Apc. php lines 41st and 42:Copy codeThe Code is as follows: ults ('admin _ username', 'apc '); // ADMIN USERNAME
Ults ('admin _ password', 'Password'); // admin password-change this to enable !!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.