mkorkmaz / redislabs-rejson
PHP Client for Redislabs ReJSON Module.
Installs: 1 326
Dependents: 1
Suggesters: 0
Security: 0
Stars: 14
Watchers: 4
Forks: 2
Open Issues: 1
Requires
- php: ^7.3 | ^8.0
- ext-json: *
- mkorkmaz/redislabs-common: ^0.2
Requires (Dev)
- ext-redis: *
- codeception/codeception: ^4.0
- codeception/module-asserts: ^1.3
- php-coveralls/php-coveralls: ^2.1
- phpstan/phpstan: ^0.12
- predis/predis: ^1.1
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.3
Suggests
- ext-redis: If your application depends of redis extention.
- predis/predis: If your application depends on predis.
This package is auto-updated.
Last update: 2021-01-05 20:57:58 UTC
README
ReJSON-PHP provides PHP Client for Redislabs' ReJSON. This library supports both widely used redis clients (PECL Redis Extension and Predis).
About ReJSON
"ReJSON is a Redis module that implements ECMA-404 The JSON Data Interchange Standard as a native data type. It allows storing, updating and fetching JSON values from Redis keys (documents)".
ReJSON-PHP Interface
Commanda are named after lowercase version of the original ReJSON commands.
<?php interface ReJSON { public function set(string $key, string $path, $json, ?string $existentialModifier = null); // $existentialModifiers: ['NX', 'XX'] public function get(string $key, $paths = null); public function getArray(string $key, $paths = null); public function del(string $key, ?string $path = '.') : int; public function forget(string $key, ?string $path = '.') : int; public function mget(...$keys, string $path); public function mgetArray(...$keys, string $path); public function type(string $key, ?string $paths = '.'); public function numincrby(string $key, string $path, int $incrementBy); public function nummultby(string $key, string $path, int $multiplyBy); public function strappend(string $key, $json, ?string $path = '.'); public function strlen(string $key, ?string $path = '.'); public function arrappend(string $key, string $path, ...$jsons); public function arrindex(string $key, string $path, $json, ?int $start = 0, ?int $stop = 0); public function arrinsert(string $key, string $path, int $index, ...$jsons); public function arrlen(string $key, string $path = '.'); public function arrpop(string $key, ?string $path = '.', ?int $index = -1); public function arrtrim(string $key, $path, ?int $start = 0, ?int $stop = 0); public function objkeys(string $key, ?string $path = '.'); public function objlen(string $key, ?string $path = '.'); public function debug(string $subcommand, ?string $key = null, ?string $path = '.'); public function resp(string $key, ?string $paths = '.'); }
Installation
The recommended method to install ReJSON-PHP is with composer.
composer require mkorkmaz/redislabs-rejson
Usage
You need PECL Redis Extension or Predis to use ReJSON-PHP.
Creating ReJSON Client
Example for PECL Redis Extension
<?php declare(strict_types=1); use Redis; use Redislabs\Module\ReJSON\ReJSON; $redisClient = new Redis(); $redisClient->connect('127.0.0.1'); $reJSON = ReJSON::createWithPhpRedis($redisClient);
Example for Predis
<?php declare(strict_types=1); use Predis; use Redislabs\Module\ReJSON\ReJSON; $redisClient = new Predis\Client(); $reJSON = ReJSON::createWithPredis($redisClient);
Running commands
- $key (or $keys - array that containes $key items) parameters are all string.
- $json (or $jsons - array that containes $json items) parameters can be any type of json encodable data (array, int, string, stdClass, any JsonSerializable object etc...).
- Commands automatically performs json encoding these data. Functions also returns json decoded data if the response is json string.
<?php $reJSON->set('test', '.', ['foo'=>'bar'], 'NX'); $reJSON->set('test', '.baz', 'qux'); $reJSON->set('test', '.baz', 'quux', 'XX'); $reJSON->set('test2', '.', ['foo2'=>'bar2']); $baz = $reJSON->get('test', '.baz'); var_dump($baz); // Prints string(4) "quux" $array = $reJSON->getArray('test', '.'); var_dump($array); /* Prints result as an array instead of an object array(2) { ["foo"]=> string(3) "bar" ["baz"]=> string(4) "quux" } */ $array = $reJSON->mgetArray('test', 'test2', '.'); var_dump($array); /* Prints result as an associative array instead of an object array(2) { ["test"]=> array(2) { ["foo"]=> string(3) "bar" ["baz"]=> string(4) "quux" } ["test2"]=> array(1) { ["foo2"]=> string(3) "bar2" } } */
Test and Development
You can use Docker Image provided by Redislabs.
docker run -p 6379:6379 redislabs/rejson:latest