Content · Web Development · PHP

Caching Expensive Computations with Redis

Redis caches costly results with a time-to-live to reduce repeated computation. Cache keys must include all parameters that change the result; Redis outages must not break the core operation.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Redis caches costly results with a time-to-live to reduce repeated computation.

The examples focus on the operation itself, including the important distinction: A per-request PHP array cache does not persist between requests.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Before You Start


Read the input requirements before applying this pattern to production data.

Example 1: Basic Use


Cache expensive data with a bounded TTL; Redis is an optional PECL extension and requires a reachable server.

<?php
$cache = new Redis();
$cache->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379);
$key = "report:3";
$hit = $cache->get($key);
if ($hit === false) {
    $hit = json_encode(["generatedAt" => gmdate("c")], JSON_THROW_ON_ERROR);
    $cache->setex($key, 120, $hit);
}
echo $hit;

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 2: Checking Behavior


Cache expensive data with a bounded TTL; Redis is an optional PECL extension and requires a reachable server.

<?php
$cache = new Redis();
$cache->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379);
$key = "report:8";
$hit = $cache->get($key);
if ($hit === false) {
    $hit = json_encode(["generatedAt" => gmdate("c")], JSON_THROW_ON_ERROR);
    $cache->setex($key, 120, $hit);
}
echo $hit;

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 3: Practical Example


Cache expensive data with a bounded TTL; Redis is an optional PECL extension and requires a reachable server.

<?php
$cache = new Redis();
$cache->connect(getenv("REDIS_HOST") ?: "127.0.0.1", 6379);
$key = "report:21";
$hit = $cache->get($key);
if ($hit === false) {
    $hit = json_encode(["generatedAt" => gmdate("c")], JSON_THROW_ON_ERROR);
    $cache->setex($key, 120, $hit);
}
echo $hit;

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Important Behavior and Edge Cases


Cache keys must include all parameters that change the result; Redis outages must not break the core operation.

Comparison with Related Approaches


ApproachDifference
Caching Expensive Computations with RedisRedis caches costly results with a time-to-live to reduce repeated computation.
Related API or alternativeA per-request PHP array cache does not persist between requests.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsCache keys must include all parameters that change the result; Redis outages must not break the core operation.
Confusing it with a related APIA per-request PHP array cache does not persist between requests.

Key Takeaways


  • Redis caches costly results with a time-to-live to reduce repeated computation.
  • Cache keys must include all parameters that change the result; Redis outages must not break the core operation.
  • A per-request PHP array cache does not persist between requests.

Use Caching Expensive Computations with Redis when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.