


Detailed explanation of the installation and use of PHP APC_PHP tutorial
1. PHPAPC installation
Download and unzip the installation package:
wget-c http://pecl.php.net/get/APC-3.1.13.tgz
tar-zvxf APC-3.1.13.tgz
cdAPC- 3.1.13
Compile and install:
wget -c http://pecl.php.net/get/APC-3.1.13.tgz
/usr/local/php/bin/phpize
./configure--enable-apc - -enable-mmap --enable-apc-spinlocks--disable-apc-pthreadmutex--with-php-config=/usr/local/php/bin/php-config
make
sudomake install
Modify php.ini and add:
extension= "apc.so" ;
;APCsetting
apc.enabled= 1
apc.shm_segments= 1
apc.shm_size= 64M
apc.optimization= 1
apc. num_files_hint= 0
apc.ttl= 0
apc.gc_ttl= 3600
apc.cache_by_default= on
重启apache或者/usr/local/php/sbin/php-fpmrestart
2. PHPAPC configuration options
[APC]
;Alternative PHP Cache is used to cache and optimize PHP intermediate code
apc.cache_by_default = On
; Whether to enable buffering for all files by default.
;If set to Off and used with the apc.filters directive starting with a plus sign, files will only be cached if they match the filter.
apc.enable_cli= Off
; Whether to enable the APC function for the CLI version, turn this command on only for testing and debugging purposes.
apc.enabled= On
; Whether to enable APC. If APC is statically compiled into PHP and you want to disable it, this is the only way.
apc.file_update_protection= 2
;When you modify files on a running server, you should perform atomic operations.
;That is, first write into a temporary file, and then rename (mv) the file to the final name.
;Text editors and programs such as cp and tar do not operate in this way, resulting in the possibility of buffering incomplete files.
;The default value 2 means that when accessing a file, if the modification time is found to be less than 2 seconds from the access time, no buffering will be performed.
;The unlucky visitor may get incomplete content, but the bad effect is not magnified by caching.
; If you can ensure that all update operations are atomic operations, you can turn off this feature with 0.
; If your system updates slowly due to heavy IO operations, you will need to increase this value.
apc.filters=
;A comma-separated list of POSIX extended regular expressions.
;If the source file name matches any of the patterns, the file will not be cached.
; Note that the file name used for matching is the file name passed to include/require, not the absolute path.
;If the first character of the regular expression is "+" it means any file matching the expression will be cached,
;If the first character is "-" then any matches will not is cached. "-" is the default value and can be omitted.
apc.ttl= 0
;The number of seconds a cache entry is allowed to stay in the buffer. 0 means never times out. The recommended value is 7200~36000.
;Setting to 0 means that the buffer may be filled with old cache entries, preventing new entries from being cached.
apc.user_ttl= 0
; Similar to apc.ttl, but for each user, the recommended value is 7200~36000.
;Setting to 0 means that the buffer may be filled with old cache entries, preventing new entries from being cached.
apc.gc_ttl= 3600
;The number of seconds that cache entries can exist in the garbage collection table.
;This value provides a safety measure, even if a server process crashes while executing a cached source file,
;and the source file has been modified, the memory allocated for the old version will not be reclaimed until until this TTL value is reached.
;Set to zero to disable this feature.
apc.include_once_override= Off
; Please keep it Off, otherwise it may cause unexpected results.
apc.max_file_size= 1M
;Does not allow files larger than this size to be cached.
apc.mmap_file_mask=
; If MMAP support is compiled for APC using –enable-mmap (enabled by default),
; the value here is the mktemp style passed to the mmap module File mask (recommended value is "/tmp/apc.XXXXXX").
;This mask is used to determine whether the memory mapped area should be file-backed or sharedmemory backed.
;For direct file-backed memory mapping, set it to "/tmp/apc.XXXXXX" (exactly 6 X's).
;To use POSIX-style shm_open/mmap, you need to set it 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 means forcing the use of anonymous mapping.
apc.num_files_hint= 1000
; The approximate number of different source files that may be included or requested on the web server (recommended value is 1024~4096).
; If you are not sure, set to 0; this setting is mainly used for sites with thousands of source files.
apc.optimization= 0
; Optimization level (recommended value is 0).
; A positive integer value indicates that the optimizer is enabled, and higher values use more aggressive optimization.
;Higher values may have very limited speed improvements, but are currently experimental.
apc.report_autofilter= Off
;Whether to record all scripts that are automatically not cached due to early/latebinding reasons.
apc.shm_segments= 1
;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
; 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
;(It is against the use of this command, it is recommended to use the apc.write_lock command)
;On a very busy server, whether it is starting a service or modifying a file ,
; may all cause race conditions due to multiple processes trying to cache a file at the same time.
;This directive is used to set the percentage of the process skipping 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.
;Encouraged to be set to 0 to disable this feature.
apc.stat= On
;Whether to enable script update check.
;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.
;If it is updated, it will automatically recompile and cache the compiled content. However, doing so has a negative impact on performance.
;If set to Off, it means no checking will be 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 a relative path, APC must check to locate the file every time 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
; Similar to the num_files_hint directive, but for each different user.
; If you are not sure, set to 0.
apc.write_lock= On
; Whether to enable write lock.
;On a very busy server, whether starting a service or modifying a file,
;it is possible to 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
; After turning on this command, for each file field that contains
3. PHP_APC function
apc_add—Cache a variable to data storage
apc_bin_dump— Get a binary dump of the given files and user variables
apc_bin_dumpfile— Output a binary dump of cached files and user variables to a file
apc_bin_load— Load a binary dump into the APC file/user cache
apc_bin_loadfile— Load a binary dump from a file into the APC file/user cache
apc_cache_info— Retrieves cached information from APC's data store
apc_cas— Updates an old value with a new value
apc_clear_cache—Clear APC cache
apc_compile_file— Stores a file in the bytecode cache, bypassing all filters.
apc_dec— Decrease a stored number
apc_define_constants— Defines a set of constants for retrieval and mass-definition
apc_delete_file— Deletes files from the opcode cache
apc_delete—Delete a variable from the user cache
apc_exists—Check whether a certain or certain keys exist in APC
apc_fetch—Loads a set of constants from the cache
apc_sma_info— Retrieves APC's Shared Memory Allocation information
apc_store— Cache a variable in the data store
4. PHPAPC usage
Copy code The code is as follows:
error_reporting(E_ALL);
classapcInit {
/**
*Apc cache-set cache
*Set cache key, value and cache time
*@param string $key KEY value
*@param string $value
*@param string $ timecache time
*/
publicstatic function set_cache($key, $value, $time = 0) {
if($time == 0) $time = null; //null情况下永久缓存
returnapc_store($key, $value, $time);;
}
/**
*Apc cache-get cache
*Get cache data by KEY
*@param string $key KEY value
*/
publicstatic function get_cache($key) {
returnapc_fetch($key);
}
/**
*Apc cache-clear a cache
*Delete a cache from memcache
*@param string $key KEY value
*/
publicstatic function clear($key) {
returnapc_delete($key);
}
/**
*Apc Cache-Clear all caches
*This feature is not recommended
*@return
*/
publicstatic function clear_all() {
returnapc_clear_cache(); //清楚缓存
}
/**
*Check if APC cache exists
*@param string $key KEY value
*/
publicstatic function exists($key) {
returnapc_exists($key);
}
/**
*Field auto-increment - used for counting
*@param string $key KEY value
*@param int $step New step value
*/
publicstatic function inc($key, $step) {
returnapc_inc($key, (int) $step);
}
/**
*Field decrement - used for counting
*@param string $key KEY value
*@param int $step New step value
*/
publicstatic function dec($key, $step) {
returnapc_dec($key, (int) $step);
}
/**
*Cache files
*/
publicstatic function cache_file($file) {
returnapc_compile_file($file);
}
/**
*Return APC cache information
*/
publicstatic function info() {
returnapc_cache_info();
}
}
apc_clear_cache();
apcInit::cache_file("HessianUtils.php");
apcInit::cache_file("Hessian1Parser.php");
apcInit::cache_file("Hessian1Writer.php");
apcInit::cache_file("HessianStream.php");
apcInit::cache_file("HessianParsing.php");
apcInit::cache_file("HessianExceptions.php");
include_once"./HessianUtils.php";
include_once"./Hessian1Parser.php";
include_once"./Hessian1Writer.php";
include_once"./HessianStream.php";
include_once"./HessianParsing.php";
include_once"./HessianExceptions.php";
header("Content-type:text/html;charset=utf-8");
header("Cache-Control:private,max-age=0,no-cache");
$HessianWriter= new Hessian1Writer();
$array= array(1, 2, 3, array('sff','张三'));
//parser
$resolver= new HessianRuleResolver('./hessian1rules.php');
//解析array
$parser_array= new Hessian1Parser($resolver, newHessianStream($HessianWriter->writeValue($array)));
print_r($parser_array->parse());
?>
五、PHPAPC监控与管理
APC提供了apc.php,用于监控与管理APC缓存。将apc.php放到网站目录中,修改账号、密码,即可通过浏览器监控与管理APC缓存。
apc.php第41、42行:
defaults('ADMIN_USERNAME','apc');// Admin Username
defaults('ADMIN_PASSWORD','password'); // Admin Password - CHANGE THIS TO ENABLE!!!

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

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

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











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 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 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 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 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.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

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 is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
