Redis as a PHP Cache Backend
Connect PHP to Redis and cache query results and computed data.
Redis as a PHP Cache Backend is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Redis?
Redis is an in-memory data structure server. It is the most popular caching backend for PHP applications due to its speed, rich data types, and built-in TTL support.
Installing the PHP Redis Extension
Install either the phpredis C extension (fastest) or the pure-PHP predis library.
# Predis via Composer:
$ composer require predis/predis
# Or install phpredis extension:
$ pecl install redisConnecting to Redis (Predis)
Create a Predis client and connect to the Redis server.
<?php
require "vendor/autoload.php";
$redis = new \Predis\Client([
"scheme" => "tcp",
"host" => "127.0.0.1",
"port" => 6379,
]);Basic Cache Operations
Set, get, and delete cache entries.
<?php
// Set with TTL (seconds):
$redis->setex("user:42", 300, json_encode($user));
// Get:
$cached = $redis->get("user:42");
$user = $cached ? json_decode($cached, true) : fetchFromDB(42);
// Delete:
$redis->del("user:42");Cache Patterns with Redis
Implement cache-aside in PHP:
<?php
function getUser(int $id): array {
global $redis;
$key = "user:$id";
$cached = $redis->get($key);
if ($cached !== null) return json_decode($cached, true);
$user = fetchUserFromDB($id);
$redis->setex($key, 3600, json_encode($user));
return $user;
}Laravel Redis Integration
Configure in .env and use the Cache facade — no manual Predis client needed.
# .env
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
// Usage:
Cache::put("key", $value, 600);
$value = Cache::get("key");Atomic Increment/Decrement
Use Redis atomic operations for counters (API usage, rate limiting).
<?php
$hits = $redis->incr("page:home:hits");
$redis->expire("page:home:hits", 86400); // reset dailyRedis Lists for Queues
Redis lists are the backend for Laravel's Redis queue driver. Jobs are pushed to and popped from lists atomically.
Redis Pub/Sub
Redis supports publish/subscribe messaging for real-time features (broadcasting events to connected WebSocket clients).
Redis Cluster
For high availability and horizontal scaling, Redis Cluster distributes data across multiple nodes. Laravel supports cluster configuration out of the box.
Monitoring Redis
Use redis-cli monitor to watch commands in real time, or Laravel Horizon (for queue monitoring). Track memory usage with redis-cli info memory.
Summary
Redis is the preferred PHP cache backend — fast, persistent (optional), and rich with data structures. In Laravel, set CACHE_DRIVER=redis and use the Cache facade. Use Predis or phpredis directly for fine-grained control.
Quick Check
Which Redis command sets a key with an expiry in seconds?
Frequently asked questions
Is the “Redis as a PHP Cache Backend” lesson free?
Yes — the full text of “Redis as a PHP Cache Backend” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Redis as a PHP Cache Backend”?
Connect PHP to Redis and cache query results and computed data. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Redis as a PHP Cache Backend” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Caching Matters in PHP
- Redis as a PHP Cache Backend
- Memcached for Session and Object Caching
- Cache Invalidation Patterns