Skip to content

Commit 04cb618

Browse files
authored
Merge pull request #3 from asanikovich/v1.1
add serializers and encryption
2 parents 22e97ff + c1ed729 commit 04cb618

File tree

9 files changed

+181
-68
lines changed

9 files changed

+181
-68
lines changed

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/vendor
2+
.DS_Store
3+
/composer.lock
4+
/.github
5+
/.idea
6+
/.git

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
uses: shivammathur/setup-php@v2
3131
with:
3232
php-version: ${{ matrix.php }}
33-
extensions: dom, curl, libxml, mbstring, zip
33+
extensions: dom, curl, libxml, mbstring, zip, igbinary, sodium
3434
tools: composer:v2
3535
coverage: xdebug
3636

Dockerfile

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
FROM php:8.2-cli-alpine3.17 as backend
22

33
RUN --mount=type=bind,from=mlocati/php-extension-installer:1.5,source=/usr/bin/install-php-extensions,target=/usr/local/bin/install-php-extensions \
4-
install-php-extensions opcache zip xsl dom exif intl pcntl bcmath sockets && \
4+
install-php-extensions opcache zip xsl dom exif intl pcntl bcmath sockets igbinary sodium && \
55
apk del --no-cache ${PHPIZE_DEPS} ${BUILD_DEPENDS}
66

7+
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS && \
8+
apk add --update linux-headers && \
9+
pecl install xdebug && \
10+
docker-php-ext-enable xdebug && \
11+
apk del -f .build-deps
12+
713
ENV COMPOSER_ALLOW_SUPERUSER=1
814
COPY --from=composer:2.3 /usr/bin/composer /usr/bin/composer
915

10-
WORKDIR /app/laravel
16+
WORKDIR /app/lib
1117

12-
COPY composer.json /app/composer.json
13-
COPY src /app/src
14-
COPY config /app/config
18+
COPY composer.json composer.json
19+
COPY src src
20+
COPY config config
21+
22+
WORKDIR /app/laravel
1523

1624
RUN composer create-project laravel/laravel /app/laravel
17-
RUN composer config repositories.laravel-roadrunner-cache path ../. && composer config minimum-stability dev
25+
RUN composer config repositories.laravel-roadrunner-cache path /app/lib && composer config minimum-stability dev
1826
RUN composer require asanikovich/laravel-roadrunner-cache spiral/roadrunner-cli spiral/roadrunner-http laravel/octane
1927
RUN composer install --optimize-autoloader --no-dev
2028
RUN php artisan vendor:publish --tag="laravel-roadrunner-cache-config"
2129

22-
COPY .rr.yaml ./.rr.yaml
30+
COPY .rr.yaml .rr.yaml
31+
COPY --from=ghcr.io/roadrunner-server/roadrunner:2023.1.1 /usr/bin/rr .
32+
33+
WORKDIR /app/php
2334

24-
COPY --from=ghcr.io/roadrunner-server/roadrunner:2023.1.1 /usr/bin/rr /app/laravel
35+
COPY . .
36+
RUN composer install --optimize-autoloader
2537

2638
EXPOSE 8080/tcp
2739
EXPOSE 6001/tcp
2840

2941
# Run RoadRunner server
30-
CMD ls -al config && ./rr serve -c .rr.yaml -o http.address=0.0.0.0:8080 -o rpc.listen='tcp://0.0.0.0:6001'
42+
CMD ["sh", "-c", "APP_BASE_PATH=/app/laravel /app/laravel/rr serve -c .rr.yaml -o http.address=0.0.0.0:8080 -o rpc.listen='tcp://0.0.0.0:6001'"]

README.md

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ composer require asanikovich/laravel-roadrunner-cache
2222

2323
### Configuration
2424

25-
Make sure you have in your RoadRunner config file (.rr.yaml) next sections:
26-
- RPC section
27-
- KV section
25+
#### Configuration RoadRunner
26+
27+
Make sure you have in your RoadRunner config file (`.rr.yaml`) next sections:
28+
- `RPC` section
29+
- `KV` section
2830

2931
Full example of RoadRunner configuration file:
3032
```yaml
@@ -48,31 +50,73 @@ kv:
4850
interval: 1
4951
```
5052
53+
#### Publish config
5154
Publish the config file and setup RPC connection:
5255
5356
```bash
5457
php artisan vendor:publish --tag="laravel-roadrunner-cache-config"
5558
```
5659

