PHPackages                             seregazhuk/react-memcached - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. seregazhuk/react-memcached

ActiveLibrary[Caching](/categories/caching)

seregazhuk/react-memcached
==========================

Async Memcached client implementation, built on top of ReactPHP

0.2.1(7y ago)261.5k4[5 issues](https://github.com/seregazhuk/php-react-memcached/issues)1MITPHPPHP &gt;=7.2

Since Oct 18Pushed 6y ago6 watchersCompare

[ Source](https://github.com/seregazhuk/php-react-memcached)[ Packagist](https://packagist.org/packages/seregazhuk/react-memcached)[ Docs](https://github.com/seregazhuk/php-react-memcached)[ RSS](/packages/seregazhuk-react-memcached/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (9)Dependencies (8)Versions (11)Used By (1)

 [![ReactPHP Memcached Client](logo.png)](logo.png)

Asynchronous Memcached PHP Client for [ReactPHP](http://reactphp.org/) ecosystem.

[![Build Status](https://camo.githubusercontent.com/00b87699463e6dae9caad5c51ef21202d41fba4136a7b7a65ba3b0cbb3e93038/68747470733a2f2f7472617669732d63692e6f72672f7365726567617a68756b2f7068702d72656163742d6d656d6361636865642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/seregazhuk/php-react-memcached)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5534cc24edd6fa2e3f0c0143a0195c2bd78c89c8de11afe17f00836a993bac42/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7365726567617a68756b2f7068702d72656163742d6d656d6361636865642f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/seregazhuk/php-react-memcached/?branch=master)[![Maintainability](https://camo.githubusercontent.com/65ec3d71cda166141bbff2922026efc0fbdf9dce2c2c71b218b727d1af96ab29/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f31353734313533386430383531653735613137392f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/seregazhuk/php-react-memcached/maintainability)[![Test Coverage](https://camo.githubusercontent.com/c8db9cea06430e13792ee669174c253a4f401c4ec8695dc199efceb6c2e14d57/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f31353734313533386430383531653735613137392f746573745f636f766572616765)](https://codeclimate.com/github/seregazhuk/php-react-memcached/test_coverage)[![StyleCI](https://camo.githubusercontent.com/e7b1fe3fd8d52f7089e218b8ee723cad69efbe38c832c59bca17eea7b61a0841/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130363136363432352f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/106166425)

**Table of Contents**

- [Installation](#installation)
- [Quick Start](#quick-start)
- [Server Address](#server-address)
- [Client](#client)
- [Retrieval Commands](#retrieval-commands)
    - [Get](#get)
- [Storage Commands](#storage-commands)
    - [Set](#set)
    - [Add](#add)
    - [Replace](#replace)
- [Delete Command](#delete-command)
- [Increment/Decrement Commands](#increment/decrement-commands)
    - [Increment](#increment)
    - [Decrement](#decrement)
- [Touch Command](#touch-command)
- [Statistics Command](#statistics-command)
- [Misc Commands](#misc-commands)
    - [Flush all](#flush-all)
    - [Version](#version)
    - [Verbosity](#verbosity)
- [Connection Closing](#connection-closing)
    - [End](#end())
    - [Close](#close())
- [Events Handling](#events-handling)
- [Errors Handling](#errors-handling)

Installation
------------

[](#installation)

### Dependencies

[](#dependencies)

Library requires PHP 5.6.0 or above.

The recommended way to install this library is via [Composer](https://getcomposer.org). [New to Composer?](https://getcomposer.org/doc/00-intro.md)

See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.

```
composer require seregazhuk/react-memcached

```

Quick Start
-----------

[](#quick-start)

```
require '../vendor/autoload.php';

use seregazhuk\React\Memcached\Factory;

$loop = React\EventLoop\Factory::create();
$client = Factory::createClient($loop);

$client->set('example', 'Hello world');

$client->get('example')->then(function ($data) {
    echo $data . PHP_EOL; // Hello world
});

// Close the connection when all requests are resolved
$client->end();

$loop->run();
```

See [other examples](https://github.com/seregazhuk/php-memcached-react/tree/master/examples).

Server address
--------------

[](#server-address)

When creating a client via the factory you can specify server address as a second argument:

```
$client = Factory::createClient($loop, 'localhost:11222');
```

If the address is not specified the client uses default `localhost:11211`.

Asynchronous Execution
----------------------

[](#asynchronous-execution)

For each Memcached command a client has a method. All commands are executed asynchronously. The client stored pending requests and once it receives the response from the server, it starts resolving these requests. That means that each command returns a promise. When the server executed a command and returns a response, the promise will be resolved with this response. If there was an error, the promise will be rejected.

Retrieval Commands
------------------

[](#retrieval-commands)

### Get

[](#get)

Get value from key:

```
$client
    ->get('some-key')
    ->then(function ($data) {
        echo "Retreived value: " . $data . PHP_EOL;
    });
```

Storage Commands
----------------

[](#storage-commands)

For `$flags` you can use PHP `MEMCACHE_COMPRESSED` constant to specify on-the-fly compression. If the value was not stored indicate because condition for `add` or `replace` commands wasn’t met, or the item was in a delete queue, the promise will be rejected with `FailedCommandException`.

### Set

[](#set)

Store key/value pair in Memcached:

```
$client
    ->set('some-key', 'my-data')
    ->then(function () {
        echo "Value was stored" . PHP_EOL;
    });

// advanced: with compression and expires in 30 seconds
$client
    ->set('some-key', 'my-data', MEMCACHE_COMPRESSED, 30)
    ->then(function () {
        echo "Value was stored" . PHP_EOL;
    });
```

### Add

[](#add)

Store key/value pair in Memcached, but only if the server **doesn’t** already hold data for this key:

```
$client
    ->add('name', 'test')
    ->then(function() {
        echo "The value was added" . PHP_EOL;
    });

// advanced: with compression and expires in 30 seconds
$client
    ->add('name', 'test', MEMCACHE_COMPRESSED, 30)
    ->then(function() {
        echo "The value was added" . PHP_EOL;
    });
```

### Replace

[](#replace)

Store key/value pair in Memcached, but only if the server already hold data for this key:

```
$client
    ->replace('name', 'test')
    ->then(function(){
        echo "The value was replaced" . PHP_EOL;
    });

// advanced
$client
    ->replace('name', 'test', $flags, $exptime)
    ->then(function(){
        echo "The value was replaced" . PHP_EOL;
    });
```

Delete Command
--------------

[](#delete-command)

Delete value by key from Memcached. If the key doesn't exist or has been deleted the promise rejects with `FailedCommandException`:

```
$client
    ->delete('name')
    ->then(function(){
        echo "The value was deleted" . PHP_EOL;
});
```

Increment/Decrement Commands
----------------------------

[](#incrementdecrement-commands)

### Increment

[](#increment)

Increment value associated with key in Memcached, item **must** exist, increment command will not create it. The limit of increment is the 64 bit mark. If key is not found, the promise will be rejected with `FailedCommandException`:

```
$client
    ->incr('var', 2)
    ->then(
        function($data){
            echo "New value is: " . $data . PHP_EOL;
        },
        function(FailedCommandException $e) {
            echo "Key not found" . PHP_EOL;
        });
```

### Decrement

[](#decrement)

Decrement value associated with key in Memcached, item **must** exist, decrement command will not create it If you try to decrement a value bellow 0, value will stay at 0. If key is not found, the promise will be rejected with `FailedCommandException`:

```
$client
    ->decr('var', 2)
    ->then(
        function($data){
            echo "New value is: " . $data . PHP_EOL;
        },
        function(FailedCommandException $e) {
            echo "Key not found" . PHP_EOL;
        });
```

If value not found, the promise will be rejected with `FailedCommandException`.

Touch Command
-------------

[](#touch-command)

The *touch* command is used to update the expiration time of an existing item without fetching it. If the key doesn't exist or has been deleted the promise rejects with `FailedCommandException`:

```
$client
    ->touch('var', $exp)
    ->then(
    function($data){
        echo "The value was toched". PHP_EOL;
    },
    function(FailedCommandException $e) {
        echo "Key not found" . PHP_EOL;
    });
```

Statistics Command
------------------

[](#statistics-command)

This command can return an array of various stats:

```
$client
    ->stats()
    ->then(function($result){
        print_r($result);
        /*
        Array
        (
            [pid] => 666
            [uptime] => 180660
            [time] => 1508342532
            ...
            [lru_bumps_dropped] => 0
        )
        */
    });
```

Misc Commands
-------------

[](#misc-commands)

### Flush all

[](#flush-all)

Flush the server key/value pairs (invalidating them) after an optional \[\] period:

```
$client
    ->flushAll()
    ->then(function() {
        echo "Everything was flushed" . PHP_EOL;
    });
```

### Version

[](#version)

Return the Memcached server version:

```
$client
    ->version()
    ->then(function($result) {
        echo "Memcached version: $result" . PHP_EOL; // Memcached version: 1.5.0
     });
```

### Verbosity

[](#verbosity)

Change the verbosity output of Memcached server:

```
$client
    ->verbosity(2)
    ->then(function($result) {
        echo "Verbosity was changed to 2" . PHP_EOL;
    });
```

Connection Closing
------------------

[](#connection-closing)

### End()

[](#end)

To close the connection call `end()` method on the client. The client waits till all pending requests are resolved and then closes the connection. All new requests to the client will be rejected with `ConnectionClosedException` exception.

### Close()

[](#close)

If you want to force the closing and don't want to wait for pending requests to be resolved, call `close()` method. It immediately closes the connection and rejects all pending requests with `ConnectionClosedException` exception.

Events Handling
---------------

[](#events-handling)

You can register event handlers for some client's events.

### close

[](#close-1)

When the connection to Memcached server is closed, the `close` event is emitted. You can listen to this event to catch connection failures:

```
$client->on('close', function () {
    // handle closed connection
});
```

### error

[](#error)

When an error occurs in the connection, the client emits `error` event and passes an exception with the problem description:

```
$client->on('error', function (Exception $e) {
    // handle error
});
```

For example you can handle broken connections like this:

```
$client->on('error', function (ConnectionClosedException $e) {
    // handle broken connection
});
```

Errors Handling
---------------

[](#errors-handling)

All exceptions that are thrown by the client or are used to reject the promises extend from the base `seregazhuk\React\Memcached\Exception\Exception` class.

For example, if you call an unknown Memcached command the promise will be rejected with `WrongCommandException`:

```
$client
    ->unknown()
    ->then('var_dump', function(WrongCommandException $e){
        echo $e->getMessage() . PHP_EOL; // Unknown command: unknown
});
```

When the connection is broken all pending promises will be rejected with `ConnectionClosedException` exception.

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~41 days

Recently: every ~75 days

Total

9

Last Release

2849d ago

PHP version history (2 changes)v0.1.0PHP &gt;=5.6

v0.2.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/cb0e185b75648fb1c0095795e222f210700b86ae8c575c3cd6dd28a970ae2f34?d=identicon)[seregazhuk](/maintainers/seregazhuk)

---

Top Contributors

[![seregazhuk](https://avatars.githubusercontent.com/u/9959761?v=4)](https://github.com/seregazhuk "seregazhuk (179 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (2 commits)")[![andreybolonin](https://avatars.githubusercontent.com/u/2576509?v=4)](https://github.com/andreybolonin "andreybolonin (1 commits)")

---

Tags

memcached-clientsphpreactphpasyncclientreactphpmemcached

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/seregazhuk-react-memcached/health.svg)

```
[![Health](https://phpackages.com/badges/seregazhuk-react-memcached/health.svg)](https://phpackages.com/packages/seregazhuk-react-memcached)
```

###  Alternatives

[ccxt/ccxt

A cryptocurrency trading API with more than 100 exchanges in JavaScript / TypeScript / Python / C# / PHP / Go

43.2k341.0k1](/packages/ccxt-ccxt)[rector/rector-src

Instant Upgrade and Automated Refactoring of any PHP code

136406.3k14](/packages/rector-rector-src)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78127.7M466](/packages/react-http)[clue/redis-react

Async Redis client implementation, built on top of ReactPHP.

28316.2M47](/packages/clue-redis-react)[clue/docker-react

Async, event-driven access to the Docker Engine API, built on top of ReactPHP.

113163.3k1](/packages/clue-docker-react)[react/datagram

Event-driven UDP datagram socket client and server for ReactPHP

99868.6k45](/packages/react-datagram)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
