RedisJson mit der PHP Redis-Erweiterung phpredis.
Phpredis-JSON bietet einen vollständigen Befehlssatz für das RedisJson-Modul. Es basiert auf phpredis
und kann als Redis-Client verwendet werden, sodass Sie einige der in phpredis
enthaltenen Funktionen auch als Redis-Client nutzen können.
Obwohl Sie RedisJSON-Befehle ausgeben können, indem Sie einige PHP Redis-Clients verwenden, die Ihnen die Möglichkeit bieten, rohe Redis-Befehle zu senden, Phpredis-JSON:
<?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 ;
RedisJsonClient::executeRawCommand
können Sie alles, was Sie wollen, an Redis senden: // raw Redis JSON command
$ client -> executeRawCommand (JsonCommands:: GET , ' people ' , ' . ' );
// raw Redis command
$ client -> executeRawCommand ( ' hget, ' hash-key', ' foo ' );
Führen Sie über die Konsole den folgenden Befehl aus dem Stammverzeichnis dieses Projekts aus:
./vendor/bin/phpunit
Wenn Sie Ihren lokalen Redis-Server nicht in 127.0.0.1:6379 konfiguriert haben, können Sie REDIS_TEST_SERVER, REDIS_TEST_PORT und REDIS_TEST_DATABASE in der Datei ./phpunit.xml
mit Ihrem lokalen Redis-Host, Port und Datenbank festlegen, bevor Sie den obigen Befehl ausführen.
Führen Sie nach der Installation von Docker den folgenden Befehl im Stammverzeichnis dieses Projekts aus:
bash run-tests-docker.sh
Durch Ausführen des obigen Bash-Skripts werden zwei Docker-Dienste erstellt, einer mit PHP 7.2 mit aktivierten xdebug- und Redis-Erweiterungen und ein anderer mit dem Image von redislabrejson:1.0.4
(Redis-Server 5 mit installiertem RedisJson-Modul). Anschließend werden die Tests im Docker-Service-Container phpredis-json
ausgeführt und schließlich werden beide Container gestoppt.
Phpredis-Json-Code wird unter MIT-Lizenz vertrieben, siehe LIZENZ-Datei