averias/phpredis-json
Composer 安装命令:
composer require averias/phpredis-json
包简介
A PHP client for RedisJSON module using phpredis extension
关键字:
README 文档
README
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
averias/phpredis-json 适用场景与选型建议
averias/phpredis-json 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29.3k 次下载、GitHub Stars 达 34, 最近一次更新时间为 2019 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「php」 「redisjson」 「rejson」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 averias/phpredis-json 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 averias/phpredis-json 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 averias/phpredis-json 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Microservice RPC through message queues.
The CodeIgniter Redis package
贝嘟分布式缓存扩展
Redis distributed locks for laravel
Redis backed library for creating background jobs and processing them later.
Alfabank REST API integration
统计信息
- 总下载量: 29.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 38
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-07