Home Backend Development PHP Tutorial Optimize PHP programs using APC cache_PHP tutorial

Optimize PHP programs using APC cache_PHP tutorial

Jul 13, 2016 pm 05:35 PM
apc cache php optimization use free public yes of program cache

Alternative PHP Cache(APC)是 PHP 的一个免费公开的优化代码缓存。它用来提供免费,公开并且强健的架构来缓存和优化 PHP 的中间代码。

APC 官方网站为http://pecl.php.net/package/apc

Windows下的拓展模块下载地址http://www.sitebuddy.com/php/accelerators/apc_windows_binaries_builds

在 Windows 下,APC 要求有 c: mp 目录,并且该目录要对 Web 服务器进程可写。

1、安装

以PHP extension 形式安装

2、配置

apc.enabled        boolean

apc.optimization   optimization

选项在脚本中可以改变

APC PHP.ini配置选项详解

[APC]

; Alternative PHP Cache 用于缓存和优化PHP中间代码

apc.cache_by_default = On

;SYS

; 是否默认对所有文件启用缓冲。

; 若设为Off并与以加号开头的apc.filters指令一起用,则文件仅在匹配过滤器时才被缓存。

apc.enable_cli = Off

;SYS

; 是否为CLI版本启用APC功能,仅用于测试和调试目的才打开此指令。

apc.enabled = On

; 是否启用APC,如果APC被静态编译进PHP又想禁用它,这是唯一的办法。

apc.file_update_protection = 2

;SYS

; 当你在一个运行中的服务器上修改文件时,你应当执行原子操作。

; 也就是先写进一个临时文件,然后将该文件重命名(mv)到最终的名字。

; 文本编辑器以及 cp, tar 等程序却并不是这样操作的,从而导致有可能缓冲了残缺的文件。

; 默认值 2 表示在访问文件时如果发现修改时间距离访问时间小于 2 秒则不做缓冲。

; 那个不幸的访问者可能得到残缺的内容,但是这种坏影响却不会通过缓存扩大化。

; 如果你能确保所有的更新操作都是原子操作,那么可以用 0 关闭此特性。

; 如果你的系统由于大量的IO操作导致更新缓慢,你就需要增大此值。

apc.filters =

;SYS

; 一个以逗号分隔的POSIX扩展正则表达式列表。

; 如果源文件名与任意一个模式匹配,则该文件不被缓存。

; 注意,用来匹配的文件名是传递给include/require的文件名,而不是绝对路径。

; 如果正则表达式的第一个字符是"+"则意味着任何匹配表达式的文件会被缓存,

; 如果第一个字符是"-"则任何匹配项都不会被缓存。"-"是默认值,可以省略掉。

apc.ttl = 0

;SYS

; 缓存条目在缓冲区中允许逗留的秒数。0 表示永不超时。建议值为7200~36000。

; 设为 0 意味着缓冲区有可能被旧的缓存条目填满,从而导致无法缓存新条目。

apc.user_ttl = 0

;SYS

; 类似于apc.ttl,只是针对每个用户而言,建议值为7200~36000。

; 设为 0 意味着缓冲区有可能被旧的缓存条目填满,从而导致无法缓存新条目。

apc.gc_ttl = 3600

;SYS

; 缓存条目在垃圾回收表中能够存在的秒数。

; 此值提供了一个安全措施,即使一个服务器进程在执行缓存的源文件时崩溃,

; 而且该源文件已经被修改,为旧版本分配的内存也不会被回收,直到达到此TTL值为止。

; 设为零将禁用此特性。

apc.include_once_override = Off

;SYS

; 请保持为Off,否则可能导致意想不到的结果。

apc.max_file_size = 1M

;SYS

; 禁止大于此尺寸的文件被缓存。

apc.mmap_file_mask =

;SYS

; 如果使用–enable-mmap(默认启用)为APC编译了MMAP支持,

; 这里的值就是传递给mmap模块的mktemp风格的文件掩码(建议值为"/tmp/apc.XXXXXX")。

; 该掩码用于决定内存映射区域是否要被file-backed或者shared memory backed。

