averias/phpredis-json

使用phpredis扩展的RedisJSON模块的PHP客户端

1.4.0 2021-02-27 13:15 UTC

README

Test Coverage Maintainability Build Status Packagist Version GitHub

Phpredis-JSON

RedisJson与PHP Redis扩展phpredis

简介

Phpredis-JSON提供了一套完整的命令,用于RedisJson模块。它建立在phpredis之上,并使用它作为Redis客户端,因此您还可以利用phpredis作为Redis客户端的一些功能。

为什么?

尽管您可以通过一些提供发送原始Redis命令能力的PHP Redis客户端来发出RedisJSON命令,但Phpredis-JSON

  • 避免了您在将PHP数据结构编码为JSON之前发送到Redis以及在Redis中解码JSON响应之前解码的任务
  • 它验证JSON编码/解码,并在失败时抛出适当的异常
  • 提供了一组命令作为RedisJSON客户端的方法

要求

  • Redis服务器4.0+版本(RedisJson模块仅从Redis 4.0+可用)
  • Redis服务器上安装了RedisJson模块,具体请参见构建和加载RedisJSON模块
  • PHP 7.2+与已安装的PHP Redis扩展5

用法

<?php

use Averias\RedisJson\Enum\JsonCommands;
use Averias\RedisJson\Exception\ResponseException;
use Averias\RedisJson\Factory\RedisJsonClientFactory;

// *** get a RedisJsonClient ***
$redisJsonClientFactory = new RedisJsonClientFactory();
/**
 * creates a RedisJsonClient with default connection params:
 * [
 *     'host' => '127.0.0.1',
 *     'port' => 6379,
 *     'timeout' => 0.0, // seconds
 *     'retryInterval' => 15 // milliseconds
 *     'readTimeout' => 2, // seconds
 *     'persistenceId' => null // string for persistent connections, null for no persistent ones
 *     'database' => 0 // Redis database index [0..15]
 * ]
 */
$defaultClient = $redisJsonClientFactory->createClient();

// creates a configured RedisJsonClient
$client = $redisJsonClientFactory->createClient([
    'host' => '127.0.0.1',
    'port' => 6390,
    'timeout' => 2,
    'database' => 15
]);

// *** Redis JSON commands ***
$result = $client->jsonSet('people', ["name" => "gafael", "age" => 12]);
echo ($result === true ? 'true' : 'false') . PHP_EOL; // true

$result = $client->jsonGet('people'); // $result = ["name":"gafael","age":12]
echo json_encode($result) . PHP_EOL; // {"name":"gafael","age":12}

$result =  $client->jsonGet('people', '.name');
echo $result . PHP_EOL; // "gafael"

$result =  $client->jsonGet('people', '.age');
echo $result . PHP_EOL; // 12

// "nonexistent" key does not exist, so a ResponseException is thrown
try {
    $result = $client->jsonGet('nonexistent');
    echo $result . PHP_EOL;
} catch (ResponseException $e) {
    echo "key nonexistent does not exist" . PHP_EOL;
}

// *** you can also send RedisJSON command as raw commands using "RedisJsonClient::executeRawCommand"  ***
// you will send
$result =  $client->executeRawCommand(JsonCommands::SET, 'people', '.colors', '["blue", "green"]');
echo $result . PHP_EOL; // 'OK'

// and receive JSON values
$result =  $client->executeRawCommand(JsonCommands::GET, 'people', '.');
echo $result . PHP_EOL; // {"name":"gafael","age":12,"colors":["blue","green"]}


// *** you can also issue redis commands and use RedisJsonClient as "phpredis" client:
echo $client->hset('hash-key', 'age', 34) . PHP_EOL; // 0
echo $client->hget('hash-key', 'age') . PHP_EOL; // 34

// $ret = [true,"val1",true,"val2"]
$ret = $client->multi()
    ->set('key1', 'val1')
    ->get('key1')
    ->set('key2', 'val2')
    ->get('key2')
    ->exec();

echo json_encode($ret) . PHP_EOL;

命令

  • RedisJSON命令:请参阅phpredis-json命令列表
  • Phpredis命令:您可以根据phpredis文档指定发送Redis命令
  • 原始命令:您可以使用RedisJsonClient::executeRawCommand发送您想要的任何内容到Redis
// raw Redis JSON command
$client->executeRawCommand(JsonCommands::GET, 'people', '.');

// raw Redis command
$client->executeRawCommand('hget, 'hash-key', 'foo');

测试

在本地Redis服务器4.0+上,安装了RedisJSON模块和Redis扩展5

从控制台运行以下命令,命令位于此项目的根目录中

./vendor/bin/phpunit

如果您尚未配置本地Redis服务器为127.0.0.1:6379,您可以在运行上述命令之前,在./phpunit.xml文件中将REDIS_TEST_SERVER、REDIS_TEST_PORT和REDIS_TEST_DATABASE设置为您的本地Redis主机、端口和数据库名称。

Docker

安装Docker后,在项目根目录中运行以下命令

bash run-tests-docker.sh

通过运行上述bash脚本,将构建两个docker服务:一个包含启用xdebug和redis扩展的PHP 7.2,另一个包含redislab\rejson:1.0.4镜像(安装了RedisJson模块的Redis服务器5)。然后,测试将在phpredis-jsondocker服务容器内运行,最后将停止两个容器。

示例

许可证

Phpredis-Json代码根据MIT许可证分发,请参阅LICENSE文件