PHP program optimization using APC cache _ PHP Tutorial

Source: Internet
Author: User
Use APC cache to optimize PHP programs. AlternativePHPCache (APC) is a free and public optimized code cache for PHP. It is used to provide free, public, and robust architectures to cache and optimize PHP intermediate code. APC officer

Alternative PHP Cache (APC) is a free and public PHP optimized code Cache. It is used to provide free, public, and robust architectures to cache and optimize PHP intermediate code.

Official APC website for

Extended module in Windows

In Windows, APC requires a c: mp directory, which must be writable to Web server processes.

1. Installation

Install in PHP extension format

2. configuration

Apc. enabled boolean

Apc. optimization

The options can be changed in the script.

Apc php. ini configuration options


; 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 shared memory 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/late binding.

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

; SYS (opposed to using this command, we recommend using 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


After this command is enabled, for each Upload file that contains the APC_UPLOAD_PROGRESS field just before the file field,

APC will automatically create an upload _ User cache entry (that is, the value of the APC_UPLOAD_PROGRESS field ).

3. Functions

Apc_cache_info-Retrieves cached information (and meta-data) from APCs data store
Apc_clear_cache-Clears the APC cache
Apc_define_constants-Defines a set of constants for later retrieval and mass-definition
Apc_delete-Removes a stored variable from the cache
Apc_fetch-Fetch a stored variable from the cache
Apc_load_constants-Loads a set of constants from the cache
Apc_sma_info-Retrieves APCs Shared Memory Allocation information
Apc_store-Cache a variable in the data store

The usage of apc is relatively simple. there are only a few functions, as listed below.

Apc_cache_info () returns the cache information

Apc_clear_cache () clears the apc cache content.

By default (no parameter), only the system cache is cleared. to clear the user cache, you must use the reset user' parameter.

Apc_define_constants (string key, array constants [, bool case_sensitive]) adds the array constants to the cache as constants.

Apc_load_constants (string Key ).

Retrieve constant cache.

Apc_store (string key, mixed var [, int ttl]).

Save data in the cache.

Apc_fetch (string key ).

Obtain the cache content saved by apc_store.

Apc_delete (string key ).

Delete the content saved by apc_store.

Apc management:

Go to to download the apc source package. there is an apc. php package. copy it to a place that your web server can access and browse it to access it.

Management interface functions:

1. Refresh Data

2. View Host Stats

3. System Cache Entries

4. User Cache Entries

5. Version Check

Http:// PHP Cache (APC) is a free and public optimized code Cache for PHP. It is used to provide free, public, and robust architectures to cache and optimize PHP intermediate code. APC official...

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: 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.