drift/react-key-value
Composer 安装命令:
composer require drift/react-key-value
包简介
Lightweight library that provides dynamic and smart local cache to your ReactPHP applications.
关键字:
README 文档
README
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);
drift/react-key-value 适用场景与选型建议
drift/react-key-value 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.55k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「reactphp」 「key-value」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 drift/react-key-value 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drift/react-key-value 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 drift/react-key-value 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, event-driven multicast UDP message client and server for ReactPHP.
repository php library
An easy Laravel integration for HashiCorp Vault
Laravel 5 - Repositories to the database layer
Key-value file store through fire015/flintstone package.
Key-value shared memory store through php shared memory module.
统计信息
- 总下载量: 4.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-29