easone95/php-redis-json

基于phpredis扩展的RedisJSON模块的PHP客户端

1.4.1 2022-12-30 11:52 UTC

This package is auto-updated.

Last update: 2024-09-29 06:15:01 UTC


README

Test Coverage Maintainability Build Status Packagist Version GitHub

Phpredis-JSON

使用PHP Redis扩展phpredis的RedisJson。

简介

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

为什么?

虽然您可以通过使用一些提供发送原始Redis命令能力的PHP Redis客户端来执行RedisJSON命令,但Phpredis-JSON

  • 避免了您在将PHP数据结构编码为JSON之前发送到Redis,以及从Redis解码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命令:您可以将Redis命令发送到phpredis文档中指定的命令
  • 原始命令:您可以使用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服务,一个带有PHP 7.2、xdebug和redis扩展的服务,另一个带有redislab\rejson:1.0.4镜像(Redis服务器5,已安装RedisJson模块)。然后测试将在phpredis-json Docker服务容器中运行,最后将停止两个容器。

示例

许可

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