Backend Development 7 min read

Mastering PHP Page Caching: From Full Static Pages to Opcode and Memcached

This article explains various PHP caching techniques—including full‑page static generation, partial page fragments, data and query caching, time‑based and content‑change strategies, as well as memory caches like Memcached, Apache cache settings, APC extension, and opcode caching—providing code examples and practical guidance to improve web performance.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Mastering PHP Page Caching: From Full Static Pages to Opcode and Memcached

Full Page Static Cache

All pages are generated as static HTML files, so user requests are served directly without invoking the PHP interpreter, a method commonly used in CMS systems such as DedeCMS. It can be implemented by capturing output with ob_start() , retrieving the buffer with ob_get_contents() , writing the content to an HTML file, and then cleaning the buffer with ob_end_clean() .

Partial Page Cache

Static cache is applied only to the infrequently changing parts of a page, while dynamic sections remain uncached and are assembled at runtime. This can be achieved using ob_get_contents() or fragment caching techniques like ESI for e‑commerce product pages.

Data Cache

Data such as product information retrieved by an ID is cached in a PHP file (often as an array) with a filename that includes the ID, allowing subsequent requests to read the data directly without querying the database.

Query Cache

Results of a specific SQL query are stored in a file; identical future queries read the cached file instead of hitting the database, using the query string to generate a unique cache filename.

Time‑Based Cache

Cache files are assigned an expiration time; within this period the cached content is served, and after expiration the data is refreshed from the database and the cache file is regenerated (e.g., homepage updated every two hours).

Content‑Change Cache

When underlying database content changes, the corresponding cache file is immediately updated, ensuring that static pages reflect the latest data without stale information.

Memory Cache (Memcached)

Memcached stores key‑value pairs in RAM to reduce database load. Example configuration and usage:

$memcachehost = '192.168.6.191';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die('Could not connect');
$memcache->set('key','cached content');
$value = $memcache->get('key');

Apache Cache

Apache does not cache by default; enabling mod_cache (with --enable-cache , --enable-disk-cache , --enable-mem-cache ) in the build configuration and configuring httpd.conf allows web acceleration.

PHP APC Cache Extension

APC (php_apc.dll on Windows) is loaded via extension=php_apc.dll in php.ini . Common settings include apc.rfc1867=on , upload and post size limits, and max_execution_time adjustments.

Opcode Cache

PHP source is compiled into opcode; caching the opcode (e.g., with XCache, Turck MMCache, PHP Accelerator) allows subsequent requests to skip parsing and compilation, executing directly for faster performance.

backendperformancecachingPHPApacheMemcachedopcode
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.