PHP OPcache Tutorial

Udemy Generic 728x90

Description

opcache is a technique that improves PHP performance. opcache stores precompiled script bytecode in shared memory so avoid loading and parsing of php script on each request.

Advantage

On each request PHP first compiles script and then execute it. Average compilation time of script is about 22.55% of total time . In opcache we save output of compilation in shared memory so on next request there is no need to load and compile the script and request is served by executing cached data. Thus by using opcache we can reduce load time of application by a huge margin.

Requirements

No external libraries are needed to build this extension.

Installation

opcache installation varies depending on underlying PHP version you are using . There are two cases .

A) – PHP 5.5 and later
B) – PHP 5.2 ,5.3 and 5.4

PHP 5.5 and later

In PHP 5.5 and its later version opcache is available by default.If you have disabled the building of default extensions with –disable-all , you must compile PHP with the –enable-opcache option for OPcache to be available.
opcache can only be compiled as a shared extension.

PHP 5.2 ,5.3 and 5.4

In PHP 5.2 , 5.3 and 5.4 opcache extention is not installed by default but opcode functionalities can be achieved by installing PECL extenstion.

Once compiled, you can use the zend_extension configuration directive to load the OPcache extension into PHP. This can be done with zend_extension=/full/path/to/opcache.so on non-Windows platforms, and zend_extension=C:\path\to\php_opcache.dll on Windows.

Recommended php.ini setting

  • 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

HOW TO USE OPCACHE

opcache provides few functions . Using these opcache functions we can implement opcache technique -

1) – opcache_compile_file
2) – opcache_get_configuration
3) – opcache_get_status
4) – opcache_invalidate
5) – opcache_reset
opcache_compile_file

This function compiles PHP script and adds it to opcode cache without executing it. This function returns TRUE if file was compiled successfully and FALSE on failure.

Syntax

boolean opcache_compile_file(string $filepath);
$filepath - path of the file to be cached 

Example

<br />
&lt;?php<br />
    $status = opcache_compile_file('/path_to_file/xyz.php');<br />
?&gt;<br />

opcache_get_configuration

This function returns configuration information about the cache instance.

Syntax

         array opcache_get_configuration(void);

Example

<br />
      &lt;?php<br />
         var_dump(opcache_get_configuration());<br />
      ?&gt;<br />

opcache_get_status

This function returns status information about cache.

Syntax

array opcache_get_status ([ boolean $get_scripts = TRUE ] );
$get_scripts - By default it is TRUE if we set it to FALSE cached script information is not included in returned array.

Example

<br />
     &lt;?php<br />
          // This  will return  php script  level and caching  information<br />
         var_dump(opcache_get_status());<br />
     ?&gt;   </p>
<p>    &lt;?php<br />
          // This  will return  only caching  information<br />
          var_dump(opcache_get_status(FALSE));<br />
     ?&gt;<br />
    

opcache_invalidate

This function invalidates a cached script.

Syntax

array opcache_invalidate(string $scriptpath [ , boolean $force = FALSE ])

$scriptpath - path of script to be invalidated
$force - if force is unset or FALSE, the script will only be invalidated if the modification time of the script is newer than
the cached opcodes. If set to TRUE, the script will be invalidated regardless of whether invalidation is necessary.

Example

<br />
&lt;?php<br />
   var_dump(opcache_invalidate(‘/path_to_file/xyz.php’);<br />
?&gt;<br />

opcache_reset

This function resets contents of the opcode cache. After calling opcache_reset(), all scripts will be reloaded and reparsed the next time they are hit.

Syntax

boolean opcache_reset ( void )

Example

<br />
&lt;?php<br />
   var_dump(opcache_reset());<br />
 ?&gt;<br />

Source

http://php.net/manual/en/book.opcache.php

Udemy Generic 728x90

Spread the word. Share this post!