Memcached.php - Plain vanilla PHP Memcached client with full support of Memcached protocol.

v1.0.1 2016-04-15 22:59 UTC



Logo of Memcached.php

Plain vanilla PHP Memcached client library with full support of Memcached ASCII protocol

Build Status Scrutinizer Scrutinizer Coverage clickalicious open-source
GitHub release SensioLabsInsight Packagist

Table of Contents


  • ~ 100% of Memcached ASCII-protocol specification covered
  • Support for storing native PHP variable types (arrays, objects ...)
  • Increment & Decrement support
  • Efficient connection sharing
  • Configurable connection close behavior
  • Clean & well documented code
  • Unit-Tested

Memcached.php covers almost 100% of the Memcached protocol specification. The code is clean, full documented and developed following the PSR coding standards (PSR-0/4, PSR-1, PSR-2). The code is unit-tested (PHPUnit) and the coverage is high. The library supports <incr> and <decr> command on stored integers (strings) and the connection handling is done like recommended in the Memcached protocol specification. Last but not least it supports seven of PHP's eight variable types - in detail four scalar types:

float (floating-point number [double])

two compound types:


and finally one special type:


So resource is the only type not supported.


  • Create a client instance and connect it (lazy) to Memcached daemon on host (on default port [11211])
  • Set key foo with value 1.00
  • Retrieve value for key foo
$client = new \Clickalicious\Memcached\Client('');

// Set a value of type float
$client->set('foo', 1.00);

// Returns 1.00 as PHP's type float!

You will find a demonstration Demo.php showing in detail how to use the Memcached.php client.


  • PHP >= 5.4 (compatible up to version 5.6 as well as 7.x - but not compatible with HHVM)


This client is neither tested nor designed to be used in heavy load environments. It was designed and developed by me as a client library for my phpMemAdmin project. So I was able to remove dependencies of both Memcache + Memcached (PECL) extensions - both are designed in a way i don't like. I've tried to align 100% with the Memcached protocol specification. In some cases I didn't liked the naming convention and so I created some proxies. As an example - I decided to implement increment() as proxy to incr() and decrement() as proxy to decr(). I will add some more responsibilities in some more classes like a PSR compatible Caching proxy and a Pool/Cluster Class for management operations soon.


For a consistent versioning i decided to make use of Semantic Versioning 2.0.0 Its easy to understand, very common and known from many other software projects.


  • Target stable release 1.0.0
  • >= 90% test coverage
  • Security check through 3rd-Party (Please get in contact with me)

Throughput Graph


The recommended way to install this library is through Composer. Require the clickalicious/memcached.php package into your composer.json file:

    "require": {
        "clickalicious/memcached.php": "~0.1"

Memcached.php is also available as download from github packed as zip-file or via git clone .


Strings, Integers and Float-Values are never modified by this library in any way. Those types will be stored by Memcached's internal system - while all other types will be serialized by this client and can optionally be stored compressed (LZW/Smaz) - in one of the next releases of this library - targeting 0.4.0. I'm working on an PoC implementation of Smaz - a short string compression library ( and on a german translation of the translation table used by Smaz.


Memcached provides a 32 Bit (Version > 1.2.1) unsigned Integer field for meta data. From the Memcached protocol specification:

Note that in memcached 1.2.1 and higher, flags may be 32-bits, instead of 16, but you might want to restrict yourself to 16 bits for compatibility with older versions.

Memcached.php uses this field for its meta data. The meta data is required to mark data for serialization and stuff like this. This meta data is stored via the clients` flags field. The lower first 8 Bits (lowest Byte) are reserved by Memcached.php. The other 8 Bits (half of the 16 Bits) can be used by your app.


The best and currently only existing documentation is the inline documentation of this project. So please have a look at the source to understand how Memcached.php works internally.


Memcached.php is unit-tested and the code coverage is high. For an in-detail view have a look at this always up to date Code Coverage report.

Running the Tests
You will find a PHPUnit configuration including testsuites in directory tests/. To run those configuration execute the following command on cli:

phpunit -c tests/phpunit.xml --testdox


The unit-tests are fired against an existing and real Memcached daemon. Please be aware that you need a running Memcached deamon on the host you run the unit-tests listening on the default port (11211). In Result the unit-tests are not that isolated cause they are bound to a running Memcached daemon and network as well.


For a consistent versioning i decided to make use of Semantic Versioning 2.0.0 Its easy to understand, very common and known from many other software projects.


  • Hardening code - more stability!
  • \Clickalicious\Memcached\Proxy
    This should become a proxy implementation which is able to act as Memcache or Memcached (both PECL) extension (emulate) for testing (primary mocking/stubbing).
  • \Clickalicious\Memcached\Server
    This should become a virtual (emulated) mode which emulates a complete Memcached backend.
  • Add compression support to be able to manipulate PECL Memcached stored data (FastLZ, zlib, LZW)
  • Replace explodes and array operations for data anlysis with regular expressions.
  • Increase coverage and cover (currently unused compression classes) more parts.

If you are interested in any of these features too - please let me know. Maybe we can adjust the priority and speed things up ...

Participate & share

... yeah. If you're a code monkey too - maybe we can build a force ;) If you would like to participate in either Code, Comments, Documentation, Wiki, Bug-Reports, Unit-Tests, Bug-Fixes, Feedback and/or Critic then please let me know as well! 687474703a2f2f6a70696c6c6f72612e636f6d2f6769746875622d747769747465722d627574746f6e2f696d672f74776565742e706e67


Thanks to our sponsors and supporters:

JetBrains Navicat
68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f6173736574732f6d656469612f6f70656e2d67726170682f6a6574627261696e735f323530783235302e706e67 687474703a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f656e2f392f39302f5072656d69756d536f66745f4e6176696361745f5072656d69756d5f4c6f676f2e706e67

Icons made by Freepik licensed by CC 3.0 BY