Giter Site home page Giter Site logo

phpredis-json's Introduction

Test Coverage Maintainability Build Status Packagist Version GitHub

Phpredis-JSON

RedisJson with the PHP Redis extension phpredis.

Intro

Phpredis-JSON provides a full set of commands for RedisJson Module. It's built on top of the phpredis and use it as Redis client, so you can also take advantage of some of the features included in phpredis as Redis client.

Why?

Although you can issue RedisJSON commands by using some PHP Redis clients which provides you the ability to send raw Redis commands, Phpredis-JSON:

  • avoids you the task of encoding your PHP data structures to JSON before sending them to Redis and decode the JSON responses back from Redis
  • it validates JSON encode/decode and throw a proper exception in case of failure
  • provides a set of commands as methods of the RedisJSON client

Requirements

  • Redis server 4.0+ version (RedisJson Module is only available from Redis 4.0+)
  • RedisJson Module installed on Redis server as specified in Building and loading RedisJSON Module
  • PHP 7.2+ with PHP Redis extension 5 installed

Usage

<?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;

Commands

  • RedisJSON commands: please take a look to the list of phpredis-json commands
  • Phpredis commands: you can send Redis commands as specified in phpredis documentation
  • Raw commands: you can send whatever you want to Redis by using RedisJsonClient::executeRawCommand:
// raw Redis JSON command
$client->executeRawCommand(JsonCommands::GET, 'people', '.');

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

Tests

On a local Redis server 4.0+ with RedisJSON module and Redis extension 5 installed

From console run the following command from the root directory of this project:

./vendor/bin/phpunit

if you don't have configured your local Redis server in 127.0.0.1:6379 you can set REDIS_TEST_SERVER and REDIS_TEST_PORT and REDIS_TEST_DATABASE in ./phpunit.xml file with your local Redis host, port and database before running the above command.

Docker

Having Docker installed, run the following command in the root directory of this project:

bash run-tests-docker.sh

by running the above bash script, two docker services will be built, one with PHP 7.2 with xdebug and redis extensions enabled and another with the image of redislab\rejson:1.0.4 (Redis server 5 with RedisJson module installed). Then the tests will run inside phpredis-json docker service container and finally both container will be stopped.

Examples

License

Phpredis-Json code is distributed under MIT license, see LICENSE file

phpredis-json's People

Contributors

averias avatar gopalvirat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

phpredis-json's Issues

phpredis-json not working properly with Spanish

When I try to use phpredis-json to store spanish text on redis server, the text is not returned its original text.
Example:
Original text: Sabía que ofrecemos
Encoded by json_encode: "Sab\u00eda que ofrecemos
Send to redis server using phpredis-json
Received json: "Sab\u00c3\u00ada que ofrecemos
Decoded by json_decode: "Sabía que ofrecemos

I also try executeRawCommand like below
$this->redisClient()->executeRawCommand(JsonCommands::SET, 'mykeytest', '.', $process_encoded)
$this->redisClient()->executeRawCommand(JsonCommands::GET, 'mykeytest', '.')
It has the same issue.

Do you know what's issue is?
Thank you

Wrong namespace in JsonCommandTraitInterface

In version 1.2.2 JsonCommandTraitInterface is in namespace Averias\RedisJson\Client which does not fit directory structure and causes error.
I assume it should be in Averias\RedisJson\Command\Traits.

Please add support for PHP 8

When installing this using composer, one needs to use --ignore-platform-reqs for every future action on composer e.g. update or to install any other package because of this error:

Problem 1
- averias/phpredis-json is locked to version 1.4.0 and an update of this package was not requested.
- averias/phpredis-json 1.4.0 requires ext-redis ^5.0.1 -> it has the wrong version installed (6.0.0-dev).
- averias/phpredis-json 1.4.0 requires php ^7.2 -> your php version (8.1.6) does not satisfy that requirement.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.