From e67ee3c9773523de2b7c0ce281c0456e1afc76b3 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Tue, 5 May 2026 22:27:39 +0300 Subject: [PATCH 1/2] Document RoadRunner KV cache --- .../tutorial/using-yii-with-roadrunner.md | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/guide/tutorial/using-yii-with-roadrunner.md b/src/guide/tutorial/using-yii-with-roadrunner.md index 5f86dea6..7af3f00f 100644 --- a/src/guide/tutorial/using-yii-with-roadrunner.md +++ b/src/guide/tutorial/using-yii-with-roadrunner.md @@ -61,6 +61,80 @@ We're specifying that the entry script is `worker.php`, the server listens on po files are served statically except `.php` and `.htaccess`. The `max_worker_memory` is a soft limit: if a worker exceeds 192 MB, it will restart after finishing its current request. Also, we're sending an additional header. +## KV cache + +RoadRunner has a [KV plugin](https://docs.roadrunner.dev/docs/key-value/overview-kv) that can be used as a PSR-16 +cache storage. It is useful when the application already runs under RoadRunner and needs a cache shared by workers. + +Install the PHP bridge: + +``` +composer require spiral/roadrunner-kv +``` + +Add a KV storage to `.rr.yaml`: + +```yaml +kv: + cache: + driver: memory + config: + interval: 60 +``` + +The `memory` driver stores values inside the RoadRunner process, so its data is lost when RoadRunner stops. +For persistent cache storage, use another KV driver from the RoadRunner documentation. + +Create `config/common/di/roadrunner-kv.php`: + +```php + static function (): StorageInterface { + $factory = new Factory(RPC::create('tcp://127.0.0.1:6001')); + + return $factory->select('cache'); + }, + + SimpleCacheInterface::class => StorageInterface::class, + YiiCacheInterface::class => Cache::class, +]; +``` + +Now services can use `Yiisoft\Cache\CacheInterface` the same way as with other cache backends: + +```php +use Yiisoft\Cache\CacheInterface; + +final readonly class FeedService +{ + public function __construct( + private CacheInterface $cache, + ) { + } + + public function latest(): array + { + return $this->cache->getOrSet('feed-latest', fn (): array => $this->loadLatest(), 60); + } + + private function loadLatest(): array + { + return []; + } +} +``` + Create `/worker.php`: ```php From d0654c0c6ea9b4610b378b1d83d02dcb88b7f3f2 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Wed, 6 May 2026 20:24:42 +0300 Subject: [PATCH 2/2] Fix RoadRunner KV cache install --- src/guide/tutorial/using-yii-with-roadrunner.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/guide/tutorial/using-yii-with-roadrunner.md b/src/guide/tutorial/using-yii-with-roadrunner.md index 7af3f00f..ce690200 100644 --- a/src/guide/tutorial/using-yii-with-roadrunner.md +++ b/src/guide/tutorial/using-yii-with-roadrunner.md @@ -66,10 +66,10 @@ exceeds 192 MB, it will restart after finishing its current request. Also, we're RoadRunner has a [KV plugin](https://docs.roadrunner.dev/docs/key-value/overview-kv) that can be used as a PSR-16 cache storage. It is useful when the application already runs under RoadRunner and needs a cache shared by workers. -Install the PHP bridge: +Install the PHP bridge and Yii cache package: ``` -composer require spiral/roadrunner-kv +composer require spiral/roadrunner-kv yiisoft/cache ``` Add a KV storage to `.rr.yaml`: