0Pricing
PHP Academy · Lesson

Memcached for Session and Object Caching

Use Memcached as a distributed in-memory store for PHP objects.

Memcached for Session and Object Caching is a free PHP Academy lesson on CoddyKit — lesson 3 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 Memcached?

Memcached is a high-performance, distributed, in-memory key-value store. It is widely used for caching database query results and session data in PHP applications.

Installing

Install the Memcached server and the PHP extension.

# Ubuntu/Debian:
$ apt install memcached php-memcached
$ service memcached start

# Verify:
$ php -m | grep memcached

Connecting to Memcached

Create a Memcached instance and add one or more servers.

<?php
$mc = new Memcached();
$mc->addServer("127.0.0.1", 11211);

// Multiple servers (distributed caching):
$mc->addServers([
    ["192.168.1.10", 11211],
    ["192.168.1.11", 11211],
]);

Basic Cache Operations

Set, get, delete, and replace cache entries.

<?php
// Set with TTL:
$mc->set("product:1", $productData, 600); // 10 min

// Get:
$product = $mc->get("product:1");
if ($mc->getResultCode() === Memcached::RES_NOTFOUND) {
    $product = fetchFromDB(1);
    $mc->set("product:1", $product, 600);
}

// Delete:
$mc->delete("product:1");

Session Storage with Memcached

Store PHP sessions in Memcached for fast, shared access across multiple web servers.

# php.ini:
session.save_handler = memcached
session.save_path    = "127.0.0.1:11211"

Laravel Memcached Integration

Configure and use Memcached as Laravel's cache driver.

# .env
CACHE_DRIVER=memcached
MEMCACHED_HOST=127.0.0.1

// Usage is identical to Redis via Cache facade:
Cache::put("key", $value, 300);
$value = Cache::get("key");

Memcached vs Redis

Memcached: simpler, multi-threaded, pure in-memory (no persistence). Redis: richer data types, optional persistence (AOF/RDB), pub/sub, cluster. For pure caching, both perform similarly. Redis is generally preferred for new projects.

Distributed Caching

Memcached natively distributes keys across multiple servers using consistent hashing. This scales horizontally as traffic grows.

Atomic CAS (Compare-And-Swap)

Memcached supports CAS operations to safely update a value only if no other client has changed it since you last read it.

<?php
$mc->get("counter", null, $cas);
$mc->cas($cas, "counter", $newValue, 60);

TTL Limits

Memcached TTL is limited to 30 days maximum. Values larger than ~1 MB are rejected. Keep cache values small and serialized efficiently.

Monitoring

Use memcached-tool 127.0.0.1:11211 stats to check hit rate, memory usage, and evictions. A low hit rate or high eviction count indicates the cache is too small.

Summary

Memcached excels at simple distributed object and session caching. In Laravel, set CACHE_DRIVER=memcached. For production with richer features, prefer Redis.

Quick Check

What is one key difference between Memcached and Redis?

Frequently asked questions

Is the “Memcached for Session and Object Caching” lesson free?

Yes — the full text of “Memcached for Session and Object Caching” 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 “Memcached for Session and Object Caching”?

Use Memcached as a distributed in-memory store for PHP objects. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Memcached for Session and Object Caching” 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

  1. Why Caching Matters in PHP
  2. Redis as a PHP Cache Backend
  3. Memcached for Session and Object Caching
  4. Cache Invalidation Patterns
← Back to PHP Academy