phpredis json
1.0.0
RedisJson 與 PHP Redis 擴充 phpredis。
Phpredis-JSON 為 RedisJson 模組提供了一整套指令。它建構在phpredis
之上,並將其用作 Redis 用戶端,因此您也可以利用phpredis
中包含的一些功能作為 Redis 用戶端。
儘管您可以使用某些 PHP Redis 用戶端發出 RedisJSON 命令,這些用戶端使您能夠發送原始 Redis 命令,但 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
向 Redis 發送任何您想要的內容: // raw Redis JSON command
$ client -> executeRawCommand (JsonCommands:: GET , ' people ' , ' . ' );
// raw Redis command
$ client -> executeRawCommand ( ' hget, ' hash-key', ' foo ' );
從控制台從該專案的根目錄執行以下命令:
./vendor/bin/phpunit
如果您尚未在 127.0.0.1:6379 中設定本機 Redis 伺服器,則可以在執行上述命令之前使用本機 Redis 主機、連接埠和資料庫在./phpunit.xml
檔案中設定 REDIS_TEST_SERVER 和 REDIS_TEST_PORT 和 REDIS_TEST_DATABASE。
安裝 Docker 後,在該專案的根目錄中執行以下命令:
bash run-tests-docker.sh
透過執行上述 bash 腳本,將建置兩個 docker 服務,一個使用 PHP 7.2,啟用 xdebug 和 redis 擴展,另一個使用redislabrejson:1.0.4
的映像(安裝了 RedisJson 模組的 Redis 伺服器 5)。然後測試將在phpredis-json
docker 服務容器內運行,最後兩個容器都將停止。
Phpredis-Json 程式碼在 MIT 許可證下分發,請參閱許可證文件