mkorkmaz/redislabs-rejson
Composer 安装命令:
composer require mkorkmaz/redislabs-rejson
包简介
Redislabs RedisJSON aka ReJSON Module Client for PHP that supports Predis and PhpRedis
README 文档
README
RedisJSON-PHP provides a client for Redislabs' ReJSON Module for PHP. This library supports both widely used redis clients (PECL Redis Extension and Predis).
About RedisJSON
"RedisJSON is a Redis module that implements ECMA-404 The JSON Data Interchange Standard as a native data type. It allows storing, updating and fetching JSON values from Redis keys (documents)".
RedisJSON-PHP Interface
Commanda are named after lowercase version of the original RedisJSON commands.
<?php use Redislabs\Interfaces\ModuleInterface; use Predis\ClientInterface as PredisClient; use Redis as PhpRedisClient; interface RedisJsonInterface extends ModuleInterface { public function set(string $key, string $path, $json, ?string $existentialModifier = null); public function get(...$arguments); public function del(string $key, ?string $path = '.'): int; public function forget(string $key, ?string $path = '.'): int; public function mget(...$arguments); public function type(string $key, ?string $paths = '.'); public function numincrby(string $key, string $path, int $incrementBy); public function nummultby(string $key, string $path, int $multiplyBy); public function strappend(string $key, $json, ?string $path = '.'); public function strlen(string $key, ?string $path = '.'); public function arrappend(string $key, string $path, ...$jsons); public function arrindex(string $key, string $path, $json, ?int $start = 0, ?int $stop = 0); public function arrinsert(string $key, string $path, int $index, ...$jsons); public function arrlen(string $key, string $path = '.'); public function arrpop(string $key, ?string $path = '.', ?int $index = -1); public function arrtrim(string $key, $path, ?int $start = 0, ?int $stop = 0); public function objkeys(string $key, ?string $path = '.'); public function objlen(string $key, ?string $path = '.'); public function debug(string $subcommand, ?string $key = null, ?string $path = '.'); public function resp(string $key, ?string $paths = '.'); public function getClient(); public function raw(string $command, ...$arguments); public static function createWithPredis(PredisClient $client); public static function createWithPhpRedis(PhpRedisClient $client); }
Installation
The recommended method to install RedisJSON-PHP for ReJSON is with composer.
composer require mkorkmaz/redislabs-rejson
If you use Redis ReJSON module version 1.0:
composer require mkorkmaz/redislabs-rejson:"^1.0"
Usage
You need PECL Redis Extension or Predis to use ReJSON-PHP.
Creating RedisJSON Client
Example for PECL Redis Extension
<?php declare(strict_types=1); use Redis; use Redislabs\Module\RedisJson\RedisJson; $redisClient = new Redis(); $redisClient->connect('127.0.0.1'); $reJSON = ReJSON::createWithPhpRedis($redisClient);
Example for Predis
<?php declare(strict_types=1); use Predis; use Redislabs\Module\RedisJson\RedisJson; $redisClient = new Predis\Client(); $redisJson = RedisJson::createWithPredis($redisClient);
Running commands
- $key (or $keys - array that containes $key items) parameters are all string.
- $json (or $jsons - array that containes $json items) parameters can be any type of json encodable data (array, int, string, stdClass, any JsonSerializable object etc...).
- Commands automatically performs json encoding these data. Functions also returns json decoded data if the response is json string.
<?php $redisJson->set('test', '.', ['foo'=>'bar'], 'NX'); $redisJson->set('test', '.baz', 'qux'); $redisJson->set('test', '.baz', 'quux', 'XX'); $redisJson->set('test2', '.', ['foo2'=>'bar2']); $baz = $redisJson->get('test', '.baz'); var_dump($baz); // Prints string(4) "quux" $array = $redisJson->get('test', '.'); var_dump($array); /* Prints result as an array instead of an object array(2) { ["foo"]=> string(3) "bar" ["baz"]=> string(4) "quux" } */ $array = $redisJson->mget('test', 'test2', '.'); var_dump($array); /* Prints result as an associative array instead of an object array(2) { ["test"]=> array(2) { ["foo"]=> string(3) "bar" ["baz"]=> string(4) "quux" } ["test2"]=> array(1) { ["foo2"]=> string(3) "bar2" } } */
Test and Development
You can use Docker Image provided by Redislabs.
docker run -p 6379:6379 redislabs/rejson:2.0.4
mkorkmaz/redislabs-rejson 适用场景与选型建议
mkorkmaz/redislabs-rejson 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 126.19k 次下载、GitHub Stars 达 43, 最近一次更新时间为 2018 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mkorkmaz/redislabs-rejson 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mkorkmaz/redislabs-rejson 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 126.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 43
- 点击次数: 13
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-07