APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”,是php内置的类memcached缓存模块。
官方主页是:http://pecl.php.net/package/APC
在 http://downloads.php.net/pierre/ 找到与php版本对应的php_apc.dll,拷贝至ext文件夹下
在php.ini中加入如下语句
extension=php_apc.dll
[apc]
apc.enabled = 1
apc.cache_by_default = on
apc.shm_segments = 1
apc.shm_size = 32
apc.ttl = 600
apc.user_ttl = 600
apc.num_files_hint = 0
apc.write_lock = On
重启apache,通过phpinfo()可以看到已经启用了apc模块
通过如下代码验证apc功能:
<?php
class apcInit {
/**
* 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 apcInit();
$apc->set_cache('test', 'hello apc');
echo $apc->get_cache('test');
?>