PHP缓存之模块缓存(APC)
PHP缓存之模块缓存(APC)
APC是Alternative PHP Cache的简称,是 PHP 的一个免费公开的优化代码缓存。它用来提供免费,公开并且强健的架构来缓存和优化 PHP 的中间代码。
1、PHP配置APC
开启APC模块,需要将 extension=php_apc.dll 前面的注释去掉即可。
APC模块的配置参数如下:
apc.enabled
类型:布尔型
apc.enabled 可以被设成 0 来禁用 APC。这主要是有用的,当 APC 被静态编译入 PHP 时,因为没有其它方法来禁用它(当编译为 DSO 的时候,可以将 php.ini 中的 extension 行注释掉)。
apc.shm_segments
类型: 整型
对编译缓存分配共享内存块的数量。如果APC用光了共享内存,而且你已经设置 apc.shm_size为系统允许的最大值的情况下,你可以试着去提高这个参数的值。
apc.shm_size
类型:整型
每个共享内存块的大小是以MB为单位的。在默认情况下,一些系统(包括大多数BSD变种系统)的共享内存块的大小限制的很低。
apc.optimization
类型:整型
优化等级。设为0则禁用优化,越高的值使用越强有力的优化。期待有适度的速度上的改进。这个还是实验性质的。
apc.num_files_hint
类型: 整型
对在你的Web服务器上被包含和请求的不同的源文件的数量的提示。如果你无法确定,设置为0或者省略;这个设置主要可能用于有成千的源文件的站点。
apc.ttl
类型:整型
当一个缓存条目在缓存区的位置被另一个条目需要时,我们需要考虑的是这个缓存条目在缓存区的位置被允许空闲的秒数。将这个参数设置为0意味着你的缓存可能 充满不新鲜的条目,同时导致新的条目无法被缓存。
apc.gc_ttl
类型:整型
缓存条目在垃圾收集列表中存活的秒数。这个值提供了出错保护在执行一个缓存源文件,而同时服务器进程死了的事件中。如果那个源文件被修改,内存分配给旧版 本的缓存条目将不会被回收,直到这个参数设定的TTL值到的时候。设置为0就是禁止这个特性。
apc.cache_by_default
类型:布尔型
默认为On,但可以被设置为Off并和以加号开头的apc.filters配合使用,文件仅仅在匹配过滤器时才被缓存。
apc.filters
类型:字符串
一个以逗号分割的POSIX扩展正则表达式的列表。如果任何模式匹配源文件名,这个文件将不会被缓存。注意用来匹配的文件名是传递给 include/require 的文件名,而不是绝对路径。如果正则表达式的第一个字符是 + ,则这个表达式就意味着任何匹配表达式的文件将会被缓存,如果第一个字符是 - 则任何匹配都不会被缓存。 - 是默认值,所以可以被省略。
apc.mmap_file_mask
类型:字符串
如果使用–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.slam_defense
类型:整型
在非常繁忙的服务器上,无论你启动服务还是修改文件,你都会导致一种多进程都试图在同一个时间缓存同一个文件的竞争。这个选项设置了进程跳过试图去缓存一 个未被缓存的文件的百分比。或者可以把这个想象成一个单独进程跳过缓存的机率。例如,设置apc.slam_defense为75就意味着进程有75%的 机率不去缓存未被缓存的文件。所以,设置的越高,越能减少缓存的碰撞机率。设置为0则禁用这个特性。
apc.file_update_protection
类型:整型
当你在一个运行着的服务器上修改文件时,你应该执行原子操作。也就是,先写一个临时文件,当写完后再重命名(mv)这个文件到它的最终位置。许多文本编辑 器,cp,tar和其他一些类似程序都不是这样操作的。这就意味着有机会去访问和(缓存)文件,当这个文件还在被写的情况下。 apc.file_update_protection的设置使得缓存标记新文件的延迟。默认值是2,意味着如果发现文件的修改时间距离访问时间不到2 秒,文件将不会被缓存。访问写到一半的文件的不幸用户将会看到离奇的情况,但至少这种情况不是持续的。如果你确信你经常使用原子操作来更新你的文件,你可
以关闭这个保护通过设置这个参数为0。如果你的系统充满io操作,并导致更新程序花费超过2秒,你可能需要去增大这个值。
apc.enable-cli
类型:整型
大多是为了测试和调试。为CLI版本的PHP开启动APC功能。一般来说,你将不会想到为每一个 CLI请求创建,移植和放弃APC的缓存,但对于各种测试情况,这是很容易的为了CLI版本开启APC。
案例:
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缓存案例
<?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.
