• Home
  • blog
  • Caching Hat-trick: Zend Opcache, Etags and Query Caching

Caching Hat-trick: Zend Opcache, Etags and Query Caching

In this article, we will be looking at some of the common caching techniques in PHP: Opcache, Expires Headers and Query Caching in MySQL. We’ll look at additional approaches in part 2.

Abstract image with cache text

PHP’s Request Lifecycle

Before we move on, it’s important to understand PHP’s request lifecycle. What happens behind the scenes when you access a PHP file from the browser?

  1. A file is fetched from the file system – it doesn’t really matter if it has changed since the last request.
  2. Lexical analysis – the human-readable code is converted into something (tokens) that the parser can understand.
  3. Parsing – the machine-readable strings are analyzed for potential errors. This is like grammar-checking.
  4. Opcode Creation – the tokens that we got from step 2 are converted into machine executable code.
  5. Machine code execution – machine code is processed and then executed.

For a more in-depth explanation, check out this article on how PHP echos a Hello World.

All of those steps happen really quickly, and yet there is a lot of time and resources wasted on every request. That is because for every request for each PHP file on the server, the server has to go through all of them.

The caching techniques that we will be going through in these two articles help bypass steps 2, 3 and 4. That means less time and fewer resources wasted, leading to faster page load times for the user.

Continue reading %Caching Hat-trick: Zend Opcache, Etags and Query Caching%