PHP Velho Oeste 2024

apc_compile_file

(PECL apc >= 3.0.13)

apc_compile_file ファイルをバイトコードキャッシュに保存し、すべてのフィルタをバイパスする

説明

apc_compile_file ( string $filename [, bool $atomic = TRUE ] ) : mixed

ファイルをバイトコードキャッシュに保存し、すべてのフィルタをバイパスします。

パラメータ

filename

コンパイルし、バイトコードキャッシュに保存したい PHP ファイルへの完全パスあるいは相対パス。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

参考

  • apc_bin_dumpfile() - キャッシュされたファイルやユーザー変数のバイナリダンプをファイルに出力する
  • apc_bin_loadfile() - バイナリダンプをファイルから APC のファイル/ユーザーキャッシュに読み込む
add a note add a note

User Contributed Notes 2 notes

up
9
Glen
15 years ago
There are reasons to use this routine.  I can think of two:

- a busy website will have multiple web servers, and before one is brought online (via load balancer), the cache is built.  This prevents problems of having too many cache misses in a short period on your entire code base, which could be massive, and cause problems.

- a website where the apc.stat flag is set to zero.  That setting provides significant performance savings because no 'stat' needs to be performed on PHP code files.  But they are not automatically rebuilt when changed.  So what do you do when your code changes?  Restart Apache, or manually clearing the APC cache with apc_clear_cache() - but both will clear the entire cache.  There may be cases where it is much better to recompile select files instead.  Some sites store data (that rarely changes) in PHP code to make good use of the opcode cache, updating that file and selectively compiling it would make writes efficient too.
up
6
Andrea Giammarchi
16 years ago
This is a simple way to cache a project entirely.

<?php // apc_compile_dir.php
function apc_compile_dir($root, $recursively = true){
   
$compiled   = true;
    switch(
$recursively){
        case   
true:
            foreach(
glob($root.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR) as $dir)
               
$compiled   = $compiled && apc_compile_dir($dir, $recursively);
        case   
false:
            foreach(
glob($root.DIRECTORY_SEPARATOR.'*.php') as $file)
               
$compiled   = $compiled && apc_compile_file($file);
            break;
    }
    return 
$compiled;
}
?>

This is an example on how to use the function to compile your project.
<?php
echo    '<pre>'.PHP_EOL;
if(
function_exists('apc_compile_file')){
   
define('APC_CLEAR_CACHE',           true);
   
define('APC_COMPILE_RECURSIVELY',   true);
   
define('APC_COMPILE_DIR',           '.');
    require
'apc_compile_dir.php';
    echo   
'APC Directory Compiler '.gmdate('Y-m-d H:i:s').PHP_EOL;
    echo   
PHP_EOL.'-------------------------'.PHP_EOL;
    if(
APC_CLEAR_CACHE){
        echo    (
apc_clear_cache() ? 'Cache Cleaned' : 'Cache Not Cleaned').PHP_EOL;
       
var_dump(apc_cache_info());
        echo   
PHP_EOL.'-------------------------'.PHP_EOL;
    }
    echo   
'Runtime Errors'.PHP_EOL;
    echo    (
apc_compile_dir(APC_COMPILE_DIR, APC_COMPILE_RECURSIVELY) ? 'Cache Created' : 'Cache Not Created').PHP_EOL;
    echo   
PHP_EOL.'-------------------------'.PHP_EOL;
   
var_dump(apc_cache_info());
}
else
    echo   
'APC is not present, nothing to do.'.PHP_EOL;
echo   
'</pre>';
?>
To Top