This Laravel package allows you to work with RoadRunner KV Cache in Laravel (as a cache driver).
Please note, that swoole
in-memory or table cache works only inside HTTP workers.
If you want a cache that works from all parts of PHP (HTTP or CLI) - our package and RoadRunner KV will help you.
You can install the package via composer:
composer require asanikovich/laravel-roadrunner-cache
Make sure you have in your RoadRunner config file (.rr.yaml
) next sections:
RPC
sectionKV
section
Full example of RoadRunner configuration file:
version: '3'
rpc:
listen: 'tcp://127.0.0.1:6001'
server:
command: php /var/www/html/vendor/bin/roadrunner-worker
http:
address: '127.0.0.1:8080'
pool:
num_workers: 1
kv:
memory:
driver: memory
config:
interval: 1
boltdb:
driver: boltdb
config:
interval: 1
Publish the config file and setup RPC connection:
php artisan vendor:publish --tag="laravel-roadrunner-cache-config"
Package supports next serializers (should be configured in store
config):
null
- default phpserializer
igbinary
- igbinary serializer,ext-igbinary
is required to be installed
Package supports encryption with sodium
, requirements:
ext-sodium
required to be installedencryption_key
filled instore
config (generated bysodium_crypto_box_keypair()
)
Add to cache configuration file (/config/cache.php
) new store with driver roadrunner
:
<?php
return [
'default' => 'rr-memory', // Default store (optional)
'stores' => [
'rr-memory' => [ // Custom store name with "memory" connection
'driver' => 'roadrunner',
'connection' => 'memory', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
'serializer' => null, // Available options: null|igbinary
'encryption_key' => null, // Available options: null|string
],
'rr-boltdb' => [ // Custom store name with "boltdb" connection (another store is optional)
'driver' => 'roadrunner',
'connection' => 'boltdb', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
'serializer' => null, // Available options: null|igbinary
'encryption_key' => null, // Available options: null|string
'prefix' => 'custom', // Custom prefix - non-empty-string
],
'rr-memory-igbinary' => [ // Custom store name with "memory" connection and "igbinary" serializer
'driver' => 'roadrunner',
'connection' => 'memory', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
'serializer' => 'igbinary', // Available options: null|igbinary
'encryption_key' => null, // Available options: null|string
],
'rr-memory-igbinary-encrypted' => [ // Custom store name with "memory" connection and encrypted "igbinary" serializer
'driver' => 'roadrunner',
'connection' => 'memory', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
'serializer' => 'igbinary', // Available options: null|igbinary
'encryption_key' => 'key1', // Available options: null|string
],
'rr-memory-encrypted' => [ // Custom store name with "memory" connection and encrypted serializer
'driver' => 'roadrunner',
'connection' => 'memory', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
'serializer' => null, // Available options: null|igbinary
'encryption_key' => 'key2', // Available options: null|string
],
],
]
To use in your code:
<?php
use Illuminate\Support\Facades\Cache;
Cache::driver()->get('key'); // Default main store - rr-memory
Cache::driver('rr-boltdb')->get('key'); // rr-boltdb store
All done! 🚀
Here are some useful commands for development
Before running tests run docker-compose:
docker-compose up -d
Run tests:
composer run test
Run tests with coverage:
composer run test-coverage
Perform type checking:
composer run phpstan
Format your code:
composer run format
For details on updates and changes, please refer to our CHANGELOG.
Laravel RoadRunner Cache is released under The MIT License (MIT). For more information, please see our License File.