PHP code example of mkorkmaz / redislabs-rejson

1. Go to this page and download the library: Download mkorkmaz/redislabs-rejson library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mkorkmaz / redislabs-rejson example snippets




use Redislabs\Interfaces\ModuleInterface;
use Predis\Client as PredisClient;
use Redis as PhpRedisClient;
use RedisCluster as PhpRedisClusterClient;

interface RedisJsonInterface extends ModuleInterface
{
    public function set(string $key, string $path, $json, ?string $existentialModifier = null): ?string;
    public function clear(string $key, string $path = '.'): int;
    public function merge(string $key, string $path, mixed $json): ?string;
    public function mset(mixed ...$arguments): ?string;
    public function toggle(string $key, string $path = '.'): bool|array|null;
    public function get(...$arguments);
    public function del(string $key, ?string $path = '.'): int;
    public function forget(string $key, ?string $path = '.'): int;
    public function mget(...$arguments);
    public function type(string $key, ?string $paths = '.');
    public function numincrby(string $key, string $path, int|float $incrementBy);
    /** @deprecated RedisJSON deprecated JSON.NUMMULTBY in version 2.0. */
    public function nummultby(string $key, string $path, int|float $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 = null, ?int $stop = null);
    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 = '.');
    public function getClient(): object;
    public function raw(string $command, mixed ...$arguments): mixed;
    public static function createWithPredis(PredisClient $client): static;
    public static function createWithPhpRedis(PhpRedisClient $client): static;
    public static function createWithPhpRedisCluster(PhpRedisClusterClient $client): static;
}


declare(strict_types=1);

use Redis;
use Redislabs\Module\RedisJson\RedisJson;

$redisClient = new Redis();
$redisClient->connect('127.0.0.1');
$reJSON = RedisJson::createWithPhpRedis($redisClient);


declare(strict_types=1);

use Predis;
use Redislabs\Module\RedisJson\RedisJson;

$redisClient = new Predis\Client();
$redisJson = RedisJson::createWithPredis($redisClient);



$redisJson->set('test', '.', ['foo'=>'bar'], 'NX');
$redisJson->set('test', '.baz', 'qux');
$redisJson->set('test', '.baz', 'quux', 'XX');
$redisJson->set('test2', '.', ['foo2'=>'bar2']);
$baz = $redisJson->get('test', '.baz');

var_dump($baz);
// Prints string(4) "quux"
$array = $redisJson->get('test', '.');
var_dump($array);
/*
Prints result as an array instead of an object
array(2) {
  ["foo"]=>
  string(3) "bar"
  ["baz"]=>
  string(4) "quux"
}

*/
$array = $redisJson->mget('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"
  }
}
*/


$json = RedisJson::createWithPhpRedis($redisClient);
$json->mset(
    '{account:42}:profile', '$', ['enabled' => true, 'count' => 1.5],
    '{account:42}:items', '$', []
);
$json->merge('{account:42}:profile', '$', ['count' => null, 'name' => 'Ada']);
$enabled = $json->toggle('{account:42}:profile', '$.enabled'); // false
$cleared = $json->clear('{account:42}:items'); // 0: already empty

use Redislabs\Module\RedisJson\RedisJson;

$cluster = new RedisCluster(
    null,
    ['127.0.0.1:7000', '127.0.0.1:7001', '127.0.0.1:7002'],
    2.0,
    2.0,
    false,
    getenv('REDIS_CLUSTER_PASSWORD') ?: null
);
$json = RedisJson::createWithPhpRedisCluster($cluster);
$json->set('{customer:42}:profile', '$', ['name' => 'Ada']);
$json->set('{customer:42}:settings', '$', ['theme' => 'dark']);
$profile = $json->get('{customer:42}:profile', '$');
$documents = $json->mget('{customer:42}:profile', '{customer:42}:settings', '$');
sh
REDIS_CLUSTER_SEEDS=127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002 \
    vendor/bin/phpunit --bootstrap vendor/autoload.php tests/Module/RedisClusterTest.php
sh
REDIS_CLUSTER_SEEDS=127.0.0.1:7000 \
    ../rejson/vendor/bin/phpunit --bootstrap vendor/autoload.php \
    ../rejson/tests/Module/RedisClusterTest.php
sh
php -r '$config = json_decode(file_get_contents("composer.json"), true, flags: JSON_THROW_ON_ERROR); $config["repositories"] = [["type" => "path", "url" => "../common", "options" => ["versions" => ["mkorkmaz/redislabs-common" => "2.0.0"]]]]; file_put_contents("composer.local.json", json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));'
COMPOSER=composer.local.json composer update