How do i enable php cache?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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):

    VersionRelease StateLink
    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 Extension 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.

      php

      <?php

      $my_file = @file('non existent file'

      or die("Failed opening file, error was : '$php_errormsg'" );

      $value = @$cache[$key];

      ?>

      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.


    How can I tell if PHP Opcache is enabled?

    Answer.
    Check which PHP handler is used by the website at Domains > example.com > PHP Settings > PHP support ( PHP version ...) ... .
    Check if Opcache is enabled for the domain at Domains > example.com > PHP Settings > Performance Settings > opcache.enable "on".

    Can PHP files be cached?

    The Windows Cache Extension for PHP includes a file cache that is used to store the content of the PHP script files in shared memory, which reduces the amount of file system operations performed by PHP engine. Resolve File Path Cache - PHP scripts very often include or operate with files by using relative file paths.

    What is PHP OpCode cache?

    What are OpCode Caches? OpCode Caches are a performance enhancing extension for PHP. They do this by injecting themselves into the execution life-cycle of PHP and caching the results of the compilation phase for later reuse. It is not uncommon to see a 3x performance increase just by enabling an OpCode cache.

    How does PHP store data in cache?

    The easiest is to serialize() the data and store it in your database. When you need to retrieve the database, query it from the database, unserialize() it, and use it as before. As second approach is to add memcache to your PHP installation and access your data via the memcache functions.