Cache Component

Build Status Code Climate

Async, Promise-based cache interface for ReactPHP.

The cache component provides a Promise-based CacheInterface and an in-memory ArrayCache implementation of that. This allows consumers to type hint against the interface and third parties to provide alternate implementations.

Table of Contents



The CacheInterface describes the main interface of this component. This allows consumers to type hint against the interface and third parties to provide alternate implementations.



This example fetches the value of the key foo and passes it to the var_dump function. You can use any of the composition provided by promises.

If the key foo does not exist, the promise will be rejected.


$cache->set('foo', 'bar');

This example eventually sets the value of the key foo to bar. If it already exists, it is overridden. No guarantees are made as to when the cache value is set. If the cache implementation has to go over the network to store it, it may take a while.



This example eventually removes the key foo from the cache. As with set, this may not happen instantly.


The ArrayCache provides an in-memory implementation of the CacheInterface.

$cache = new ArrayCache();

$cache->set('foo', 'bar');

Common usage

Fallback get

A common use case of caches is to attempt fetching a cached value and as a fallback retrieve it from the original data source if not found. Here is an example of that:

    ->then(null, 'getFooFromDb')

First an attempt is made to retrieve the value of foo. A promise rejection handler of the function getFooFromDb is registered. getFooFromDb is a function (can be any PHP callable) that will be called if the key does not exist in the cache.

getFooFromDb can handle the missing key by returning a promise for the actual value from the database (or any other data source). As a result, this chain will correctly fall back, and provide the value in both cases.

Fallback get and set

To expand on the fallback get example, often you want to set the value on the cache after fetching it from the data source.

    ->then(null, array($this, 'getAndCacheFooFromDb'))

public function getAndCacheFooFromDb()
    return $this->db
        ->then(array($this, 'cacheFooFromDb'));

public function cacheFooFromDb($foo)
    $this->cache->set('foo', $foo);

    return $foo;

By using chaining you can easily conditionally cache the value if it is fetched from the database.


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

This will install the latest supported version:

$ composer require react/cache:^0.4.2

See also the CHANGELOG for details about version upgrades.

This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 7+ and HHVM. It's highly recommended to use PHP 7+ for this project.


To run the test suite, you first need to clone this repo and then install all dependencies through Composer:

$ composer install

To run the test suite, go to the project root and run:

$ php vendor/bin/phpunit


MIT, see LICENSE file.