-
Notifications
You must be signed in to change notification settings - Fork 0
Memcache Adapter
InitPHP\Sessions\Adapters\MemCacheAdapter stores sessions in Memcache or
Memcached, with a per-item TTL. The driver is auto-detected, preferring the
modern Memcached extension over the legacy Memcache one when both are present.
Requirements: ext-memcached (preferred) or ext-memcache.
Option keys are case-insensitive.
| Option | Type | Default | Notes |
|---|---|---|---|
host |
string | 127.0.0.1 |
|
port |
int | 11211 |
|
weight |
int | 1 |
Server weight. |
raw |
bool | false |
Enable the binary protocol (Memcached only). |
prefix |
string | '' |
Key prefix. |
ttl |
int | 86400 |
Item expiry in seconds. |
If neither extension is loaded, the constructor throws a
SessionNotSupportedAdapter. A failure to connect at first use
throws a SessionAdapterException.
use InitPHP\Sessions\Session;
use InitPHP\Sessions\Adapters\MemCacheAdapter;
$adapter = new MemCacheAdapter([
'host' => '127.0.0.1',
'port' => 11211,
'weight' => 1,
'raw' => false,
'prefix' => 'sess_',
'ttl' => 86400,
]);
Session::createImmutable($adapter)->start();| Available extension(s) | Driver used |
|---|---|
memcached only |
Memcached |
memcache only |
Memcache |
| both | Memcached (preferred) |
| neither | throws SessionNotSupportedAdapter
|
The raw (binary protocol) option only applies to the Memcached driver and is
ignored by the legacy Memcache driver.
write() stores each item with ttl seconds of expiry, so the backend evicts
stale sessions automatically — no garbage collection step is required. Memcache
is a cache, so treat sessions as ephemeral: an eviction under memory pressure
logs the user out.
-
read()returns the stored payload, or''when the key is absent (a new session). - The connection is established lazily on first read/write, and closed in the adapter's destructor.
- You already run Memcached and want fast, in-memory, throwaway sessions.
- You don't need persistence across restarts (use Redis if you want TTL and more durability/features).
initphp/sessions · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Usage
Adapters
- Adapters Overview
- File Adapter
- Redis Adapter
- PDO Adapter
- Cookie Adapter
- Memcache / Memcached Adapter
- MongoDB Adapter
- Custom Adapters
Reference
Practical Guides
Migration & Help