Skip to content

driftphp/reactphp-key-value

Repository files navigation

ReactPHP key-value cache

CircleCI

Just a simple key-value local cache for your ReactPHP projects

Set a key

You can set a value given a key in this cache. As simple as it sounds.

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$cache = new LocalKeyValueCache($loop);
$cache->set('my_key', 'Any value');

Define TTL

You can add a TTL for this key. After n seconds, this key will be automatically deleted.

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$ttl = 0.1; // Means 0.1 second (100 milliseconds)
$cache = new LocalKeyValueCache($loop);
$cache->set('my_key', 'Any value', $ttl);

Get a key

You can get a value from this cache by using the key. If the value is present inside the cache, this one will be returned with no transformations. Otherwise, null will be returned.

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$cache = new LocalKeyValueCache($loop);
$cache->set('my_key', 'Any value');
$value = $cache->get('my_key');

Refresh TTL on access

TTL can offer a proper way to basically clean elements that are almost not used. By defining a value in TTL, by default this key will be deleted after n seconds, no matter how many times this key has been requested until this moment. If we want to automatically refresh this TTL each time we access to a key, we can use this feature.

In this example, we can see that the key is defined with a TTL of 2 seconds and is requested each second, enabling the refreshTTL flag. In normal circumstances, after 2 seconds the get method should return null, but because we are forcing the cache to refresh this TTL when we access the key, as long as we don't have time gaps larger than 2 seconds, our key will always be available.

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$cache = new LocalKeyValueCache($loop);
$ttl = 2; // Means 2 seconds
$cache->set('my_key', 'Any value');

// ... After 1 second
$value = $cache->get('my_key', true); // Found

// ... After 1 second
$value = $cache->get('my_key', true); // Found

// ... After 1 second
$value = $cache->get('my_key', true); // Found

// ... After 3 second
$value = $cache->get('my_key', true); // Not Found

With this strategy you will only save locally these values used most frequent, finding this way a nice equilibrium between cache efficiency and storage size.

Delete a key

You can manually delete a key. If the key is not found inside the cache, nothing will happen.

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;

$loop = Factory::create();
$cache = new LocalKeyValueCache($loop);
$cache->delete('my_key');

Using the middleware

In your applications you might want to use this cache as a simple and thin middleware layer, so you can easily enable and disable without changing your domain implementation.

Well, then you should use the KeyValueCacheMiddleware class, acting as an uncoupled piece in the middle.

So, having this original code in PHP

return $this
    ->dbConnection
    ->find('token', '123');

You could easily add a simple layer that caches during 10 minutes, updating the key freshness each time this one es requested.

use React\EventLoop\Factory;
use Drift\Cache\LocalKeyValueCache;
use Drift\Cache\KeyValueCacheMiddleware;

$loop = Factory::create();
$ttl = 600; // 10 minutes
$cache = new LocalKeyValueCache($loop);
$middleware = new KeyValueCacheMiddleware($cache);

return $middleware->getOrAsk('token_123', function() {
    return $this
        ->dbConnection
        ->find('token', '123');
}, $ttl, true);