Home Articles Forums Gallery Marketplace Videos

Using OPcache

Zend OPcache works automatically once enabled and properly configured. You do not need to add anything to your PHP scripts, HTML pages, or application code.

OPcache operates transparently at the PHP engine level.

How it works

When PHP executes a script:

  1. PHP checks whether its compiled version is already in OPcache.
  2. If it is, PHP executes the cached opcodes.
  3. If it isn’t, PHP compiles the source and stores the resulting opcodes in shared memory.
  4. Subsequent requests reuse those compiled opcodes.

This happens automatically.

Configuration worth checking

In your PHP configuration (php.ini), these are the important settings:

opcache.enable=1
opcache.enable_cli=0

opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000

opcache.validate_timestamps=1
opcache.revalidate_freq=2

These are reasonable starting values, not mandatory settings. In particular:

  • opcache.enable: Enables OPcache for web requests.
  • opcache.memory_consumption: Memory allocated for cached opcodes, in MB.
  • opcache.interned_strings_buffer: Memory for interned strings.
  • opcache.max_accelerated_files: Maximum number of cached script entries.
  • opcache.validate_timestamps: Checks whether PHP source files have changed.
  • opcache.revalidate_freq: How frequently timestamp checks occur, in seconds.

With validate_timestamps=1 and revalidate_freq=2, changes to PHP files should generally be recognized within a couple of seconds.

If you disable timestamp validation for production performance, remember that you’ll need to reset OPcache or restart the PHP handler when deploying changed scripts.

How to verify OPcache is working

Create a temporary PHP file:

<?php
echo '<pre>';
print_r(opcache_get_status(false));
echo '</pre>';

Look for:

[opcache_enabled] => 1

You can also inspect:

  • opcache_statistics
  • num_cached_scripts
  • hits
  • misses
  • memory_usage
  • opcache_hit_rate

These tell you whether scripts are being cached and how effectively OPcache is being used.

Security note: Don’t leave a publicly accessible diagnostic script on your website. It exposes server configuration information. Delete it after testing.

Leave a Comment

Licensed under CC BY-NC 4.0

Peter Martin viewpoints are those of its owner. You may share and adapt this article for non-commercial purposes, provided proper attribution is given. Attribution should include:

Title: Using OPcache
Author: Peter Martin
Original URL: https://www.woodcentral.com/-/peter/using-opcache/
License: CC BY-NC 4.0

Site Index