Today I want to do a large file upload progress effect. Later, it was said on the Internet that there is PHP apc that can achieve it. Later, Baidu looked up the PHP apc information. PHP
APC provides two caching functions, namely caching Opcode (target file), which we call apc_compiler_cache. It also provides some
The interface is used by PHP developers to store user data in memory. We call it apc_user_cache
I want to use the APC cache to see how effective it is, but I can’t find the corresponding version of the php-apc.dll extension file in Windows, so I have to in linux
Installed under
, no need to install from source code, just yum:
First, you need to install the dependency package of apc:
The code is as follows |
Copy code |
yum install php-pear php-devel httpd- devel pcre-devel gcc make
代码如下 |
复制代码 |
yum install php-pear php-devel httpd-devel pcre-devel gcc make
|
|
Then use pecl to install apc:
The code is as follows |
Copy code |
pecl install apc
代码如下 |
复制代码 |
pecl install apc
|
|
Add apc extension to configuration file:
The code is as follows |
Copy code |
echo "extension=apc.so" > /etc/php.d/apc.ini
代码如下 |
复制代码 |
echo "extension=apc.so" > /etc/php.d/apc.ini
|
|
Finally remember to restart the server:
代码如下 |
复制代码 |
service httpd restart |
, and then use PHP’s phpinfo() function to detect:
APC cache function example
In APC, we can also enjoy the feature of caching large file upload progress brought by APC. We need to set apc.rfc1867 to 1 in php.ini,
And add a hidden field APC_UPLOAD_PROGRESS to the form. The value of this field can randomly generate a hash to ensure uniqueness.
APC has many settings, you can set it in php.ini, such as:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64
apc.max_file_size = 10M
apc.stat=1
|
[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64
apc.max_file_size = 10M
apc.stat=1
|
I tried the common APC function in PHP:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
/*添加变量到数据存储
bool apc_add ( string $key , mixed $var [, int $ttl = 0 ] )
如果key存在了,不会覆盖,但返回false
*/
apc_add('url','http://www.111cn.Net');
/*从缓存中取出存储的变量
mixed apc_fetch ( mixed $key [, bool &$success ] )
*/
var_dump(apc_fetch('url'));
/*使用 apc_store() 存储的变量,.key是唯一的,所以 两个值使用同一个名称,原来的将被新的值覆盖
bool apc_store ( string $key , mixed $var [, int $ttl = 0 ] )
*/
apc_store('var','新的变量');
/*从用户缓存中删除某个变量
mixed apc_delete ( string $key )
*/
apc_delete('url');
/*清楚apc缓存
bool apc_clear_cache ([ string $cache_type ] )
*/
apc_clear_cache('user');
/*检查APC中是否存在某个或者某些key,如果Key存在的话返回 TRUE , 否则返回 FALSE
mixed apc_exists ( mixed $keys )
*/
if(apc_exsists('url')){
echo "这个key真存在";
}else{
echo "貌似这个key是不存在的";
}
/*递增一个储存的数字,成功时返回 key 的当前值, 或者在失败时返回 FALSE
int apc_inc ( string $key [, int $step = 1 [, bool &$success ]] )
*/
apc_store('anumber', 42);
$ret = apc_inc('anumber', 1, $fail);
var_dump($ret);
var_dump($fail);
/*递减一个存储变量的数字,成功时返回 key 的当前值, 或者在失败时返回 FALSE
int apc_dec ( string $key [, int $step = 1 [, bool &$success ]] )
*/
$ret = apc_dec('astring', 1, $fail);
var_dump($ret);
var_dump($fail);
|
<🎜>/*Add variables to data storage
bool apc_add ( string $key , mixed $var [, int $ttl = 0 ] )
If the key exists, it will not be overwritten, but false will be returned.
*/
apc_add('url','http://www.111cn.Net');
/*Get the stored variables from the cache
mixed apc_fetch ( mixed $key [, bool &$success ] )
*/
var_dump(apc_fetch('url'));
/*Use apc_store() to store variables, .key is unique, so two values use the same name, and the original one will be overwritten by the new value
bool apc_store ( string $key , mixed $var [, int $ttl = 0 ] )
*/
apc_store('var','new variable');
/*Delete a variable from the user cache
mixed apc_delete ( string $key )
*/
apc_delete('url');
/*Clear apc cache
bool apc_clear_cache ([ string $cache_type ] )
*/
apc_clear_cache('user');
/*Check whether there is a certain key or keys in APC, if the key exists, return TRUE, otherwise return FALSE
mixed apc_exists ( mixed $keys )
*/
if(apc_exsists('url')){
echo "This key really exists";
}else{
echo "It seems that this key does not exist";
}
/*Increments a stored number, returning the current value of key on success, or FALSE on failure
int apc_inc ( string $key [, int $step = 1 [, bool &$success ]] )
*/
apc_store('anumber', 42);
$ret = apc_inc('anumber', 1, $fail);
var_dump($ret);
var_dump($fail);
/* Decrement a number stored in the variable, returning the current value of key on success, or FALSE on failure
int apc_dec ( string $key [, int $step = 1 [, bool &$success ]] )
*/
$ret = apc_dec('astring', 1, $fail);
var_dump($ret);
var_dump($fail);<🎜>
|
In addition, a very useful APC cache class is provided:
The code is as follows
代码如下 |
复制代码 |
/*********************************************************************************
* APC缓存类,版权(摘自InitPHP框架)
***********************************************************************************/
class MyApc {
/**
* Apc缓存-设置缓存
* 设置缓存key,value和缓存时间
* @param string $key KEY值
* @param string $value 值
* @param string $time 缓存时间
*/
public function set_cache($key, $value, $time = 0) {
if ($time == 0) $time = null; //null情况下永久缓存
return apc_store($key, $value, $time);;
}
/**
* Apc缓存-获取缓存
* 通过KEY获取缓存数据
* @param string $key KEY值
*/
public function get_cache($key) {
return apc_fetch($key);
}
/**
* Apc缓存-清除一个缓存
* 从memcache中删除一条缓存
* @param string $key KEY值
*/
public function clear($key) {
return apc_delete($key);
}
/**
* Apc缓存-清空所有缓存
* 不建议使用该功能
* @return
*/
public function clear_all() {
apc_clear_cache('user'); //清除用户缓存
return apc_clear_cache(); //清楚缓存
}
/**
* 检查APC缓存是否存在
* @param string $key KEY值
*/
public function exists($key) {
return apc_exists($key);
}
/**
* 字段自增-用于记数
* @param string $key KEY值
* @param int $step 新增的step值
*/
public function inc($key, $step) {
return apc_inc($key, (int) $step);
}
/**
* 字段自减-用于记数
* @param string $key KEY值
* @param int $step 新增的step值
*/
public function dec($key, $step) {
return apc_dec($key, (int) $step);
}
/**
* 返回APC缓存信息
*/
public function info() {
return apc_cache_info();
}
}
//使用方法如下:
$apc = new MyApc();
$apc->set_cache('key','test by www.phpddt.com');
print_r($apc->get_cache('key'));
|
|
Copy code
|
/***************************************************** *******************************
* APC cache class, copyright (extracted from InitPHP framework)
*************************************************** **********************************/
class MyApc {
/**
* Apc cache-set cache
* Set cache key, value and cache time
* @param string $key KEY value
* @param string $value value
* @param string $time cache time
*/
public function set_cache($key, $value, $time = 0) {
if ($time == 0) $time = null; //Cache permanently in case of null
Return apc_store($key, $value, $time);;
}
|
/**
* Apc cache-get cache
* Get cache data through KEY<🎜>
* @param string $key KEY value<🎜>
*/<🎜>
public function get_cache($key) {<🎜>
Return apc_fetch($key);<🎜>
}<🎜>
<🎜>
/**<🎜>
* Apc cache - clear a cache <🎜>
* Delete a cache from memcache<🎜>
* @param string $key KEY value<🎜>
*/<🎜>
public function clear($key) {<🎜>
Return apc_delete($key);<🎜>
}<🎜>
<🎜>
/**<🎜>
* Apc cache-clear all caches<🎜>
* It is not recommended to use this function<🎜>
* @return<🎜>
*/<🎜>
public function clear_all() {<🎜>
apc_clear_cache('user'); //Clear user cache<🎜>
Return apc_clear_cache(); //Clear cache<🎜>
}<🎜>
<🎜>
/**<🎜>
* Check if APC cache exists<🎜>
* @param string $key KEY value<🎜>
*/<🎜>
public function exists($key) {<🎜>
Return apc_exists($key);<🎜>
}<🎜>
<🎜>
/**<🎜>
* Field auto-increment - used for counting<🎜>
* @param string $key KEY value<🎜>
* @param int $step New step value<🎜>
*/<🎜>
public function inc($key, $step) {<🎜>
Return apc_inc($key, (int) $step);<🎜>
}<🎜>
<🎜>
/**<🎜>
* Field decrement - used for counting<🎜>
* @param string $key KEY value<🎜>
* @param int $step New step value<🎜>
*/<🎜>
public function dec($key, $step) {<🎜>
Return apc_dec($key, (int) $step);<🎜>
}<🎜>
<🎜>
/**<🎜>
* Return APC cache information<🎜>
*/<🎜>
public function info() {<🎜>
Return apc_cache_info();<🎜>
}<🎜>
}<🎜>
<🎜>
<🎜>
//How to use it: <🎜>
$apc = new MyApc();<🎜>
$apc->set_cache('key','test by www.phpddt.com');
print_r($apc->get_cache('key'));
Summary
1. Use Spinlocks lock mechanism to achieve the best performance.
2. APC provides apc.php for monitoring and managing APC cache. Don’t forget to change the administrator name and password
3. APC creates shared memory through mmap anonymous mapping by default, and cache objects are stored in this "large" memory space. It is managed by APC itself
Manage shared memory
4. We need to adjust the values of apc.shm_size, apc.num_files_hints, and apc.user_entries_hint through statistics. Until the end
Good
5. Well, I admit that apc.stat = 0 can get better performance. You can accept whatever you want from me.
6. PHP predefined constants, you can use the apc_define_constants() function. However, according to APC developers, pecl hidef has better performance
Good, just throw define, it is inefficient.
7. Function apc_store(), for PHP variables such as system settings, the life cycle is the entire application (from the httpd daemon until the httpd daemon
process shutdown), it would be better to use APC instead of Memcached. After all, do not use the network transmission protocol tcp.
8. APC is not suitable for caching frequently changed user data through the function apc_store(), and some strange phenomena will occur.
http://www.bkjia.com/PHPjc/629903.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629903.htmlTechArticleToday I want to do a large file upload progress effect. Later, it was said on the Internet that there is php apc that can achieve it. Then Baidu looked at php apc information, PHP APC provides two caching functions, namely caching Opcode (target text...