How to use PHP OPCache ?

The OPCache is used for improving the performance of PHP as it stores the precompiled bytecode, in result deleting the need for loading and parsing the PHP scripts upon each request.

Requirements: Packages such as Zend OPCache are required for the purposeful use. The zendOPCache package contains PHP versions 5.2, 5.3 and 5.4. This package is used with the basic fulfillment of the need for opcode caching and hence optimization. It will improve the performance of PHP by storing the precompiled bytecode in the shared memory and eliminating the need of reading the code from the disk and compiling it for future access.
For the Package download of Zend OPCache(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 PHP Versions 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 Versions 5.5.0 or later
    OPCache can only be compiled as a shared extension under this version. Firstly, you need to enable the building of default extension with –enable-opcache option to make it available.
    Afterwards, you can use the zend_extension configuration directive to lead the OP Cache extension into PHP. Use zend_extension=/full/path/to/opcache.so 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 regarding New Releases, Downloads, ChangeLog and additional information can be found here.

Advised php.ini setting:

  • Make the following changes in php.ini file for optimized 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

A full list of configuration directive supported by OPcache is also available.

OPCache Functions:

  1. opcache_compile_file() Function: This function is used to compile and cache a PHP script without executing it.

    Syntax:

    bool opcache_compile_file( $file ) 

    This function compiles a PHP script and adds that in the opcode cache without executing the file, this can be used to prime the cache after a Web server restart by pre-caching files that will be included later requests. The $file is used as a parameter. It is the path to the PHP script to be compiled. The above description returns true if the file was compiled successfully else false.

    Errors/Exceptions: For any errors (if occur), for example, in this case, an error of level E_Warning if generated, use as a @ as a prefix, as we prefix it, an error message that might be generated might get ignored.
    If any custom error Handler function with set_error_handler() is called, which in turn calls the error_reporting() which returns 0 when preceded by a @ which means, that error is ignored and the program proceeds further.
    If the track_errors feature is enabled, any error message generated by the expression will be saved in a variable namely $php_errormsg which overwrites any further errors if occur. Also, this variable can be checked early if you want to make use of it.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?php
      
    // 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];
    ?>

    chevron_right

    
    

    Note: This operator is known by the veteran PHPers as the STFU operator.

  2. opcache_get_configuration() Function: This function is used to get the configuration information about the cache.

    Syntax:


    array opcache_get_configuration( void )

    This function returns configuration data about the cache instance and also returns an array of information including the ini file.

    Errors/Exceptions: If opcache.restrict_api is in use and the current path is in violation of the rule then an E_WARNING will be raised, no status information will be returned.

  3. opcache_get_status() Function: This function is used to get the status information about the cache.

    Syntax:

    array opcache_get_status( $get_scripts = TRUE )

    This function returns the state information about the cache instance, $get_scripts is used as a parameter including script specific state information.

    Return Value: It returns an array of information, and it will optionally containing script specific state information, or FALSE on failure.

    Errors/Exceptions: If opcache.restrict_api is in use and the current path is in violation of the rule then an E_WARNING will be raised, no status information will be returned.

  4. opcache_invalidate() Function: This function is used to invalidate the cached script.

    Syntax:

    bool opcache_invalidate( $script, $force = FALSE )

    This function invalidates the particular script from the opcode cache. If force is unset or FALSE, the script will only be invalidated if the modification time of the script is new compared to the cached opcodes. The $script is used as a parameter denoting the path to the script being invalidated. The $force is used as a parameter if it set to TRUE, the script will be invalidated regardless of whether invalidation is necessary.

    Return value: TRUE if the opcode cache for the script was invalidated or if there was nothing to invalidate, or FALSE if the opcode cache is disabled.

  5. opcache_is_script_cached() Function: It will tell whether a script is cached in OPCache or not.

    Syntax:

    bool opcache_is_script_cached( $file )

    This function checks if a PHP script has been cached in OPCache. This could be used more easily to detect the “warning” of the cache for the particular script. The $file is used as a parameter describes the path to the PHP script being checked.

    Returns: It will returns TRUE if file is cached in OPCache, FALSE otherwise.

  6. opcache_reset() Function: 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 reparsed the next time they are hit.

    Return Value: It will return TRUE if the opcode cache was reset and FALSE if the opcode cache is disabled.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.