PHPackages                             drift/react-key-value - 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. drift/react-key-value

ActiveLibrary[Caching](/categories/caching)

drift/react-key-value
=====================

Lightweight library that provides dynamic and smart local cache to your ReactPHP applications.

0.1.0(5y ago)24.3k1MITPHPPHP ^7.4 || ^8.0

Since Apr 29Pushed 5y ago1 watchersCompare

[ Source](https://github.com/driftphp/reactphp-key-value)[ Packagist](https://packagist.org/packages/drift/react-key-value)[ Docs](https://github.com/driftphp/reactphp-key-value)[ RSS](/packages/drift-react-key-value/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

ReactPHP key-value cache
========================

[](#reactphp-key-value-cache)

[![CircleCI](https://camo.githubusercontent.com/bc63f0b68ce2803e6137a38838b2ef00b1836e8b54ef9ea3f7fc5f076e98229b/68747470733a2f2f636972636c6563692e636f6d2f67682f64726966747068702f72656163747068702d63616368652e7376673f7374796c653d737667)](https://circleci.com/gh/driftphp/reactphp-cache)

Just a simple key-value local cache for your [ReactPHP](https://reactphp.org/)projects

Set a key
---------

[](#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

[](#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
---------

[](#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

[](#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
------------

[](#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
--------------------

[](#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);
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

1845d ago

### Community

Maintainers

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

---

Top Contributors

[![mmoreram](https://avatars.githubusercontent.com/u/521409?v=4)](https://github.com/mmoreram "mmoreram (5 commits)")

---

Tags

reactphpcacheKey value

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/drift-react-key-value/health.svg)

```
[![Health](https://phpackages.com/badges/drift-react-key-value/health.svg)](https://phpackages.com/packages/drift-react-key-value)
```

###  Alternatives

[psr/simple-cache

Common interfaces for simple caching

8.1k727.3M2.1k](/packages/psr-simple-cache)[psr/cache

Common interface for caching libraries

5.2k686.9M1.3k](/packages/psr-cache)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[clue/docker-react

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

113154.9k1](/packages/clue-docker-react)[beste/in-memory-cache

A PSR-6 In-Memory cache that can be used as a fallback implementation and/or in tests.

2512.2M6](/packages/beste-in-memory-cache)[rtcamp/nginx-helper

Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also provides cloudflare edge cache purging with Cache-Tags.

23517.0k1](/packages/rtcamp-nginx-helper)

PHPackages © 2026

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