mkorkmaz/redislabs-rejson

Redislabs RedisJSON 即 ReJSON 模块 PHP 客户端,支持 Predis 和 PhpRedis

2.3 2023-11-03 05:03 UTC

This package is auto-updated.

Last update: 2024-09-03 06:39:13 UTC


README

RedisJSON-PHP 为 PHP 提供 Redislabs ReJSON 模块的客户端。此库支持广泛使用的 Redis 客户端(PECL Redis 扩展Predis)。

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License

关于 RedisJSON

"RedisJSON 是一个 Redis 模块,它实现了 ECMA-404 JSON 数据交换标准作为原生数据类型。它允许从 Redis 键(文档)中存储、更新和检索 JSON 值。"

关于 RedisJSON 的更多信息.

RedisJSON-PHP 接口

命令名称与原始 RedisJSON 命令的小写版本相对应。

<?php

use Redislabs\Interfaces\ModuleInterface;
use Predis\ClientInterface as PredisClient;
use Redis as PhpRedisClient;

interface RedisJsonInterface extends ModuleInterface
{
    public function set(string $key, string $path, $json, ?string $existentialModifier = 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 $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 = '.');
    public function getClient();
    public function raw(string $command, ...$arguments);
    public static function createWithPredis(PredisClient $client);
    public static function createWithPhpRedis(PhpRedisClient $client);
}

安装

推荐使用 composer 安装 RedisJSON-PHP 用于 ReJSON。

composer require mkorkmaz/redislabs-rejson

如果你使用 Redis ReJSON 模块版本 1.0

composer require mkorkmaz/redislabs-rejson:"^1.0"

使用方法

你需要 PECL Redis 扩展或 Predis 来使用 ReJSON-PHP。

创建 RedisJSON 客户端

PECL Redis 扩展的示例
<?php
declare(strict_types=1);

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

$redisClient = new Redis();
$redisClient->connect('127.0.0.1');
$reJSON = ReJSON::createWithPhpRedis($redisClient);
Predis 的示例
<?php
declare(strict_types=1);

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

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

运行命令

  • $key (或 $keys - 包含 $key 项的数组) 参数都是字符串。
  • $json (或 $jsons - 包含 $json 项的数组) 参数可以是任何可 json 编码的数据类型(数组、整数、字符串、stdClass、任何 JsonSerializable 对象等...)。
  • 命令会自动对这些数据进行 json 编码。如果响应是 json 字符串,则函数还会返回 json 解码的数据。
<?php

$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"
  }
}
*/

测试和开发

你可以使用 Redislabs 提供的 Docker 镜像。

docker run -p 6379:6379 redislabs/rejson:2.0.4