All Projects → seregazhuk → php-react-memcached

seregazhuk / php-react-memcached

Licence: MIT license
Asynchronous Memcached PHP Client for ReactPHP ecosystem

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-react-memcached

Reactphp Symfony
Symfony 4 Hello World example running on ReactPHP.
Stars: ✭ 24 (-11.11%)
Mutual labels:  reactphp
Symfony Async Kernel
[Deprecated] Symfony Async Kernel adpater
Stars: ✭ 111 (+311.11%)
Mutual labels:  reactphp
Http Client
[Deprecated] Event-driven, streaming HTTP client for ReactPHP.
Stars: ✭ 228 (+744.44%)
Mutual labels:  reactphp
Supervisord
Async first supervisord HTTP API Client for PHP 7
Stars: ✭ 14 (-48.15%)
Mutual labels:  reactphp
Filesystem
Evented filesystem access.
Stars: ✭ 102 (+277.78%)
Mutual labels:  reactphp
Promise
Promises/A implementation for PHP.
Stars: ✭ 2,078 (+7596.3%)
Mutual labels:  reactphp
Recoil
Asynchronous coroutines for PHP 7.
Stars: ✭ 765 (+2733.33%)
Mutual labels:  reactphp
pecan
ReactPHP Shell, based on the Symfony Console component.
Stars: ✭ 43 (+59.26%)
Mutual labels:  reactphp
Zanzara
Asynchronous PHP Telegram Bot Framework built on top of ReactPHP
Stars: ✭ 107 (+296.3%)
Mutual labels:  reactphp
Child Process
Event-driven library for executing child processes with ReactPHP.
Stars: ✭ 225 (+733.33%)
Mutual labels:  reactphp
Event Loop
ReactPHP's core reactor event loop that libraries can use for evented I/O.
Stars: ✭ 945 (+3400%)
Mutual labels:  reactphp
React Restify
A ReactPHP framework to create RESTfull api
Stars: ✭ 87 (+222.22%)
Mutual labels:  reactphp
Hhvmcraft
📦 Minecraft Beta 1.7.3 Server implemented in PHP/HHVM, powered by ReactPHP.
Stars: ✭ 180 (+566.67%)
Mutual labels:  reactphp
Reactphp Reactjs Hostname Analyzer Example
Stars: ✭ 8 (-70.37%)
Mutual labels:  reactphp
Enyim.Caching
High performance Memcached client, incorporating both synchronous and asynchronous with various transcoders (Json.NET BSON, MessagePack, Protocol Buffers) on .NET
Stars: ✭ 18 (-33.33%)
Mutual labels:  memcached-clients
Socket
Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP.
Stars: ✭ 900 (+3233.33%)
Mutual labels:  reactphp
Demo
Demo for DriftPHP
Stars: ✭ 117 (+333.33%)
Mutual labels:  reactphp
http-middleware-psr15-adapter
A PSR-15 middleware adapter for react/http
Stars: ✭ 18 (-33.33%)
Mutual labels:  reactphp
net-mqtt-client-react
Asynchronous MQTT client built on React
Stars: ✭ 45 (+66.67%)
Mutual labels:  reactphp
Php Console Spinner
Colorful highly configurable spinner for php cli applications (suitable for async apps)
Stars: ✭ 225 (+733.33%)
Mutual labels:  reactphp

ReactPHP Memcached Client

Asynchronous Memcached PHP Client for ReactPHP ecosystem.

Build Status Scrutinizer Code Quality Maintainability Test Coverage StyleCI

Table of Contents

Installation

Dependencies

Library requires PHP 5.6.0 or above.

The recommended way to install this library is via Composer. New to Composer?

See also the CHANGELOG for details about version upgrades.

composer require seregazhuk/react-memcached

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.

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

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

Get

Get value from key:

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

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

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

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

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 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

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 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

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

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

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

Return the Memcached server version:

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

Verbosity

Change the verbosity output of Memcached server:

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

Connection Closing

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()

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

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

close

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

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].