cache / memcached-adapter
A PSR-6 cache implementation using Memcached. This implementation supports tags
Requires
- php: ^8.2
- cache/adapter-common: ^3.0
- cache/hierarchical-cache: ^2.1
- psr/cache: ^3.0
- psr/simple-cache: ^2.0 || ^3.0
Requires (Dev)
- cache/integration-tests: ^1.0@dev
- phpunit/phpunit: ^11.5
Suggests
- ext-memcached: The extension required to use this pool.
Provides
- psr/cache-implementation: 3.0
- psr/simple-cache-implementation: 2.0 || 3.0
README
This package provides PSR-6 and PSR-16 cache implementations backed by the Memcached extension. The pool supports tags and hierarchical keys.
Installation
Install the package and enable the Memcached extension:
composer require cache/memcached-adapter:^3.0
Usage
use Cache\Adapter\Memcached\MemcachedCachePool; $client = new Memcached(); $client->addServer('127.0.0.1', 11211); $pool = new MemcachedCachePool($client);
The pool enables Memcached's binary protocol by default. Pass a Memcached option map as the second constructor argument to override it or set other client options:
$pool = new MemcachedCachePool($client, [ Memcached::OPT_BINARY_PROTOCOL => false, Memcached::OPT_CONNECT_TIMEOUT => 1000, ]);
Bulk operations
The PSR-16 getMultiple(), setMultiple(), and deleteMultiple() methods use the native Memcached bulk commands. Bulk writes keep the same expiration for every value and remove old tag references after storage succeeds.
The pool treats false from Memcached's getMulti() as a backend failure. It throws CachePoolException instead of returning a batch of cache misses.
Upgrading to version 3
Version 3 stores a generation snapshot with each tagged item and a separate marker for each tag. Version 2 workers cannot safely share this format.
The constructor now accepts a Memcached option map instead of a Boolean binary-protocol flag.
Stop or drain all workers, clear Memcached, and then deploy version 3. Follow the same sequence before a rollback.
Contributing
Send pull requests to the main repository. Report issues on the GitHub issue tracker.