Change language

How do I use PHP OPCache?

Requirements:Packages such as Zend OPCache are required for targeted use. The zendOPCache package contains PHP versions 5.2 , 5.3 , and 5.4 . This package is used with a basic need for opcode caching and therefore optimization. This will improve PHP performance by keeping the precompiled bytecode in shared memory and eliminating the need to read the code from disk and compile it for future access. 
To download the Zend OPCache package (direct download links): Version Release State Link 7.0.1 beta Zend OPCacheV 7.0. 1 (83.1kB) 7.0.2 beta Zend OPCache V 7.0.2 (85.8kB) 7.0. 3 beta Zend OPCache V 7.0. 3 (92.0kB) 7.0.4 Stable Zend OPCache V 7.0.4 (94.1kB) 7.0. 5 Stable Zend OPCache V 7.0. 5 (94.8kB) Enabling OPCache extensions:
  • For versions PHP 5.2, 5.3 and 5. 4
    Due to Unavailability of a DLL (Dynamic Link Library) for PECL (PHP Extention and Application Repository) installation of the PECL extensions can be found here .
  • For PHP 5.5.0 or newer
    OPCache can only be compiled as a generic extension under that version. First, you need to enable the default build of the extension with the –enable-opcache option to make it available. 
    After that you can use the config directive zend_extension, to bring the OP Cache extension to PHP. Use zend_extension = / full / path / to / on non-Windows platforms and zend_extension = C: /path/to/php_opcache.dll on Windows.
    To use OPCache with Xdebug , you need to load OPCache before Xdebug.
  • Information on new releases, downloads, ChangeLog and more information can be found here .
Recommended php.ini setting:
  • Change the php.ini file below to optimize performance.
    opcache.memory_consumption = 128 opcache.interned_strings_buffer = 8 opcache.max_accelerated_files = 4000 opcache.revalidate_freq = 60 opcache.fast_shutdown = 1 opcache.enable_cli = 1
Complete list configuration directives  supported by OPcache is also available.OPCache functions:
  • opcache_compile_file() function:this function is used to compile and caching the PHP script without executing it. Syntax:
    bool opcache_compile_file ($file)
    This function compiles PHP script and adds that in the opcode cache without executing the file, this can be used to fill the cache after restarting the web server by pre-caching the files to be included in subsequent requests. File $is used as a parameter. This is the path to the PHP script to be compiled. The above description returns true,if the file was compiled successfully, otherwise false .Errors / Exceptions:For any errors (if they occur), for example, in this case, an error of the level E_Warning,if it is generated, use as a prefix @as a prefix, and an error message that may be generated, may be ignored,
    If any custom error handler function is called with set_error_handler(), which in turn calls error_reporting(), which returns 0 when preceded by @, which means the error is ignored and the program continues to run. 
    If track_errors is enabled, any error message generated by the expression will be stored in a variable, namely $php_errormsg, which will overwrite any further errors if they occur. Also, this variable can be checked beforehand if you want to use it.  
    // Intentional file error $my_file = @file ( ’non existent file’ ) or die ( "Falied opening file, error was:’ $php_errormsg’ " );  
    // This works for any expression, not just functions $value = @ $cache [ $key ]; ?>
    Note.This operator is known PHP veterans as an STFU statement.
  • opcache_get_configuration() Function:This function is used to get information about the cache configuration. Syntax:
    array opcache_get_configuration (void)
    This function returns configuration data about a cache instance and also returns an array of information including INI file.Errors / Exceptions:If opcache.restrict_apiis used and the current path violates the rule, then E_WARNING will be raised, no status information returned.
  • opcache_get_status() Function:This function is used to get information about the state of the cache. Syntax:
    array opcache_get_status ($get_scripts = TRUE)
    This function returns information about the state of an instance and the cache, $get_scripts is used as a parameter,including information about the state of a particular script.Returned value:returns an array of information and may contain information about the state specific script or FALSE on error.Errors / Exceptions:If opcache.restrict_api is used and the current path violates the rule, then E_WARNING will be raised and no status information will be returned.
  • function opcache_invalidate():this function is used to invalidate a cached script. Syntax:
     bool opcache_invalidate ($script, $force = FALSE)
    This function invalidates a specific script from the opcode cache. If force is not set or FALSE, the script will only be invalidated if the script modification time is new compared to the cached opcodes. $Scriptis used as an invalid script path parameter. $Forceis used as a parameter, if set to TRUE, the script will be invalidated whether or not invalidation is needed.Returned value: TRUE if the opcode cache for the script was invalidated, or if there was nothing to invalidate, or FALSE if the opcode cache was disabled.
  • opcache_is_script_cached() Function: It tells whether the script is cached in OPCache or not. Syntax:
    bool opcache_is_script_cached ($file)

    This function checks if the PHP script has been cached in OPCache. This could be used to more easily detect a cache "warning" for a particular script. File $is used as a parameter describing the path to the PHP script being checked.Returns:Returns TRUE if the file is cached in OPCache, otherwise FALSE.

  • function opcache_reset():This function resets the contents of the opcode cache. Syntax:
     bool opcache_reset (void)
    This function resets the entire opcode cache. After calling opcache_reset(), all scripts will be reloaded and re-parsed on next click.Return Value:it will return TRUE if the opcode cache was flushed and FALSE if the opcode cache operations are disabled.
  • Shop

    Best laptop for Sims 4


    Best laptop for Zoom


    Best laptop for Minecraft


    Best laptop for engineering student


    Best laptop for development


    Best laptop for Cricut Maker


    Best laptop for hacking


    Best laptop for Machine Learning


    Latest questions


    psycopg2: insert multiple rows with one query

    12 answers


    How to convert Nonetype to int or string?

    12 answers


    How to specify multiple return types using type-hints

    12 answers


    Javascript Error: IPython is not defined in JupyterLab

    12 answers


    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method