search
HomeBackend DevelopmentPHP TutorialPHP cache module cache (APC)_PHP tutorial

PHP cache module cache (APC)

APC is the abbreviation of Alternative PHP Cache, which 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.

1. PHP configuration APC

To enable the APC module, you need to remove the comment in front of extension=php_apc.dll.
The configuration parameters of the APC module are as follows:

apc.enabled

Type: Boolean
apc.enabled can be set to 0 to disable APC. This is mainly useful when APC is statically compiled into PHP, since there is no other way to disable it (when compiling as DSO, the extension line in php.ini can be commented out).

apc.shm_segments

Type: Integer
The number of shared memory blocks allocated to the compilation cache. If APC runs out of shared memory and you have set apc.shm_size to the maximum value allowed by the system, you can try to increase the value of this parameter.

apc.shm_size

Type: Integer
The size of each shared memory block is in MB. By default, some systems (including most BSD variants) have very low shared memory block size limits.

apc.optimization

Type: Integer
Optimization level. Set to 0 to disable optimization, higher values ​​use more powerful optimizations. Expect modest speed improvements. This is still experimental in nature.

apc.num_files_hint

Type: Integer
An indication of the number of different source files being included and requested on your web server. If you are unsure, set it to 0 or omit it; this setting is likely to be useful primarily on sites with thousands of source files.

apc.ttl

Type: Integer
When a cache entry's location in the cache is needed by another entry, we need to consider the number of seconds that cache entry's location in the cache is allowed to be idle. Setting this parameter to 0 means that your cache may be filled with stale entries, causing new entries to not be cached.

apc.gc_ttl

Type: Integer
The number of seconds a cache entry remains alive in the garbage collection list. This value provides error protection in the event that a cached source file is executed and the server process dies at the same time. If that source file is modified, the memory allocated to the old version of the cache entry will not be reclaimed until the TTL value set by this parameter is reached. Setting it to 0 disables this feature.

apc.cache_by_default

Type: Boolean
Defaults to On, but can be set to Off and used with apc.filters starting with a plus sign. Files will only be cached if they match the filter.

apc.filters

Type: String
A comma-separated list of POSIX extended regular expressions. If any pattern matches the source file name, the file will not be cached. Note that the file name used to match is the file name passed to include/require, not the absolute path. If the first character of the regular expression is + , then the expression means that any file matching the expression will be cached, if the first character is - then any matches will not be cached. - is the default value, so can be omitted.

apc.mmap_file_mask

Type: String
If APC was compiled with MMAP support using --enable-mmap (enabled by default), the value here is the mktemp-style file mask passed to the mmap module (the recommended value is "/tmp/apc.XXXXXX"). This mask is used to determine whether the memory mapped area should be file-backed or shared memory backed. For direct file-backed memory mapping, set it to look like "/tmp/apc.XXXXXX" (exactly 6 X's). To use POSIX-style shm_open/mmap, it needs to be set to "/apc.shm.XXXXXX". You can also set it to "/dev/zero" to use the kernel's "/dev/zero" interface for anonymously mapped memory. Not defining this directive forces the use of anonymous mapping.


apc.slam_defense

Type: Integer
On a very busy server, whether you start a service or modify a file, you create a race for multiple processes trying to cache the same file at the same time. This option sets the percentage at which the process skips attempts to cache an uncached file. Or think of this as the probability of a single process skipping the cache. For example, setting apc.slam_defense to 75 means that the process has a 75% chance of not caching uncached files. Therefore, the higher the setting, the more likely it is to reduce the cache collision probability. Set to 0 to disable this feature.

apc.file_update_protection

Type: Integer
When you modify files on a running server, you should perform atomic operations. That is, write a temporary file first, and then rename (mv) the file to its final location when finished. Many text editors, cp, tar and other similar programs do not operate this way. This means there is an opportunity to access and (cache) the file while the file is still being written. The setting of apc.file_update_protection causes the cache to delay marking new files. The default value is 2, which means that if the modification time of the file is found to be less than 2 seconds from the access time, the file will not be cached. Unlucky users who access a half-written file will see bizarre behavior, but at least it's not persistent. If you are sure that you frequently use atomic operations to update your files, you can To turn off this protection by setting this parameter to 0. If your system is flooded with IO operations and causing the update process to take more than 2 seconds, you may need to increase this value.

apc.enable-cli

Type: Integer
Mostly for testing and debugging. Enable the APC function for the CLI version of PHP. Generally speaking, you wouldn't think of creating, porting, and discarding APC's cache for every CLI request, but for various testing situations, it's easy enough to turn on APC for the CLI version.


Case:
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64
apc.optimization = 0
apc.num_files_hint = 1000
apc.ttl = 0
apc.gc_ttl = 3600
apc.cache_by_default = On
apc.slam_defense = 0
apc.file_update_protection = 2
apc.enable_cli = 0
apc.stat=0

2. APC cache case

<?php
class Cache_Driver{


    //获取缓存信息
	public function get($id)
	{
	   $data = apc_fetch($id);
	   if (!is_array($data))
	   {
            $data = apc_fetch($id);
       }

		return (is_array($data)) ? $data[0] : FALSE;
	}
	//设置缓存信息
	public function set($id, $data, $ttl = 60)
	{
		return apc_store($id, array($data, time(), $ttl), $ttl);
	}
	//删除key值对应的缓存
	public function delete($id)
	{
		return apc_delete($id);
	}
}
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/871189.htmlTechArticlePHP Cache Module Cache (APC) APC is the abbreviation of Alternative PHP Cache, which is a free and public optimization of PHP Code caching. It is designed to provide a free, open and robust framework to mitigate...
Statement
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
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 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: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 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.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 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

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function