function write_cache_file($file, $content) { // Open $handle = @fopen($file, 'r+b'); // @ - file may not exist if (!$handle) { $handle = fopen($file, 'wb'); if (!$handle) { return false; } } // Lock flock($handle, LOCK_EX); ftruncate($handle, 0); // Write if (fwrite($handle, $content) === false) { // Unlock and close flock($handle, LOCK_UN); fclose($handle); return false; } // Unlock and close flock($handle, LOCK_UN); fclose($handle); // Force opcache to recompile this script if (function_exists('opcache_invalidate')) { opcache_invalidate($file, true); } return true; }
/** * {@inheritdoc} */ public function write($key, $content) { $dir = dirname($key); if (!is_dir($dir)) { if (false === @mkdir($dir, 0777, true)) { clearstatcache(false, $dir); if (!is_dir($dir)) { throw new RuntimeException(sprintf('Unable to create the cache directory (%s).', $dir)); } } } elseif (!is_writable($dir)) { throw new RuntimeException(sprintf('Unable to write in the cache directory (%s).', $dir)); } $tmpFile = tempnam($dir, basename($key)); if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $key)) { @chmod($key, 0666 & ~umask()); if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) { // Compile cached file into bytecode cache if (function_exists('opcache_invalidate')) { opcache_invalidate($key, true); } elseif (function_exists('apc_compile_file')) { apc_compile_file($key); } } return; } throw new RuntimeException(sprintf('Failed to write cache file "%s".', $key)); }
public function flush_file($filename) { if (file_exists($filename)) { } else { if (file_exists(ABSPATH . $filename)) { $filename = ABSPATH . DIRECTORY_SEPARATOR . $filename; } elseif (file_exists(WP_CONTENT_DIR . DIRECTORY_SEPARATOR . $filename)) { $filename = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . $filename; } elseif (file_exists(WPINC . DIRECTORY_SEPARATOR . $filename)) { $filename = WPINC . DIRECTORY_SEPARATOR . $filename; } elseif (file_exists(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $filename)) { $filename = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $filename; } else { return false; } } if (function_exists('opcache_invalidate')) { return opcache_invalidate($filename, true); } else { if (function_exists('apc_compile_file')) { return apc_compile_file($filename); } } return false; }
/** * Writes all configuration to application configuration file * @return bool result, true if success */ public function commit() { $data = <<<PHP <?php /* * ! WARNING ! * * This file is auto-generated. * Please don't modify it by-hand or all your changes can be lost. */ {$this->append} return PHP; $data .= VarDumper::export($this->configuration); $data .= ";\n\n"; $result = file_put_contents($this->filename, $data, LOCK_EX) !== false; if ($result) { if (function_exists('opcache_invalidate')) { opcache_invalidate($this->filename, true); } if (function_exists('apc_delete_file')) { @apc_delete_file($this->filename); } } return $result; }
/** * Returns all supported and active opcaches * * @return array Array filled with supported and active opcaches */ public function getAllActive() { $xcVersion = phpversion('xcache'); $supportedCaches = array('OPcache' => array('active' => extension_loaded('Zend OPcache') && ini_get('opcache.enable') === '1', 'version' => phpversion('Zend OPcache'), 'canReset' => TRUE, 'canInvalidate' => function_exists('opcache_invalidate'), 'error' => FALSE, 'clearCallback' => function ($fileAbsPath) { if ($fileAbsPath !== NULL && function_exists('opcache_invalidate')) { opcache_invalidate($fileAbsPath); } else { opcache_reset(); } }), 'WinCache' => array('active' => extension_loaded('wincache') && ini_get('wincache.ocenabled') === '1', 'version' => phpversion('wincache'), 'canReset' => TRUE, 'canInvalidate' => TRUE, 'error' => FALSE, 'clearCallback' => function ($fileAbsPath) { if ($fileAbsPath !== NULL) { wincache_refresh_if_changed(array($fileAbsPath)); } else { // No argument means refreshing all. wincache_refresh_if_changed(); } }), 'XCache' => array('active' => extension_loaded('xcache'), 'version' => $xcVersion, 'canReset' => !ini_get('xcache.admin.enable_auth'), 'canInvalidate' => FALSE, 'error' => FALSE, 'clearCallback' => function ($fileAbsPath) { if (!ini_get('xcache.admin.enable_auth')) { xcache_clear_cache(XC_TYPE_PHP); } })); $activeCaches = array(); foreach ($supportedCaches as $opcodeCache => $properties) { if ($properties['active']) { $activeCaches[$opcodeCache] = $properties; } } return $activeCaches; }
function invalidate($file) { // Invalidates a specific cached script. The script will be parsed again on the next visit. // Be sure to include the full location to the file; typically you will use BASEPATH to point // to the desired file -- e.g., BASEPATH.'public/views/example/example.php' return opcache_invalidate($file, true); }
/** * Formats the output and saved it to disc. * * @param string $identifier filename * @param $contents $contents language array to save * @return bool \File::update result */ public function save($identifier, $contents) { // store the current filename $file = $this->file; // save it $return = parent::save($identifier, $contents); // existing file? saved? and do we need to flush the opcode cache? if ($file == $this->file and $return and static::$flush_needed) { if ($this->file[0] !== '/' and (!isset($this->file[1]) or $this->file[1] !== ':')) { // locate the file if ($pos = strripos($identifier, '::')) { // get the namespace path if ($file = \Autoloader::namespace_path('\\' . ucfirst(substr($identifier, 0, $pos)))) { // strip the namespace from the filename $identifier = substr($identifier, $pos + 2); // strip the classes directory as we need the module root $file = substr($file, 0, -8) . 'lang' . DS . $identifier; } else { // invalid namespace requested return false; } } else { $file = \Finder::search('lang', $identifier); } } // make sure we have a fallback $file or $file = APPPATH . 'lang' . DS . $identifier; // flush the opcode caches that are active static::$uses_opcache and opcache_invalidate($file, true); static::$uses_apc and apc_compile_file($file); } return $return; }
/** * @Request({"config": "array", "option": "array", "tab": "int"}, csrf=true) */ public function saveAction($data, $option, $tab = 0) { // TODO: validate $data['app.debug'] = @$data['app.debug'] ?: '0'; $data['profiler.enabled'] = @$data['profiler.enabled'] ?: '0'; $data['app.nocache'] = @$data['app.nocache'] ?: '0'; $data['cache.cache.storage'] = @$data['cache.cache.storage'] ?: 'auto'; $option['system:app.site_title'] = @$option['system:app.site_title'] ?: ''; $option['system:maintenance.enabled'] = @$option['system:maintenance.enabled'] ?: '0'; foreach ($data as $key => $value) { $this->config->set($key, $value); } file_put_contents($this->configFile, $this->config->dump()); foreach ($option as $key => $value) { $this['option']->set($key, $value, true); } if ($data['cache.cache.storage'] != $this['config']->get('cache.cache.storage') || $data['app.debug'] != $this['config']->get('app.debug')) { $this['system']->clearCache(); } if (function_exists('opcache_invalidate')) { opcache_invalidate($this->configFile); } $this['message']->success(__('Settings saved.')); return $this->redirect('@system/settings', compact('tab')); }
/** * Initialize the ClearCache-Callbacks * * @return void */ protected static function initialize() { self::$clearCacheCallbacks = array(); // Zend OpCache (built in by default since PHP 5.5) - http://php.net/manual/de/book.opcache.php if (extension_loaded('Zend OPcache') && ini_get('opcache.enable') === '1') { self::$clearCacheCallbacks[] = function ($absolutePathAndFilename) { if ($absolutePathAndFilename !== null && function_exists('opcache_invalidate')) { opcache_invalidate($absolutePathAndFilename); } else { opcache_reset(); } }; } // WinCache - http://www.php.net/manual/de/book.wincache.php if (extension_loaded('wincache') && ini_get('wincache.ocenabled') === '1') { self::$clearCacheCallbacks[] = function ($absolutePathAndFilename) { if ($absolutePathAndFilename !== null) { wincache_refresh_if_changed(array($absolutePathAndFilename)); } else { // Refresh everything! wincache_refresh_if_changed(); } }; } // XCache - http://xcache.lighttpd.net/ // Supported in version >= 3.0.1 if (extension_loaded('xcache')) { self::$clearCacheCallbacks[] = function ($absolutePathAndFilename) { // XCache can only be fully cleared. if (!ini_get('xcache.admin.enable_auth')) { xcache_clear_cache(XC_TYPE_PHP); } }; } }
public function setParams($params) { file_put_contents($file = $this->getParamsFile(), '<?php return ' . var_export($params, true) . ';'); if (function_exists('opcache_invalidate')) { opcache_invalidate($file); } }
/** * @return void */ private function loadFile($class, $generator) { $file = "{$this->tempDirectory}/{$class}.php"; if (!$this->isExpired($file) && @(include $file) !== FALSE) { // @ file may not exist return; } if (!is_dir($this->tempDirectory)) { @mkdir($this->tempDirectory); // @ - directory may already exist } $handle = fopen("{$file}.lock", 'c+'); if (!$handle || !flock($handle, LOCK_EX)) { throw new Nette\IOException("Unable to acquire exclusive lock on '{$file}.lock'."); } if (!is_file($file) || $this->isExpired($file)) { list($toWrite[$file], $toWrite["{$file}.meta"]) = $this->generate($class, $generator); foreach ($toWrite as $name => $content) { if (file_put_contents("{$name}.tmp", $content) !== strlen($content) || !rename("{$name}.tmp", $name)) { @unlink("{$name}.tmp"); // @ - file may not exist throw new Nette\IOException("Unable to create file '{$name}'."); } elseif (function_exists('opcache_invalidate')) { @opcache_invalidate($name, TRUE); // @ can be restricted } } } if (@(include $file) === FALSE) { // @ - error escalated to exception throw new Nette\IOException("Unable to include '{$file}'."); } flock($handle, LOCK_UN); }
/** * Identical than file_put_contents, but must be used instead for PHP files in order to invalidate * PHP caching * * @param $filename string * @param $data string */ function script_put_contents($filename, $data) { if (file_put_contents($filename, $data)) { if (function_exists('opcache_invalidate') && substr($filename, -4) == '.php') { opcache_invalidate($filename, true); } } }
/** * Invalidates a PHP file from a possibly active opcode cache. * * In case the opcode cache does not support to invalidate an individual file, * the entire cache will be flushed. * * @param string $pathname * The absolute pathname of the PHP file to invalidate. */ public static function invalidate($pathname) { clearstatcache(TRUE, $pathname); // Check if the Zend OPcache is enabled and if so invalidate the file. if (function_exists('opcache_invalidate')) { opcache_invalidate($pathname, TRUE); } }
public static function invalidateScript($path) { if (extension_loaded('Zend OPcache')) { opcache_invalidate($path); } elseif (extension_loaded('apc')) { apc_delete_file($path); } }
function __cms_config($key, $value = NULL, $delete = FALSE, $file_name, $config_load_alias) { if (!file_exists($file_name)) { return FALSE; } $pattern = array(); $pattern[] = '/(\\$config\\[(\'|")' . $key . '(\'|")\\] *= *")(.*?)(";)/si'; $pattern[] = "/(" . '\\$' . "config\\[('|\")" . $key . "('|\")\\] *= *')(.*?)(';)/si"; if ($delete) { $replacement = ''; $str = file_get_contents($file_name); $str = preg_replace($pattern, $replacement, $str); @chmod($file_name, 0777); if (strpos($str, '<?php') !== FALSE && strpos($str, '$config') !== FALSE) { @file_put_contents($file_name, $str); @chmod($file_name, 0555); } return FALSE; } else { if ($value === NULL) { // enforce refresh if (function_exists('opcache_invalidate')) { opcache_invalidate($file_name); } include $file_name; if (!isset($config)) { $config = array(); } if (key_exists($key, $config)) { $value = $config[$key]; } else { $value = ''; } return $value; } else { $str = file_get_contents($file_name); $replacement = '${1}' . addslashes($value) . '${5}'; $found = FALSE; foreach ($pattern as $single_pattern) { if (preg_match($single_pattern, $str)) { $found = TRUE; break; } } if (!$found) { $str .= PHP_EOL . '$config[\'' . $key . '\'] = \'' . addslashes($value) . '\';'; } else { $str = preg_replace($pattern, $replacement, $str); } @chmod($file_name, 0777); if (strpos($str, '<?php') !== FALSE && strpos($str, '$config') !== FALSE) { @file_put_contents($file_name, $str, LOCK_EX); @chmod($file_name, 0555); } return $value; } } }
/** * {@inheritDoc} */ protected function doSetAll(array $values) { file_put_contents($this->path, '<?php return ' . var_export($values, true) . ';'); if (function_exists('opcache_invalidate')) { opcache_invalidate($this->path); } $this->values = $values; return $this; }
function save() { $this->_cacheObj->save(@GLZ_CHARSET != 'UTF-8' ? utf8_decode($this->output) : $this->output, NULL, org_glizy_Paths::get('CACHE') . get_class($this)); $fileName = $this->_cacheObj->getFileName(); if (function_exists('opcache_invalidate')) { opcache_invalidate($fileName, true); } return $fileName; }
/** * Invalidates precompiled script cache (such as OPCache or APC) for the given file. * @param string $fileName file name. * @since 1.0.2 */ protected function invalidateScriptCache($fileName) { if (function_exists('opcache_invalidate')) { opcache_invalidate($fileName, true); } if (function_exists('apc_delete_file')) { @apc_delete_file($fileName); } }
/** * {@inheritdoc} */ public function write($key, $content) { $this->filesystem->put($key, $content); // Compile cached file into bytecode cache if (function_exists('opcache_invalidate')) { opcache_invalidate($key, true); } elseif (function_exists('apc_compile_file')) { apc_compile_file($key); } }
/** * Sauve le tableau $array dans le fichier $filename **/ protected function writeArray($array) { if (file_put_contents($this->filename, "<?php\n return " . var_export($array, true) . ';', LOCK_EX) === false) { throw new Minz_PermissionDeniedException($this->filename); } if (function_exists('opcache_invalidate')) { opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include } return true; }
public function invalidate() { try { $script = $this->input->get('script', '', 'raw'); opcache_invalidate($script, true); \Dsc\System::addMessage('Invalidated ' . $script, 'success'); } catch (\Exception $e) { \Dsc\System::addMessage($e->getMessage(), 'error'); } $this->app->reroute('/admin/cache/opcache'); }
/** * Saves configuration file and invalidates opcache. * * @param mixed $data Optional data to be saved, usually array. * @throws \RuntimeException */ public function save($data = null) { parent::save($data); // Invalidate configuration file from the opcache. if (function_exists('opcache_invalidate')) { // PHP 5.5.5+ @opcache_invalidate($this->filename); } elseif (function_exists('apc_invalidate')) { // APC @apc_invalidate($this->filename); } }
/** * Removes files from the opcache. This assumes that the files still * exist on the instance in a previous checkout. * * @return int */ public function doClean() { $status = opcache_get_status(true); $filter = new StaleFiles($status['scripts'], $this->path); $files = $filter->filter(); foreach ($files as $file) { $status = opcache_invalidate($file['full_path'], true); if (false === $status) { $this->log(sprintf('Could not validate "%s".', $file['full_path']), 'error'); } } return 0; }
public function save() { @rename($this->filename, $this->filename . '.bak.php'); if (file_put_contents($this->filename, "<?php\n return " . var_export($this->data, true) . ';', LOCK_EX) === false) { throw new Minz_PermissionDeniedException($this->filename); } if (function_exists('opcache_invalidate')) { opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include } invalidateHttpCache(); return true; }
public static function update() { $files = self::getFilesToDeleteIfOld(); foreach ($files as $file) { $path = PIWIK_INCLUDE_PATH . $file; if (file_exists($path)) { if (function_exists('opcache_invalidate')) { @opcache_invalidate($file, $force = true); } self::deleteIfLastModifiedBefore14August2014($path); } } }
protected static function saveExtensions($vendorDir, array $extensions) { $file = $vendorDir . self::EXTENSION_FILE; if (!file_exists(dirname($file))) { mkdir(dirname($file), 0777, true); } $array = str_replace("'<vendor-dir>", '$vendorDir . \'', var_export($extensions, true)); // $array = var_export($extensions, true); file_put_contents($file, "<?php\n\n\$vendorDir = dirname(__DIR__);\n\nreturn {$array};\n"); // invalidate opcache of extensions.php if exists if (function_exists('opcache_invalidate')) { opcache_invalidate($file, true); } }
/** * Sets a new dynamic configuration * * @param array $config */ public static function save($config) { $content = "<" . "?php return "; $content .= var_export($config, true); $content .= "; ?" . ">"; $configFile = self::getConfigFilePath(); file_put_contents($configFile, $content); if (function_exists('opcache_invalidate')) { opcache_reset(); opcache_invalidate($configFile); } if (function_exists('apc_compile_file')) { apc_compile_file($configFile); } }
public function resetCache($file = null) { $success = false; if ($file === null) { $success = opcache_reset(); } else { if (function_exists('opcache_invalidate')) { $success = opcache_invalidate(urldecode($file), true); } } if ($success) { $this->compileState(); } return $success; }
/** * @Request({"config": "array", "options": "array"}, csrf=true) */ public function saveAction($values = [], $options = []) { $config = new Config(); $config->merge(include $file = App::get('config.file')); foreach ($values as $module => $value) { $config->set($module, $value); } file_put_contents($file, $config->dump()); foreach ($options as $module => $value) { $this->configAction($module, $value); } if (function_exists('opcache_invalidate')) { opcache_invalidate($file); } return ['message' => 'success']; }
/** * Builds the pages cache * * @param string $language The language to build the cache for. */ public function buildCache($language) { list($keys, $navigation) = $this->getData($language); $fs = new Filesystem(); $cachePath = FRONTEND_CACHE_PATH . '/Navigation/'; $keysFile = $cachePath . 'keys_' . $language . '.php'; $fs->dumpFile($keysFile, $this->dumpKeys($keys, $language)); $phpFile = $cachePath . 'navigation_' . $language . '.php'; $fs->dumpFile($phpFile, $this->dumpNavigation($navigation, $language)); $fs->dumpFile($cachePath . 'editor_link_list_' . $language . '.js', $this->dumpEditorLinkList($navigation, $keys, $language)); // clear the php5.5+ opcode cache if (function_exists('opcache_invalidate')) { opcache_invalidate($keysFile); opcache_invalidate($phpFile); } }