private function compileFiles($path) { $files = $this->getFiles($path); $br = php_sapi_name() == "cli" ? "\n" : "<br />"; foreach ($files as $file) { @opcache_compile_file($file); } return $files; }
/** * @param string $pathToFile * * @return boolean */ public function compile($pathToFile) { $files = $this->_getCompiledFiles(); if (isset($files[$pathToFile])) { // work around to avoid a segmentation fault in php binary :-( return TRUE; } return @opcache_compile_file($pathToFile); }
public function compileScripts() { if (!extension_loaded('Zend OPcache')) { return; } $files = array(); foreach ($this->_refresh_rules as $rules) { $rules = (object) $rules; $files = array_merge($files, File::getFilesFromDirectory($rules->dir, $rules->extension, $rules->recursive)); } foreach ($files as $file) { if (!\opcache_is_script_cached($file)) { // questo ciclo dovrebbe farlo solo allo start del ws, dato che una volta che sono tutti cacheati, controlla lui // se ci sono modifiche da fare dopo i secondi opcache.revalidate_freq passati dalla memorizzazione alla request Logger::debug('Ho compilato lo script ' . $file . ' perchè non era compilato', RENDER_CORE_LOGNAME); opcache_compile_file($file); } } }
<?php $fileToRun = $argv[1]; opcache_reset(); opcache_compile_file($fileToRun); require $fileToRun;
$files[] = $fullPath; } } } } return $files; } echo '--------------------------<br>'; echo 'Очищаем старый кэш...'; // opcache_reset(); echo 'OK<br>'; echo '--------------------------<br>'; echo '--------------------------<br>'; echo 'Статус кэша:<br>'; var_dump(opcache_get_status()); echo '--------------------------<br>'; $foldersToLoad = array("_core", "_modules"); $coreFolder = str_replace("_bootstrap.php", "", __FILE__); echo '--------------------------<br>'; echo 'Загружаем:<br>'; foreach ($foldersToLoad as $folder) { foreach (preload($folder) as $file) { echo $file; if (@opcache_compile_file($file)) { echo '...<font color="green">OK</font><br>'; } else { echo '...<font color="red">FAILED</font><br>'; } } } echo '--------------------------<br>';
function __construct($di) { global $lgs_application_dir, $opcache_installed, $nocache; $this->di = $di; $controllerNamespace = ''; if (!empty($di->routing['module'])) { $controllerNamespace .= '\\Modules\\' . ucfirst(strtolower($di->routing['module'])); if (!empty($di->routing['submodule'])) { $controllerNamespace .= '\\Submodules\\' . ucfirst(strtolower($di->routing['submodule'])); } $this->viewPath = $lgs_application_dir . 'app' . $controllerNamespace . '/views/'; } else { $this->viewPath = $lgs_application_dir . 'app/views/'; } $controllerNamespace .= '\\Controllers'; $controllerNamespaceName = ucfirst(strtolower($di->routing['controller'])); $controllerNamespace .= '\\' . $controllerNamespaceName; $controllerNamespaceClassName = $controllerNamespace . 'Controller'; if ($opcache_installed) { $layoutId = 1; // depends on a file main layout/template now done in a dummy way $macrosPath = __DIR__ . '/../app//views/partials/macros/global_scope_profiles/profile-' . $layoutId . '.php'; if ($nocache) { require_once __DIR__ . '/../app//views/partials/macros/global_scope_profiles/profile-' . $layoutId . '.php'; } if (!opcache_is_script_cached($macrosPath)) { opcache_compile_file($macrosPath); } } if (!isset($di->view)) { $di->view = new View($di); } if (!isset($di->flow)) { $di->flow = new Flow($di); } if (!isset($di->db)) { $di->db = new Db($di); } Model::$staticdi = $di; Model::$staticdb = $di->db; $controller = new $controllerNamespaceClassName($di); $actionFunctionName = $di->routing['action'] . 'Action'; $this->templateRelativePath = $controllerNamespaceName . '/' . $di->routing['action'] . '.php'; $controller->{$actionFunctionName}(); $templatePath = $this->viewPath . $this->templateRelativePath; if (file_exists($templatePath)) { include str_replace('\\', '/', $templatePath); } }
<?php echo opcache_compile_file('D:\\git\\programmer\\PHP\\tmp.php'); print_r(opcache_get_status());
<?php if (isset($_GET['file'])) { opcache_compile_file($_GET['file']); }
<?php mb_strcut(); mb_substr(); mb_substr(); mb_substr(); opcache_compile_file(); $f = new mysqli('', '', '', '', '', '');
}]; // Filter the iterator using our custom filter iterator $filter = new CallbackFilterIterator($iterator, $filters); // Iterate over the files that match $return = 0; foreach ($filter as $file) { $realPath = $file->getRealPath(); if ($config['linter']) { /* opcache_compile_file will fatal error on parse errors, so we must lint each file first. */ $out = []; exec("php -l " . escapeshellarg($realPath), $out, $return); } if ($return == 0) { // Increment our counter $files++; // Cache the file with Zend OpCache opcache_compile_file($realPath); } else { // Increment our error counter $errors++; } } } // Get the end time $end = microtime(true); // Display results echo "Cached {$files} files in " . ($end - $start) . " seconds" . PHP_EOL; echo "Encountered {$errors} errors" . PHP_EOL;
function render_admin_manual_page() { if (isset($_POST['action']) && isset($_POST['_wpnonce']) && check_admin_referer('opcache_ctrl', '_wpnonce')) { switch ($_POST['action']) { case 'compile': if (isset($_POST['file']) && file_exists($_POST['file']) && !is_dir($_POST['file'])) { if (version_compare(PHP_VERSION, '5.5.11') < 0 or !opcache_is_script_cached($_POST['file'])) { opcache_compile_file($_POST['file']); printf('<div class="updated"><p>%s</p></div>', esc_html__('Compiled!', 'opcache')); } else { printf('<div class="error"><p>%s</p></div>', esc_html__('The script is already cached.', 'opcache')); } } else { printf('<div class="error"><p>%s</p></div>', esc_html__('No such file or directory.', 'opcache')); } break; case 'invalidate': if (isset($_POST['file']) && file_exists($_POST['file']) && !is_dir($_POST['file'])) { if (version_compare(PHP_VERSION, '5.5.11') < 0 or opcache_is_script_cached($_POST['file'])) { if (isset($_POST['force']) && $_POST['force'] == 'on') { opcache_invalidate($_POST['file'], true); printf('<div class="updated"><p>%s</p></div>', esc_html__('Force Invalidated!', 'opcache')); } else { opcache_invalidate($_POST['file']); printf('<div class="updated"><p>%s</p></div>', esc_html__('Invalidated!', 'opcache')); } } else { printf('<div class="error"><p>%s</p></div>', esc_html__('The script is not cached yet.', 'opcache')); } } else { printf('<div class="error"><p>%s</p></div>', esc_html__('No such file or directory.', 'opcache')); } break; } } $this->load_view('admin-manual.php'); }
/** * Require a file * * @access private * @param string $path */ private function require_file($path) { $path = realpath($path); if (file_exists($path)) { require_once $path; // Opcache compilation $opcache_enabled = ini_get('opcache.enable'); $opcache_cli_enabled = ini_get('opcache.enable_cli'); if (php_sapi_name() == 'cli' and $opcache_cli_enabled or php_sapi_name() != 'cli' and $opcache_enabled) { if (function_exists('opcache_is_script_cached') and function_exists('opcache_compile_file')) { if (!opcache_is_script_cached($path)) { opcache_compile_file($path); } } } return true; } else { throw new \Exception('File not found'); } }
/** * {@inheritdoc} */ protected function doSave(array $values, $lifetime) { $ok = true; $data = array($lifetime ? time() + $lifetime : PHP_INT_MAX, ''); $allowCompile = 'cli' !== PHP_SAPI || ini_get('opcache.enable_cli'); foreach ($values as $key => $value) { if (null === $value || is_object($value)) { $value = serialize($value); } elseif (is_array($value)) { $serialized = serialize($value); $unserialized = parent::unserialize($serialized); // Store arrays serialized if they contain any objects or references if ($unserialized !== $value || false !== strpos($serialized, ';R:') && preg_match('/;R:[1-9]/', $serialized)) { $value = $serialized; } } elseif (is_string($value)) { // Serialize strings if they could be confused with serialized objects or arrays if ('N;' === $value || isset($value[2]) && ':' === $value[1]) { $value = serialize($value); } } elseif (!is_scalar($value)) { throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, gettype($value))); } $data[1] = $value; $file = $this->getFile($key, true); $ok = $this->write($file, '<?php return ' . var_export($data, true) . ';') && $ok; if ($allowCompile) { @opcache_compile_file($file); } } return $ok; }
<?php ini_set('memory_limit', '-1'); $directoryIterator = new RecursiveDirectoryIterator("/application"); $iterator = new RecursiveIteratorIterator($directoryIterator); $phpFileIterator = new RegexIterator($iterator, '/^.+\\.php$/i', RecursiveRegexIterator::GET_MATCH); foreach ($phpFileIterator as $name => $cur) { echo "Warming {$name}..." . PHP_EOL; opcache_compile_file($name); }
<?php opcache_compile_file($file);
function OpCacheCtl($action, $basepath, $redis = null) { if ($action === 'prime' or $action === 'primeall') { $cmd = 'opcache_compile_file'; } if ($action === 'reset') { $cmd = 'opcache_invalidate'; } if ($action === 'prime') { $files = $redis->sMembers('php_opcache_prime'); foreach ($files as $file) { opcache_compile_file($file); } } if ($action === 'primeall' or $action === 'reset') { if (is_file($basepath)) { if (parseFileStr($basepath, '.') === 'php' && $basepath !== '/srv/http/command/cachectl.php') { $cmd($basepath); } } elseif (is_dir($basepath)) { $scan = glob(rtrim($basepath, '/') . '/*'); foreach ($scan as $index => $path) { OpCacheCtl($path, $action); } } } }
/** * Uses the Zend OPCache control functions to perform an in-process * validation of a file. This function will fail on code that declares new * symbols (e.g., classes or functions) if they have already been loaded * into the current process. * * @param string $path * * @return bool */ private function opcacheLint($path) { opcache_invalidate($path, true); return @opcache_compile_file($path); }
/** * Uses composer's classmap to prime all files. * * @return int */ public function doPopulate() { $return = 0; $files = $this->getFilesFromAutoload(); $successfulFiles = 0; foreach ($files as $file) { $this->log("Priming {$file}.", 'info'); if (@opcache_compile_file($file)) { $this->log("Success!", 'info'); $successfulFiles++; continue; } // ignore errors $this->log("Could not compile: {$file}", 'error'); } $this->log(sprintf('Succesfully primed the cache for %d of %d files', $successfulFiles, count($files))); return $return; }
function lgs_autoloader($class) { global $nocache, $opcache_installed; $class = str_replace('\\', '/', $class); $classParts = explode('/', $class); if ($classParts[0] == 'Models') { $includepath = __DIR__ . '/../app/' . $class . '.php'; } else { if ($classParts[0] == 'Controllers') { $includepath = __DIR__ . '/../app/' . $class . '.php'; } else { $includepath = __DIR__ . '/../app/Controllers/' . $class . '.php'; } } include $includepath; if ($opcache_installed) { // reset in other logically better place so not to do it twice if ($nocache) opcache_reset(); if (!opcache_is_script_cached($includepath)) { echo '<br / >some not cached'; opcache_compile_file($includepath); } else { echo '<br / >some cached'; } } }