57-
Add to cache configuration file (/config/cache.php) new store with driver 'roadrunner':
60+
#### Serializers
61+
62+
Package supports next serializers (should be configured in `store` config):
63+
- `null` - default php `serializer`
64+
- `igbinary` - igbinary serializer, `ext-igbinary` is required to be installed
65+
66+
#### Encryption
67+
68+
Package supports encryption with `sodium`, requirements:
69+
- `ext-sodium` required to be installed
70+
- `encryption_key` filled in `store` config (generated by `sodium_crypto_box_keypair()`)
71+
72+
#### Setup cache config file
73+
74+
Add to cache configuration file (`/config/cache.php`) new store with driver `roadrunner`:
75+
76+
#### Config file example
5877
```php
5978
<?php
79+
return [
6080
'default' => 'rr-memory', // Default store (optional)
6181

6282
'stores' => [
63-
'rr-memory' => [ // Your custom store name
83+
'rr-memory' => [ // Custom store name with "memory" connection
6484
'driver' => 'roadrunner',
65-
// section name from KV plugin settings in RoadRunner config file (.rr.yaml)
66-
'connection' => 'memory',
85+
'connection' => 'memory', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
86+
'serializer' => null, // Available options: null|igbinary
87+
'encryption_key' => null, // Available options: null|string
6788
],
68-
'rr-boltdb' => [ // Your custom store name (another store is optional)
89+
'rr-boltdb' => [ // Custom store name with "boltdb" connection (another store is optional)
6990
'driver' => 'roadrunner',
70-
// section name from KV plugin settings in RoadRunner config file (.rr.yaml)
71-
'connection' => 'boltdb',
91+
'connection' => 'boltdb', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
92+
'serializer' => null, // Available options: null|igbinary
93+
'encryption_key' => null, // Available options: null|string
94+
'prefix' => 'custom', // Custom prefix - non-empty-string
95+
],
96+
'rr-memory-igbinary' => [ // Custom store name with "memory" connection and "igbinary" serializer
97+
'driver' => 'roadrunner',
98+
'connection' => 'memory', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
99+
'serializer' => 'igbinary', // Available options: null|igbinary
100+
'encryption_key' => null, // Available options: null|string
101+
],
102+
'rr-memory-igbinary-encrypted' => [ // Custom store name with "memory" connection and encrypted "igbinary" serializer
103+
'driver' => 'roadrunner',
104+
'connection' => 'memory', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
105+
'serializer' => 'igbinary', // Available options: null|igbinary
106+
'encryption_key' => 'key1', // Available options: null|string
107+
],
108+
'rr-memory-encrypted' => [ // Custom store name with "memory" connection and encrypted serializer
109+
'driver' => 'roadrunner',
110+
'connection' => 'memory', // section name from KV plugin settings in RoadRunner config file (.rr.yaml)
111+
'serializer' => null, // Available options: null|igbinary
112+
'encryption_key' => 'key2', // Available options: null|string
72113
],
73114
],
115+
]
74116
```
75117

118+
### Usage
119+
76120
To use in your code:
77121
```php
78122
<?php

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"orchestra/testbench": "^8.0"
2222
},
2323
"suggest": {
24-
"ext-igbinary": "(>3.1.6) Igbinary serailizer support"
24+
"ext-igbinary": "(>3.1.6) Igbinary serailizer support",
25+
"ext-sodium": "Sodium encryption support"
2526
},
2627
"autoload": {
2728
"psr-4": {
@@ -35,8 +36,8 @@
3536
},
3637
"scripts": {
3738
"phpstan": "vendor/bin/phpstan analyse --memory-limit=2G",
38-
"test": "vendor/bin/phpunit",
39-
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit",
39+
"test": "docker-compose exec rr /bin/sh -c 'vendor/bin/phpunit'",
40+
"test-coverage": "docker-compose exec rr /bin/sh -c 'XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text'",
4041
"format": "vendor/bin/pint"
4142
},
4243
"config": {

docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ services:
77
- "8080:8080"
88
- "6001:6001"
99
environment:
10-
APP_BASE_PATH: /app/laravel
1110
LARAVEL_OCTANE: 1

phpunit.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@
1515
<coverage>
1616
<report>
1717
<html outputDirectory="build/coverage"/>
18-
<text outputFile="build/coverage.txt"/>
1918
<clover outputFile="build/logs/clover.xml"/>
2019
</report>
2120
</coverage>
2221
<logging>
2322
<junit outputFile="build/report.junit.xml"/>
2423
</logging>
25-
<php>
26-
<env name="DB_DATABASE" value="laravel"/>
27-
<env name="DB_HOST" value="127.0.0.1"/>
28-
<env name="DB_USERNAME" value="root"/>
29-
</php>
3024
<source>
3125
<include>
3226
<directory suffix=".php">./src</directory>

src/RoadRunnerFactory.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Spiral\Goridge\RPC\RPC;
88
use Spiral\Goridge\RPC\RPCInterface;
99
use Spiral\RoadRunner\KeyValue\Factory;
10+
use Spiral\RoadRunner\KeyValue\Serializer\DefaultSerializer;
11+
use Spiral\RoadRunner\KeyValue\Serializer\IgbinarySerializer;
12+
use Spiral\RoadRunner\KeyValue\Serializer\SodiumSerializer;
1013

1114
class RoadRunnerFactory
1215
{
@@ -21,7 +24,18 @@ public static function createRPC(): RPCInterface
2124
*/
2225
public static function createLaravelCacheStore(RPCInterface $rpc, array $config): RoadRunnerCacheStore
2326
{
24-
$storage = (new Factory($rpc))->select($config['connection']);
27+
if (($config['serializer'] ?? null) === 'igbinary') {
28+
$serializer = new IgbinarySerializer();
29+
} else {
30+
$serializer = new DefaultSerializer();
31+
}
32+
33+
$encryptionKey = $config['encryption_key'] ?? null;
34+
if ($encryptionKey !== null) {
35+
$serializer = new SodiumSerializer($serializer, $encryptionKey);
36+
}
37+
38+
$storage = (new Factory($rpc, $serializer))->select($config['connection']);
2539

2640
/** @phpstan-ignore-next-line */
2741
return new RoadRunnerCacheStore($storage, $config['prefix'] ?? config('cache.prefix', ''));

0 commit comments

Comments
 (0)