asanikovich/laravel-roadrunner-cache

RoadRunner KV cache for laravel

v1.0.1 2023-06-30 11:48 UTC

README

Latest Version on Packagist GitHub Tests Status GitHub Tests Coverage Status GitHub Code Style Status GitHub Lint Status Total Downloads Licence

This Laravel package allows you to work with RoadRunner KV Cache in Laravel (as a cache driver).

For laravel/octane users

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.

Getting Started

Installing the Package

You can install the package via composer:

composer require asanikovich/laravel-roadrunner-cache

Configuration

Configuration RoadRunner

Make sure you have in your RoadRunner config file (.rr.yaml) next sections:

  • RPC section
  • KV 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 config

Publish the config file and setup RPC connection:

php artisan vendor:publish --tag="laravel-roadrunner-cache-config"

Serializers

Package supports next serializers (should be configured in store config):

  • null - default php serializer
  • igbinary - igbinary serializer, ext-igbinary is required to be installed

Encryption

Package supports encryption with sodium, requirements:

  • ext-sodium required to be installed
  • encryption_key filled in store config (generated by sodium_crypto_box_keypair())

Setup cache config file

Add to cache configuration file (/config/cache.php) new store with driver roadrunner:

Config file example

<?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
        ],
    ],
]

Usage

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! 🚀

Development

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

Updates and Changes

For details on updates and changes, please refer to our CHANGELOG.

License

Laravel RoadRunner Cache is released under The MIT License (MIT). For more information, please see our License File.