1111use Exception ;
1212use InvalidArgumentException ;
1313use Predis \Client as PredisClient ;
14+ use Predis \Cluster \RedisStrategy ;
1415use Predis \Collection \Iterator \Keyspace ;
1516use RobiNN \Pca \Dashboards \DashboardException ;
1617use RobiNN \Pca \Dashboards \Redis \Compatibility \RedisCompatibilityInterface ;
1718use RobiNN \Pca \Dashboards \Redis \Compatibility \RedisExtra ;
19+ use RuntimeException ;
20+ use Throwable ;
1821
1922/**
2023 * @method bool restore(string $key, int $ttl, string $value)
@@ -38,7 +41,7 @@ class PredisCluster extends PredisClient implements RedisCompatibilityInterface
3841 'zset ' => 'zset ' ,
3942 'hash ' => 'hash ' ,
4043 'stream ' => 'stream ' ,
41- 'ReJSON-RL ' => 'rejson ' ,
44+ 'ReJSON-RL ' => 'json ' ,
4245 ];
4346
4447 /**
@@ -207,7 +210,7 @@ public function pipelineKeys(array $keys): array {
207210 if (is_array ($ result ) && count ($ result ) >= 3 ) {
208211 $ data [$ key ] = [
209212 'ttl ' => $ result [0 ],
210- 'type ' => $ result [1 ],
213+ 'type ' => $ this -> data_types [( string ) $ result [ 1 ]] ?? $ result [1 ],
211214 'size ' => $ result [2 ] ?? 0 ,
212215 'count ' => isset ($ result [3 ]) && is_numeric ($ result [3 ]) ? (int ) $ result [3 ] : null ,
213216 ];
@@ -317,4 +320,40 @@ public function commandExists(string $command): bool {
317320 public function restoreKeys (string $ key , int $ ttl , string $ value ): bool {
318321 return (string ) $ this ->restore ($ key , $ ttl , $ value ) === 'OK ' ;
319322 }
323+
324+ /**
325+ * @throws Exception
326+ */
327+ public function jsonGet (string $ key ): string {
328+ return (string ) $ this ->json ('jsonget ' , $ key , [$ key ]);
329+ }
330+
331+ /**
332+ * @throws Exception
333+ */
334+ public function jsonSet (string $ key , mixed $ path ): bool {
335+ return (string ) $ this ->json ('jsonset ' , $ key , [$ key , '$ ' , $ path ]) === 'OK ' ;
336+ }
337+
338+ /**
339+ * Predis cannot route JSON.* commands on its own, so the slot is computed from the key and set on the (native) command explicitly.
340+ *
341+ * @param array<int, mixed> $arguments
342+ *
343+ * @throws Exception
344+ */
345+ private function json (string $ id , string $ key , array $ arguments ): mixed {
346+ $ command = $ this ->createCommand ($ id , $ arguments );
347+ $ command ->setSlot ((new RedisStrategy ())->getSlotByKey ($ key ));
348+
349+ try {
350+ return $ this ->executeCommand ($ command );
351+ } catch (Throwable $ e ) {
352+ throw new RuntimeException ($ e ->getMessage (), (int ) $ e ->getCode (), $ e );
353+ }
354+ }
355+
356+ protected function moduleList (): mixed {
357+ return $ this ->nodes [0 ]->executeRaw (['MODULE ' , 'LIST ' ]);
358+ }
320359}
0 commit comments