Implementing Full Page Cache with Redis in PHP
This article explains how to improve the performance of rarely‑changed web pages such as user agreements by storing their rendered HTML in Redis as a full‑page cache, describing the design of helper functions, the RedisFPC class implementation, usage examples, testing results, and practical recommendations.
The author encountered a performance problem where pages like registration agreements were stored in a database and fetched on every request, causing noticeable latency because the content changes only rarely.
To solve this, a full‑page cache (FPC) based on Redis is introduced. The idea is to cache the entire rendered HTML of a page in Redis after the first request and serve it directly on subsequent accesses, optionally refreshing it when the source changes.
The core helper functions are:
/**
* Get the content of a URL.
* @param string $url The URL to fetch.
* @return boolean|string
*/
public function getUrlText($url)
{
if (empty($url)) {
return false;
}
return file_get_contents($url);
}The remember function checks if a cache entry exists; if not, it obtains the content either by calling a callback or by fetching the URL, stores it in Redis, and returns the content.
/**
* Store or retrieve a cache entry.
* @param string $cacheName Cache key.
* @param string|callable $urlOrCallback URL or callback to obtain data.
* @param null|int $ttl Time‑to‑live.
* @return boolean|string
*/
public function remember($cacheName, $urlOrCallback, $ttl = null)
{
$value = $this->get($cacheName);
if (!$value) {
if (is_callable($urlOrCallback)) {
$text = $urlOrCallback();
} else {
$text = $this->getUrlText($urlOrCallback);
}
if (empty($text)) {
throw new \Exception('can not get value:' . $urlOrCallback);
}
$this->put($cacheName, $text, $ttl);
return $text;
} else {
return $value;
}
}The refresh function deletes an existing cache entry and then calls remember to rebuild it.
/**
* Refresh a cache entry.
* @param string $cacheName Cache key.
* @param string|callable $urlOrCallback URL or callback.
* @param null|int $ttl Time‑to‑live.
* @return boolean|string
*/
public function refresh($cacheName, $urlOrCallback, $ttl = null)
{
$this->delete($cacheName);
return $this->remember($cacheName, $urlOrCallback, $ttl);
}The full RedisFPC class encapsulates these methods together with get, put, and delete operations that interact with a Redis instance.
<?php
namespace RedisFPC;
class RedisFPC
{
private $redis;
public function __construct($redis = [])
{
$this->redis = new \Redis();
return $this->redis->connect('127.0.0.1');
}
// ... (methods remember, get, put, delete, refresh, getUrlText, getKey as shown above) ...
private function getKey($cacheName)
{
return 'FPC:' . $cacheName;
}
}A test script demonstrates usage: it creates a RedisFPC instance, defines the URL of the page to cache (e.g., http://localhost:1002/m_about.php), and calls remember with a 24‑hour TTL.
<?php
use RedisFPC\RedisFPC;
require_once 'redisFPC.php';
$url = 'http://localhost:1002/m_about.php';
$fpc = new RedisFPC();
echo $fpc->remember('ServiceAgreement', $url, 60*60*24);Testing shows that the first request takes about 2.4 seconds (loading from the database), while the second request is much faster because the content is served from Redis.
In the final recommendations, the author advises using the remember function for initial caching, the refresh function when the source page changes, and setting an appropriate TTL to control cache expiration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
