-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRedis
1208 lines (1092 loc) · 34.7 KB
/
Redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Created by TextMate.
* User: lidiexy
* Date: 4/11/14
* Time: 5:09 PM
* Version: 0.1.0
*/
/**
* Class Redis
* @private $host
* @private $port
* @private $conn Connection handler
*/
class Redis {
const MAX_DB_COUNT = 15;
private $host;
private $port;
private $conn;
public function __construct($host = '127.0.0.1', $port = 6379) {
$this->host = $host;
$this->port = $port;
}
/**
* Function for create and get socket connection
* @return resource
* @throws Exception
*/
private function getConnection() {
if(!$this->conn) {
if (!$sock = fsockopen($this->host, $this->port, $errno, $errstr)) {
$error_msg = "ERROR: $errno - $errstr.\n";
throw new Exception($error_msg);
}
$this->conn = $sock;
}
return $this->conn;
}
/**
* Close connection with Redis. Just the QUIT command.
* @return bool|string
*/
private function closeConnection() {
if($this->conn) {
$cmd = 'QUIT';
$response = $this->executeCommand($cmd);
return $this->getError($response);
} else {
return false;
}
}
private function executeCommand($commands) {
$command = null;
$this->getConnection();
if (!$this->conn) return false;
if (is_array($commands)) {
$commands = implode("\r\n", $commands);
}
$command .= $commands . "\r\n";
$fwrite = 0;
for ($written = 0; $written < strlen($command); $written += $fwrite) {
if (!$fwrite = fwrite($this->conn, substr($command, $written))) {
return false;
}
}
return $this->getResponse();
}
private function getResponse() {
if (!$this->conn) return false;
return trim(fgets($this->conn), "\r\n ");
}
private function getError($response) {
if (strpos($response, '-ERR') === 0) {
return substr($response, 5);
}
return false;
}
/**
* Return formated string of $value to accept spaces in the string.
* @param $value
* @return string
*/
private function generateValidValue(&$value) {
if(strpos($value, ' ') === false) {
return $value;
} else {
$value = trim($value, '"\' ');
return '"' . $value . '"';
}
}
private function validateEntry($key, &$value) {
if(empty($key) || !ctype_alnum($key) ) {
$error_msg = "ERROR: Redis server need alphanumeric key.\n";
throw new Exception($error_msg);
} elseif (empty($value)) {
$error_msg = "ERROR: This Redis class implementation deny empty values.\n";
throw new Exception($error_msg);
}
$value = $this->generateValidValue($value);
return true;
}
/**
* PING to Redis Server, must response +PONG
* @return bool|string
*/
public function ping() {
return $this->executeCommand('PING');
}
/**
* Select database for Redis. The max number of database allowed is 15.
* @param int $db
* @return bool|string
* @throws Exception
*/
public function selectDB($db = 0) {
if(($db < 0) && ($db > self::MAX_DB_COUNT)) {
$error_msg = "ERROR: The range of allowed index for DB is 0 - " . self::MAX_DB_COUNT . " for databases in Redis.\n";
throw new Exception($error_msg);
} else {
$cmd = 'SELECT ' . $db;
return $this->executeCommand($cmd);
}
}
/************ Scalar Operations ************/
/**
* Execute a SET command to Redis Scalar .
* $key must be alphanumeric
* $value can not be empty
* @param $key
* @param $value
* @return bool|string
*/
public function set($key, $value) {
if($this->validateEntry($key, $value)) {
$cmd = 'SET ' . $key . ' ' . $value;
$response = $this->executeCommand($cmd);
return $this->getError($response);
} else {
return false;
}
}
/**
* Execute a GET command to Redis Scalar.
* @param $key
* @return bool|string
*/
public function get($key) {
$cmd = "GET " . $key;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
$length = (int)substr($response, 1);
if ($length > 0) {
$value = $this->getResponse();
return $value;
} else {
return false;
}
}
/**
* Execute a DEL command to Redis. Delete a key.
* @param $key
* @return bool|string
*/
public function delete($key) {
$cmd = "DEL " . $key;
return $this->executeCommand($cmd);
}
/**
* Check if a key exists in Redis.
* @param $key
* @return bool
*/
public function exists($key) {
$cmd = "EXISTS " . $key;
return $this->executeCommand($cmd) == ':1';
}
/**
* Increment any key by 1 or with the increment number defined by $by param.
* @param $key
* @param int $by
* @return string
*/
public function inc($key, $by = 1) {
if(!isset($by) || $by === 1) {
$cmd = "INCR " . $key;
} else {
$cmd = "INCRBY " . $key . " " . $by;
}
$response = $this->executeCommand($cmd);
return substr($response, 1);
}
/**
* Decrement any key by 1 or with the decrement number defined by $by param.
* @param $key
* @param int $by
* @return string
*/
public function dec($key, $by = 1) {
if(!isset($by) || $by === 1) {
$cmd = "DECR " . $key;
} else {
$cmd = "DECRBY " . $key . " " . $by;
}
$response = $this->executeCommand($cmd);
return substr($response, 1);
}
/**
* Append $value to scalar defined in $key.
* @param $key
* @param $value
* @return bool|string
*/
public function appendToScalar($key, $value) {
if($this->validateEntry($key, $value)) {
$cmd = "APPEND " . $key . " " . $value;
$response = $this->executeCommand($cmd);
return substr($response, 1);
} else {
return $this->get($key);
}
}
/************ List Operations ************/
/**
* Return the length of the list, FALSE if an error was raised.
* @param $key
* @return int|bool
*/
public function getListLength($key) {
$cmd = "LLEN " . $key;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
}
/**
* Insert to the top of list $key the element $value.
* @param $key
* @param $value
* @return bool|int|string Return the length of the list $key or error message
*/
public function prependList($key, $value) {
if($this->validateEntry($key, $value)) {
$cmd = "LPUSH " . $key . " " . $value;
$response = $this->executeCommand($cmd);
if(!$this->getError($response)) {
return (int) substr($response, 1);
} else {
return $this->getError($response);
}
} else {
return false;
}
}
/**
* Add to the bottom of list $key the element $value.
* @param $key
* @param $value
* @return bool|int|string Return the length of the list $key or error message
*/
public function appendList($key, $value) {
if($this->validateEntry($key, $value)) {
$cmd = "RPUSH " . $key . " " . $value;
$response = $this->executeCommand($cmd);
if(!$this->getError($response)) {
return (int) substr($response, 1);
} else {
return $this->getError($response);
}
} else {
return false;
}
}
/**
* Remove and get the first element in a $key list.
* @param $key
* @return bool|string
*/
public function popFirstFromList($key) {
$cmd = "LPOP " . $key;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
$length = (int)substr($response, 1);
if ($length > 0) {
$value = $this->getResponse();
return $value;
} else {
return false;
}
}
/**
* Remove and get the last element in a $key list.
* @param $key
* @return bool|string
*/
public function popLastFromList($key) {
$cmd = "RPOP " . $key;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
$length = (int)substr($response, 1);
if ($length > 0) {
$value = $this->getResponse();
return $value;
} else {
return false;
}
}
/**
* Return the elements of the list $key. The amount are defined by $limit and beginning from $offset.
* If no params then the function returns all the elements from the list $key.
* If $limit = 0 and $offset is not 0, then the function return all the elements beginning from $offset.
* Also this function permit negative values for $limit and guarantees the top elements from $offset start.
* @param $key
* @param int $limit Total count of elements.
* @param int $offset Start point to get the elements.
* @return array
*/
public function getList($key, $limit = 0, $offset = 0) {
$start = $offset;
if($limit == 0) {
$end = $this->getListLength($key) - 1;
} elseif($limit < 0) {
$end = $limit;
} else {
$limit--;
$end = $start + $limit;
}
$cmd = 'LRANGE ' . $key . ' ' . $start . ' ' . $end;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
$count = (int)substr($response, 1);
$list = array();
for ($i = 0; $i < $count; $i++) {
$length = substr($this->getResponse(), 1);
$value = $this->getResponse();
$list[] = $value;
}
return $list;
}
/**
* Return the intersection of List defined by $key with the $filters array given.
* You may get the an array filtered with a range of elements.
* The amount are defined by $limit and beginning from $offset.
* @param $key
* @param array $filters
* @param int $limit
* @param int $offset
* @return array
*/
public function getFilteredList($key, $filters = array(), $limit = 0, $offset = 0) {
$listToFilter = $this->getList($key, $limit, $offset);
if(sizeof($filters) == 0) {
return $listToFilter;
}
$list = array_intersect($listToFilter, $filters);
return $list;
}
/**
* Remove $count elements of the list $key with matching with $value.
* According with the syntax of Redis server, $count could be negative or positive number.
* It is mean that the search to remove $value start from top or bottom of the list.
* $count = 0 it's the same case of $count = +/- 1
* @param $key
* @param $value
* @param int $count
* @return bool|int
*/
public function removeFromList($key, $value, $count = 0) {
$cmd = 'LREM ' . $key . ' ' . $count . ' ' . $value;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
}
/**
* Remove the string or array of elements from $key list.
* @param $key
* @param $filters string|array
* @return bool|int
*/
public function removeByFilter($key, $filters) {
if(!is_array($filters)) {
return $this->removeFromList($key, $filters);
} elseif(is_array($filters) && sizeof($filters) == 1) {
return $this->removeFromList($key, $filters);
} else {
$list = $this->getFilteredList($key, $filters);
foreach ($list as $item) {
$this->removeFromList($key, $item);
}
return true;
}
}
/**
* Truncate the list $key. The range are defined by $limit and beginning from $offset.
* Also this function permit negative values for $limit and slice the elements between $offset and $limit.
* @param $key
* @param int $limit
* @param int $offset
* @return bool
*/
public function truncateList($key, $limit, $offset = 0) {
$start = $offset;
if($limit == 0) {
$end = $this->getListLength($key) - 1;
} elseif($limit < 0) {
$end = $limit;
} else {
$limit--;
$end = $start + $limit;
}
$cmd = 'LTRIM ' . $key . ' ' . $start . ' ' . $end;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return true;
}
/************ Set Operations ************/
/**
* Add member $value to the set $set. Repeated values will not added to the set.
* @param $key
* @param $value
* @return bool
*/
public function addSetMember($key, $value) {
if($this->validateEntry($key, $value)) {
$cmd = 'SADD ' . $key . ' ' . $value;
$response = $this->executeCommand($cmd);
return $response == ':1';
} else {
return false;
}
}
/**
* Remove member $value from the set $set.
* @param $key
* @param $value
* @return bool
*/
public function removeSetMember($key, $value) {
if($this->validateEntry($key, $value)) {
$cmd = 'SREM ' . $key . ' ' . $value;
$response = $this->executeCommand($cmd);
return $response == ':1';
} else {
return false;
}
}
/**
* Check if $value is part of the set $set.
* @param $key
* @param $value
* @return bool
*/
public function isSetMember($key, $value) {
if($this->validateEntry($key, $value)) {
$cmd = 'SISMEMBER ' . $key . ' ' . $value;
$response = $this->executeCommand($cmd);
return $response == ':1';
} else {
return false;
}
}
/**
* Return the array of members of the set $key.
* @param $key
* @return array|bool
*/
public function getSetMembers($key) {
$cmd = 'SMEMBERS ' . $key;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
$count = (int)substr($response, 1);
$list = array();
for ($i = 0; $i < $count; $i++) {
$length = substr($this->getResponse(), 1);
$value = $this->getResponse();
$list[] = $value;
}
return $list;
}
/**
* Get the length or the number of members in a set defined by $key.
* @param $key
* @return bool|int
*/
public function getSetLength($key) {
$cmd = "SCARD " . $key;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
}
/**
* Subtract multiple sets and return the array of elements of the operation.
* @param $key_resulted
* @param $key_search string|array
* @return array|bool
*/
public function diffSet($key_resulted, $key_search) {
if(!is_array($key_search)) {
$cmd = 'SDIFF ' . $key_resulted . ' ' . $key_search;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
} elseif(is_array($key_search) && sizeof($key_search) == 1) {
$cmd = 'SDIFF ' . $key_resulted . ' ' . $key_search[0];
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
} else {
$cmd = 'SDIFF ' . $key_resulted . ' ';
foreach ($key_search as $key) {
$cmd .= $key . ' ';
}
$cmd = rtrim($cmd);
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
}
$count = (int)substr($response, 1);
$list = array();
for ($i = 0; $i < $count; $i++) {
$length = substr($this->getResponse(), 1);
$value = $this->getResponse();
$list[] = $value;
}
return $list;
}
/**
* Subtract multiple sets and store the resulting set in a key
* @param $destination_key
* @param $key_resulted
* @param $key_search string|array
* @return bool|string
*/
public function diffSetStored($destination_key, $key_resulted, $key_search) {
if(!is_array($key_search)) {
$cmd = 'SDIFFSTORE ' . $destination_key . ' ' . $key_resulted . ' ' . $key_search;
$response = $this->executeCommand($cmd);
return $this->getError($response);
} elseif(is_array($key_search) && sizeof($key_search) == 1) {
$cmd = 'SDIFFSTORE ' . $destination_key . ' ' . $key_resulted . ' ' . $key_search[0];
$response = $this->executeCommand($cmd);
return $this->getError($response);
} else {
$cmd = 'SDIFFSTORE ' . $destination_key . ' ' . $key_resulted . ' ';
foreach ($key_search as $key) {
$cmd .= $key . ' ';
}
$cmd = rtrim($cmd);
$response = $this->executeCommand($cmd);
return $this->getError($response);
}
}
/************ Ordered Set Operations ************/
/**
* Add one or more members to a sorted set, or update its score if it already exists.
* $assoc_array have the form [value => score] where the KEY of the array is the Ni {value} of $key
* and the VALUE for that KEY is the Ni {score} associated.
* @param $key
* @param $assoc_array
* @return bool
*/
public function addSortedSetMember($key, $assoc_array) {
if(is_array($assoc_array)) {
$cmd = 'ZADD ' . $key . ' ';
foreach ($assoc_array as $value => $score) {
$cmd .= $score . ' ' . $value . ' ';
}
$cmd = rtrim($cmd);
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
} else {
return false;
}
}
/**
* Get the number of members in a sorted set.
* @param $key
* @return bool|int
*/
public function getSortedSetLength($key) {
$cmd = "ZCARD " . $key;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
}
/**
* Count the members in a sorted set with scores within the given values [$min, $max].
* @param $key
* @param $min
* @param $max
* @return bool|int
* @throws Exception
*/
public function getSortedSetCountWithRangeOfScore($key, $min, $max) {
if($min > $max) {
$error_msg = "ERROR: The argument \$min must be leather than \$max argument.\n";
throw new Exception($error_msg);
}
$cmd = "ZCOUNT " . $key . ' ' . $min . ' ' . $max;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
}
/**
* Return a range of members in a sorted set, by index.
* The array returned have the form [value => score] when the argument $with_score = TRUE.
* If the argument $with_score = FALSE then the array will return [# => value].
* @param $key
* @param int $limit
* @param int $offset
* @param bool $with_score
* @return array|bool
*/
public function getSortedSetRangeByIndex($key, $limit = 0, $offset = 0, $with_score = false) {
$start = $offset;
if($limit == 0) {
$end = $this->getSortedSetLength($key) - 1;
} elseif($limit < 0) {
$end = $limit;
} else {
$limit--;
$end = $start + $limit;
}
$cmd = 'ZRANGE ' . $key . ' ' . $start . ' ' . $end;
if($with_score) {
$cmd .= ' WITHSCORES';
}
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
$count = (int)substr($response, 1);
$list = array();
$value = '';
for ($i = 0; $i < $count; $i++) {
$length = substr($this->getResponse(), 1);
if($with_score) {
if($i % 2 == 0) {
$value = $this->getResponse();
} else {
$score = $this->getResponse();
$list[$value] = $score;
}
} else {
$value = $this->getResponse();
$list[] = $value;
}
}
return $list;
}
/**
* Return a range of members in a sorted set, by score.
* The array returned have the form [value => score] when the argument $with_score = TRUE.
* If the argument $with_score = FALSE then the array will return [# => value].
* @param $key
* @param $min
* @param $max
* @param bool $with_score
* @param int $limit
* @param int $offset
* @return array|bool
* @throws Exception
*/
public function getSortedSetRangeByScore($key, $min, $max, $with_score = false, $limit = 0, $offset = 0) {
if($min > $max) {
$error_msg = "ERROR: The argument \$min must be leather than \$max argument.\n";
throw new Exception($error_msg);
}
$start = $offset;
if($limit <= 0) {
$end = $this->getSortedSetLength($key) - 1;
} else {
$limit--;
$end = $start + $limit;
}
$cmd = 'ZRANGEBYSCORE ' . $key . ' ' . $min . ' ' . $max;
if($with_score) {
$cmd .= ' WITHSCORES';
}
$cmd .= ' LIMIT ' . $start . ' ' . $end;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
$count = (int)substr($response, 1);
$list = array();
$value = '';
for ($i = 0; $i < $count; $i++) {
$length = substr($this->getResponse(), 1);
if($with_score) {
if($i % 2 == 0) {
$value = $this->getResponse();
} else {
$score = $this->getResponse();
$list[$value] = $score;
}
} else {
$value = $this->getResponse();
$list[] = $value;
}
}
return $list;
}
/**
* Determine the index of a member in a sorted set.
* @param $key
* @param $member
* @return bool|int
*/
public function getSortedSetRank($key, $member) {
if($this->validateEntry($key, $member)) {
$cmd = "ZRANK " . $key . ' ' . $member;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
} else {
return false;
}
}
/**
* Get the score associated with the given member in a sorted set.
* @param $key
* @param $member
* @return bool|string
*/
public function getSortedSetScore($key, $member) {
if($this->validateEntry($key, $member)) {
$cmd = "ZSCORE " . $key . ' ' . $member;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return $this->getResponse();
} else {
return false;
}
}
/**
* Remove one or more members from a sorted set.
* The argument $array_members must be an array instead it have jus one member.
* @param $key
* @param array $array_members
* @return bool|int
*/
public function removeFromSortedSet($key, $array_members) {
if(is_array($array_members)) {
$cmd = 'ZREM ' . $key . ' ';
foreach ($array_members as $member) {
$cmd .= $member . ' ';
}
$cmd = rtrim($cmd);
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
} else {
return false;
}
}
/**
* Remove all members in a sorted set within the given indexes.
* @param $key
* @param int $start
* @param int $end
* @return bool|int
*/
public function removeFromSortedSetByRank($key, $start = 0, $end = -1) {
if($end == -1) {
$end = $this->getSortedSetLength($key) - 1;
}
$cmd = 'ZREMRANGEBYRANK ' . $key . ' ' . $start . ' ' . $end;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
}
/**
* Remove all members in a sorted set within the given scores.
* @param $key
* @param $min
* @param $max
* @return bool|int
* @throws Exception
*/
public function removeFromSortedSetByScore($key, $min, $max) {
if($min > $max) {
$error_msg = "ERROR: The argument \$min must be leather than \$max argument.\n";
throw new Exception($error_msg);
}
$cmd = 'ZREMRANGEBYSCORE ' . $key . ' ' . $min . ' ' . $max;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
}
/************ Hash Operations ************/
/**
* Set the string value of a hash field.
* @param $key
* @param $field
* @param $value
* @return bool|string
*/
public function setHash($key, $field, $value) {
if($this->validateEntry($field, $value) && (!empty($key)) && ctype_alnum($key)) {
$cmd = 'HSET ' . $key . ' ' . $field . ' ' . $value;
$response = $this->executeCommand($cmd);
return $this->getError($response);
} else {
return false;
}
}
/**
* Set the value of a hash field, only if the field does not exist.
* @param $key
* @param $field
* @param $value
* @return bool|string
*/
public function setHashIfFieldNotExist($key, $field, $value) {
if($this->validateEntry($field, $value) && (!empty($key)) && ctype_alnum($key)) {
$cmd = 'HSETNX ' . $key . ' ' . $field . ' ' . $value;
$response = $this->executeCommand($cmd);
return $this->getError($response);
} else {
return false;
}
}
/**
* Set multiple hash fields to multiple values.
* The argument $assoc_array have the form [field => value].
* @param $key
* @param array $assoc_array
* @return bool|int
*/
public function setHashMultiFields($key, $assoc_array) {
if(is_array($assoc_array)) {
$cmd = 'HMSET ' . $key . ' ';
foreach ($assoc_array as $field => $value) {
$cmd .= $field . ' ' . $value . ' ';
}
$cmd = rtrim($cmd);
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
} else {
return false;
}
}
/**
* Get the number of fields in a hash.
* @param $key
* @return bool|int
*/
public function getHashLength($key) {
$cmd = "HLEN " . $key;
$response = $this->executeCommand($cmd);
if ($this->getError($response)) {
return false;
}
return (int)substr($response, 1);
}
/**
* Determine if a hash field exists.
* @param $key
* @param $field
* @return bool
*/
public function existsHashField($key, $field) {
$cmd = 'HEXISTS ' . $key . ' ' . $field;