PHP 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); } } ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
![WLAN expansion module has stopped [fix]](https://img.php.cn/upload/article/000/465/014/170832352052603.gif?x-oss-process=image/resize,m_fill,h_207,w_330)
If there is a problem with the WLAN expansion module on your Windows computer, it may cause you to be disconnected from the Internet. This situation is often frustrating, but fortunately, this article provides some simple suggestions that can help you solve this problem and get your wireless connection working properly again. Fix WLAN Extensibility Module Has Stopped If the WLAN Extensibility Module has stopped working on your Windows computer, follow these suggestions to fix it: Run the Network and Internet Troubleshooter to disable and re-enable wireless network connections Restart the WLAN Autoconfiguration Service Modify Power Options Modify Advanced Power Settings Reinstall Network Adapter Driver Run Some Network Commands Now, let’s look at it in detail

This article details methods to resolve event ID10000, which indicates that the Wireless LAN expansion module cannot start. This error may appear in the event log of Windows 11/10 PC. The WLAN extensibility module is a component of Windows that allows independent hardware vendors (IHVs) and independent software vendors (ISVs) to provide users with customized wireless network features and functionality. It extends the capabilities of native Windows network components by adding Windows default functionality. The WLAN extensibility module is started as part of initialization when the operating system loads network components. If the Wireless LAN Expansion Module encounters a problem and cannot start, you may see an error message in the event viewer log.

Which folder does the browser cache the video in? When we use the Internet browser every day, we often watch various online videos, such as watching music videos on YouTube or watching movies on Netflix. These videos will be cached by the browser during the loading process so that they can be loaded quickly when played again in the future. So the question is, in which folder are these cached videos actually stored? Different browsers store cached video folders in different locations. Below we will introduce several common browsers and their

DNS (DomainNameSystem) is a system used on the Internet to convert domain names into corresponding IP addresses. In Linux systems, DNS caching is a mechanism that stores the mapping relationship between domain names and IP addresses locally, which can increase the speed of domain name resolution and reduce the burden on the DNS server. DNS caching allows the system to quickly retrieve the IP address when subsequently accessing the same domain name without having to issue a query request to the DNS server each time, thereby improving network performance and efficiency. This article will discuss with you how to view and refresh the DNS cache on Linux, as well as related details and sample code. Importance of DNS Caching In Linux systems, DNS caching plays a key role. its existence

Title: Caching mechanism and code examples of HTML files Introduction: When writing web pages, we often encounter browser cache problems. This article will introduce the caching mechanism of HTML files in detail and provide some specific code examples to help readers better understand and apply this mechanism. 1. Browser caching principle In the browser, whenever a web page is accessed, the browser will first check whether there is a copy of the web page in the cache. If there is, the web page content is obtained directly from the cache. This is the basic principle of browser caching. Benefits of browser caching mechanism

A Beginner's Guide to Guava Cache: Speed Up Your Applications Guava Cache is a high-performance in-memory caching library that can significantly improve application performance. It provides a variety of caching strategies, including LRU (least recently used), LFU (least recently used), and TTL (time to live). 1. Install Guava cache and add the dependency of Guava cache library to your project. com.goog

Optimizing Cache Size and Cleanup Strategies It is critical to allocate appropriate cache size to APCu. A cache that is too small cannot cache data effectively, while a cache that is too large wastes memory. Generally speaking, setting the cache size to 1/4 to 1/2 of the available memory is a reasonable range. Additionally, having an effective cleanup strategy ensures that stale or invalid data is not kept in the cache. You can use APCu's automatic cleaning feature or implement a custom cleaning mechanism. Sample code: //Set the cache size to 256MB apcu_add("cache_size",268435456); //Clear the cache every 60 minutes apcu_add("cache_ttl",60*60); Enable compression

In PHP development, the caching mechanism improves performance by temporarily storing frequently accessed data in memory or disk, thereby reducing the number of database accesses. Cache types mainly include memory, file and database cache. Caching can be implemented in PHP using built-in functions or third-party libraries, such as cache_get() and Memcache. Common practical applications include caching database query results to optimize query performance and caching page output to speed up rendering. The caching mechanism effectively improves website response speed, enhances user experience and reduces server load.
