PHP cache module cache (APC)_PHP tutorial

WBOY
Release: 2016-07-13 10:19:51
Original
1170 people have browsed it

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);
	}
}
?>
Copy after login


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...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!