; 对于直接的file-backed内存映射,要设置成"/tmp/apc.XXXXXX"的样子(恰好6个X)。

; 要使用POSIX风格的shm_open/mmap就需要设置成"/apc.shm.XXXXXX"的样子。

; 你还可以设为"/dev/zero"来为匿名映射的内存使用内核的"/dev/zero"接口。

; 不定义此指令则表示强制使用匿名映射。

apc.num_files_hint = 1000

;SYS

; Web服务器上可能被包含或被请求的不同源文件的大致数量(建议值为1024~4096)。

; 如果你不能确定,则设为 0 ;此设定主要用于拥有数千个源文件的站点。

apc.optimization = 0

; 优化级别(建议值为 0 ) 。

; 正整数值表示启用优化器,值越高则使用越激进的优化。

; 更高的值可能有非常有限的速度提升,但目前尚在试验中。

apc.report_autofilter = Off

;SYS

; Whether to record all scripts that are automatically not cached due to early/late binding reasons.

apc.shm_segments = 1

;SYS

; The number of shared memory blocks allocated for the compiler buffer (recommended value is 1).

; If APC runs out of shared memory and the apc.shm_size directive has been set to the maximum allowed by the system,

; You can try increasing this value.

apc.shm_size = 30

;SYS

; The size of each shared memory block (in MB, the recommended value is 128~256).

; Some systems (including most BSD variants) have a very small default shared memory block size.

apc.slam_defense = 0

;SYS (It is against the use of this command, it is recommended to use the apc.write_lock command)

; On a very busy server, whether starting a service or modifying a file,

; can cause a race condition due to multiple processes trying to cache a file at the same time.

; This directive is used to set the percentage of the process that skips the caching step when processing uncached files.

; For example, setting it to 75 means that there is a 75% probability of not caching when an uncached file is encountered, thereby reducing the chance of collision.

; It is encouraged to set to 0 to disable this feature.

apc.stat = On

;SYS

; Whether to enable script update checking.

; Be very careful when changing this command value.

; The default value On means that APC checks whether the script has been updated every time it is requested,

; Automatically recompile and cache the compiled content if updated. However, doing so has a negative impact on performance.

; If set to Off, no checking is performed, thus greatly improving performance.

; But in order for the updated content to take effect, you must restart the web server.

; This directive is also valid for include/require files. But it should be noted that

; If you use relative paths, APC must check to locate the file every include/require.

; Using absolute paths can skip the check, so you are encouraged to use absolute paths for include/require operations.

apc.user_entries_hint = 100

;SYS

; Similar to the num_files_hint directive, but for each different user.

; Set to 0 if you are not sure.

apc.write_lock = On

;SYS

; Whether to enable write lock.

; On a very busy server, whether starting a service or modifying a file,

; can cause a race condition due to multiple processes trying to cache a file at the same time.

; Enabling this directive can avoid race conditions.

apc.rfc1867 = Off

;SYS

; After turning on this command, for each uploaded file that contains the APC_UPLOAD_PROGRESS field just before the file field,

; APC will automatically create a user cache entry for upload_ (that is, the APC_UPLOAD_PROGRESS field value).

3. Function

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, with only a few functions, listed below.

apc_cache_info () returns cache information

apc_clear_cache() clears the apc cache content.

By default (no parameters), only the system cache is cleared. To clear the user cache, the ‘user’ parameter is required.

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

apc_load_constants (string Key).

Remove constant cache.

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

Save data in cache.

apc_fetch (string key).

Get the cache content saved by apc_store

apc_delete (string key).

Delete the content saved by apc_store.

apc management:

Go to pecl.php.net to download the apc source code package. There is apc.php, copy it to a place where your web server can access it, and browse to access it.

Management interface functions include:

1. Refresh Data

2. View Host Stats

3. System Cache Entries

4. User Cache Entries

5. Version Check

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508271.htmlTechArticleAlternative PHP Cache (APC) is a free and public optimized code cache for PHP. It is used to provide a free, open and robust framework for caching and optimizing PHP intermediate code. APC Official...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

See all articles