Introduction

Relay is a PHP extension, that is a drop-in replacement for PhpRedis as well as a shared in-memory cache like APCu.

It allows PHP to interact with Redis just like PhpRedis does, however all retrieved keys are held the PHP master process’ memory, which is shared across all FPM workers.

That means if Worker #1 fetches the users:count key from Redis, Worker #2, #3, … will instantaneously retrieve that key from Relay, without having to communicate with Redis.

$redis = new Relay;
$redis->connect('127.0.0.1', 6379);

// Only retrieve key from Redis, if it isn’t in Relay’s memory
$users = $redis->get('users:count');

To prevent its cache from going stale, Relay uses server-assisted client side caching to actively invalidate its in-memory cache.

Meaning, Relay will instantaneously know when the dataset on Redis changes and act accordingly. Whether that’s because of another application changing a key, or someone flushing the database by hand.

$redis = new Relay('127.0.0.1', 6379);

// Retrieve key from Relay’s memory
$users = $redis->get('users:count'); // 42

// Relay will instantaneously delete its copy of `users:count`
shell_exec('redis-cli SET users:count 47');

// This call will retrieve the key directly from Redis
$users = $redis->get('users:count'); // 47

Furthermore, Relay offers event listeners to update long-running processes and application runtime caches.

$redis = new Relay('127.0.0.1', 6379);

App::setUserCount($redis->get('users:count'));

$relay->onInvalidated(function ($event) {
    if ($event->key === 'users:count') {
        $users = App::setUserCount(null);
    }
});

Continue to the installation instructions.