opcache,php自身集成的缓存功能。由于我的系统是ubuntu server系统,所以在安装的时候很简单,直接apt-get install 方式安装的php,而且安装的是php5.5的版本,所以系统是自带opcache的,不需要我们再重新安装。
接下来要配置一下opcache
sudo vi /etc/php5/fpm/conf.d ; configuration for php ZendOpcache module ; priority=05 ;zend_extension=opcache.so opcache.enable=1 opcache.save_comments=1 opcache.memory_consumption=512 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 opcode.caching=1 opcache.force_restart_timeout=3600 opcache.optimization_level=0xffffffff opcache.optimization=1 opcache.use_cwd=0 opcache.revalidate_path=0 opcache.enable_file_override=0
然后修改php.ini文件,在最后添加一行zend_extension=opcache.so
重启php-fpm或者是nginx,我们可以看到在phpinfo的界面出现了zend opcache的配置信息


好了,到这里我们的opcache已经开始启用了。当然,在上面配置中,可以在php.ini中不加最后一行内容,但是在opcache的配置文件中要把注释去掉,也可以。
现在看起来还是很别扭,我们可以通过直观的图表形式展现出来



好吧,没发过附件,无法上传,好像格式不对,直接上代码吧,网上也有很多的,可以自行百度。
op.php
2,
'used_memory_percentage_high_threshold' => 80,
'used_memory_percentage_mid_threshold' => 60,
'allow_invalidate' => true
);
$validPages = array('overview', 'files', 'reset', 'invalidate');
$page = (empty($_GET['page']) || !in_array($_GET['page'], $validPages)
? 'overview'
: strtolower($_GET['page'])
);
if ($page == 'reset') {
opcache_reset();
header('Location: ?page=overview');
exit;
}
if ($page == 'invalidate') {
$file = (isset($_GET['file']) ? trim($_GET['file']) : null);
if (!$settings['allow_invalidate'] || !function_exists('opcache_invalidate') || empty($file)) {
header('Location: ?page=files&error=1');
exit;
}
$success = (int)opcache_invalidate(urldecode($file), true);
header("Location: ?page=files&success={$success}");
exit;
}
$opcache_config = opcache_get_configuration();
$opcache_status = opcache_get_status();
$opcache_funcs = get_extension_funcs('Zend OPcache');
if (!empty($opcache_status['scripts'])) {
uasort($opcache_status['scripts'], function($a, $b) {
return $a['hits'] < $b['hits'];
});
}
function memsize($size, $precision = 3, $space = false)
{
$i = 0;
$val = array(' bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
while (($size / 1024) > 1) {
$size /= 1024;
++$i;
}
return sprintf("%.{$precision}f%s%s",
$size, (($space && $i) ? ' ' : ''), $val[$i]);
}
function rc($at = null)
{
static $i = 0;
if ($at !== null) {
$i = $at;
} else {
echo (++$i % 2 ? 'even' : 'odd');
}
}
$data = array_merge(
$opcache_status['memory_usage'],
$opcache_status['opcache_statistics'],
array(
'total_memory_size' => memsize($opcache_config['directives']['opcache.memory_consumption']),
'used_memory_percentage' => round(100 * (
($opcache_status['memory_usage']['used_memory'] + $opcache_status['memory_usage']['wasted_memory'])
/ $opcache_config['directives']['opcache.memory_consumption'])),
'hit_rate_percentage' => round($opcache_status['opcache_statistics']['opcache_hit_rate']),
'wasted_percentage' => round($opcache_status['memory_usage']['current_wasted_percentage'], 2),
'used_memory_size' => memsize($opcache_status['memory_usage']['used_memory']),
'free_memory_size' => memsize($opcache_status['memory_usage']['free_memory']),
'wasted_memory_size' => memsize($opcache_status['memory_usage']['wasted_memory']),
'files_cached' => number_format($opcache_status['opcache_statistics']['num_cached_scripts']),
'hits_size' => number_format($opcache_status['opcache_statistics']['hits']),
'miss_size' => number_format($opcache_status['opcache_statistics']['misses']),
'blacklist_miss_size' => number_format($opcache_status['opcache_statistics']['blacklist_misses']),
'num_cached_keys_size' => number_format($opcache_status['opcache_statistics']['num_cached_keys']),
'max_cached_keys_size' => number_format($opcache_status['opcache_statistics']['max_cached_keys']),
)
);
$threshold = '';
if ($data['used_memory_percentage'] >= $settings['used_memory_percentage_high_threshold']) {
$threshold = ' high';
} elseif ($data['used_memory_percentage'] >= $settings['used_memory_percentage_mid_threshold']) {
$threshold = ' mid';
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'
) {
echo json_encode($data);
exit;
}
$host = (function_exists('gethostname')
? gethostname()
: (php_uname('n')
?: (empty($_SERVER['SERVER_NAME'])
? $_SERVER['HOST_NAME']
: $_SERVER['SERVER_NAME']
)
)
);
?>
Overview
%
memory usage
%
hit rate
total memory:
used memory:
free memory:
wasted memory: (%)
number of cached files:
number of hits:
number of misses:
blacklist misses:
number of cached keys:
max cached keys:
File usage
file cached
Script
Details
' . DIRECTORY_SEPARATOR;
echo join(DIRECTORY_SEPARATOR, array_slice($parts, 0, $settings['compress_path_threshold'])) . DIRECTORY_SEPARATOR;
echo '';
echo join(DIRECTORY_SEPARATOR, array_slice($parts, $settings['compress_path_threshold']));
if (count($parts) > $settings['compress_path_threshold']) {
echo DIRECTORY_SEPARATOR;
}
echo "{$base}";
} else {
echo htmlentities($s['full_path'], ENT_COMPAT, 'UTF-8');
}
?>
Force file invalidation
hits: ,
memory:
last used:
has been invalidated
ocp.php
1 || php_sapi_name()=='cli' || empty($_SERVER['REMOTE_ADDR']) ) { die; } // weak block against indirect access
$time=time();
define('CACHEPREFIX',function_exists('opcache_reset')?'opcache_':(function_exists('accelerator_reset')?'accelerator_':''));
if ( !empty($_GET['RESET']) ) {
if ( function_exists(CACHEPREFIX.'reset') ) { call_user_func(CACHEPREFIX.'reset'); }
header( 'Location: '.str_replace('?'.$_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']) );
exit;
}
if ( !empty($_GET['RECHECK']) ) {
if ( function_exists(CACHEPREFIX.'invalidate') ) {
$recheck=trim($_GET['RECHECK']); $files=call_user_func(CACHEPREFIX.'get_status');
if (!empty($files['scripts'])) {
foreach ($files['scripts'] as $file=>$value) {
if ( $recheck==='1' || strpos($file,$recheck)===0 ) call_user_func(CACHEPREFIX.'invalidate',$file);
}
}
header( 'Location: '.str_replace('?'.$_SERVER['QUERY_STRING'],'',$_SERVER['REQUEST_URI']) );
} else { echo 'Sorry, this feature requires Zend Opcache newer than April 8th 2013'; }
exit;
}
?>
OCP - Opcache Control Panel
Opcache Control Panel
Opcache not detected?'; die; }
if ( !empty($_GET['FILES']) ) { echo 'files cached
'; files_display(); echo ''; exit; }
if ( !(isset($_REQUEST['GRAPHS']) && !$_REQUEST['GRAPHS']) && CACHEPREFIX=='opcache_') { graphs_display(); if ( !empty($_REQUEST['GRAPHS']) ) { exit; } }
ob_start(); phpinfo(8); $phpinfo = ob_get_contents(); ob_end_clean(); // some info is only available via phpinfo? sadly buffering capture has to be used
if ( !preg_match( '/module\_Zend (Optimizer\+|OPcache).+?(\| [^>]+\<\/td\>\ | [0-9\,\. ]+\<\/td\>\<\/tr\>/','',$opcache[2]); }
if ( function_exists(CACHEPREFIX.'get_status') && $status=call_user_func(CACHEPREFIX.'get_status') ) {
$uptime=array();
if ( !empty($status[CACHEPREFIX.'statistics']['start_time']) ) {
$uptime['uptime']=time_since($time,$status[CACHEPREFIX.'statistics']['start_time'],1,'');
}
if ( !empty($status[CACHEPREFIX.'statistics']['last_restart_time']) ) {
$uptime['last_restart']=time_since($time,$status[CACHEPREFIX.'statistics']['last_restart_time']);
}
if (!empty($uptime)) {print_table($uptime);}
if ( !empty($status['cache_full']) ) { $status['memory_usage']['cache_full']=$status['cache_full']; }
echo 'memory'; print_table($status['memory_usage']); unset($status[CACHEPREFIX.'statistics']['start_time'],$status[CACHEPREFIX.'statistics']['last_restart_time']); echo 'statistics'; print_table($status[CACHEPREFIX.'statistics']); } if ( empty($_GET['ALL']) ) { meta_display(); exit; } if ( !empty($configuration['blacklist']) ) { echo 'blacklist'; print_table($configuration['blacklist']); } if ( !empty($opcache[3]) ) { echo 'runtime'; echo $opcache[3]; } $name='zend opcache'; $functions=get_extension_funcs($name); if (!$functions) { $name='zend optimizer+'; $functions=get_extension_funcs($name); } if ($functions) { echo 'functions'; print_table($functions); } else { $name=''; } $level=trim(CACHEPREFIX,'_').'.optimization_level'; if (isset($configuration['directives'][$level])) { echo 'optimization levels'; $levelset=strrev(base_convert($configuration['directives'][$level], 10, 2)); $levels=array( 1=>'Constants subexpressions elimination (CSE) true, false, null, etc.Optimize series of ADD_STRING / ADD_CHAR Convert CAST(IS_BOOL,x) into BOOL(x) Convert INIT_FCALL_BY_NAME + DO_FCALL_BY_NAME into DO_FCALL', 2=>'Convert constant operands to expected types Convert conditional JMP with constant operands Optimize static BRK and CONT', 3=>'Convert $a = $a + expr into $a += expr Convert $a++ into ++$a Optimize series of JMP', 4=>'PRINT and ECHO optimization (defunct)', 5=>'Block Optimization - most expensive pass Performs many different optimization patterns based on control flow graph (CFG)', 9=>'Optimize register allocation (allows re-usage of temporary variables)', 10=>'Remove NOPs' ); echo '
ini'; print_table(ini_get_all($name,true)); } foreach ($configuration as $key=>$value) { echo '',$key,''; print_table($configuration[$key]); } exit; } meta_display(); echo ' |