Skip to content
EuroVPSEuroVPS

Understanding PHP Caching: OPcache, Memcached and Redis

PHP runs a very large share of dynamic websites, which makes it important for PHP developers to be up to speed with caching: OPcache, data caches and page caches.

By Thomas

Updated 4 min read
41 understanding php caching hero

What is PHP?

PHP is a programming language used to create dynamic content on a web page, like show a list of data records from a database, retrieve advertising, or show a news feed or Twitter stream. A way to speed up all of that is to use PHP caching.

What is Caching?

Web pages load faster by storing parts or all of the page in memory, in a file on the web server, or with the content distribution network (CDN) instead of creating it each time someone retrieves that URL. That is called caching. That provides for a better user experience as users do not have to wait as long when they click for the next web page to load.

You can cache content, like images, HTML, and data. For example, it’s not necessary to retrieve the same image 10 times for 10 different people. Instead it is better to cache that. The cache stores that web page with all the data already in place, like a list of names. 

The Programmer’s Cookbook

Consider a database call. It does not make sense to read the database 100 times for 100 people all loading the same data records, such as something that is common to many people, like the price of an item that you are promoting up front. Instead, it is more efficient to fetch that value from cache.

Applications can also cache API responses. For example, an API is how in-page advertising works. But that is a counter example, as you would not cache an ad since the advertiser shows different ads to different people.

So caching is not applicable to all situations. In particular, it is not suitable for data that changes frequently or is unique to each user.

PHP has one important cache built in: OPcache, which ships with PHP. Every time a PHP script runs, PHP first compiles it to bytecode; OPcache keeps that compiled bytecode in shared memory, so scripts are not recompiled on every request. Make sure it is enabled and that its memory is large enough for your code base. It is usually the cheapest PHP speed-up available.

PHP has no built-in cache for your data, though. For that there are open source frameworks and extensions, such as Memcached and Redis clients, that let the programmer design their code to work this way. A client library checks the cache before querying the database.

PHP Caching and WordPress

WordPress has an object cache API that can be backed by Memcached or Redis, and page-cache plugins such as W3 Total Cache or WP Super Cache add full-page caching. That will help speed up your content management publishing as well. And any custom WordPress code you write can use caching if you add a framework.

Tuning the Cache

A fair objection: how can a cache know the last five headlines without checking? The answer is it does not.

Caching is controlled by size and time. So you could set the cache time to 5 seconds or 5 minutes.

When the cache time expires the PHP code makes the round trip to the database server or API to check to see if anything is changed.

Caches are also limited by size: when a cache is full, the least recently used entries are evicted to make room for new ones.

Final Takeaway

PHP runs a very large share of dynamic websites. 

OPcache, a data cache and a page cache are the three layers worth getting right. If you have questions, contact our team.

From the EuroVPS blog archive, first published 10 August 2021.

All field notes