forked from e-oz/Memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedisServer.php
1451 lines (1330 loc) · 38.1 KB
/
RedisServer.php
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
namespace Jamm\Memory;
/**
* @method mixed DEBUG_OBJECT($key) Get debugging information about a key
* @method mixed DEBUG_SEGFAULT() Make the server crash
* @method string ECHO($message) Echo the given string
* @method string PING() Ping the server
* @method int LASTSAVE() Get the UNIX time stamp of the last successful save to disk Ping the server
* @method mixed MONITOR() Listen for all requests received by the server in real time
* @method mixed OBJECT($subcommand) Inspect the internals of Redis objects
* @method mixed RANDOMKEY() Return a random key from the keyspace
* @method mixed SAVE() Synchronously save the dataset to disk
* @method mixed SHUTDOWN() Synchronously save the dataset to disk and then shut down the server
* @method mixed SLOWLOG($subcommand) Manages the Redis slow queries log
* @method string SPOP(string $key) Remove and return a random member from a set
* @method string SRANDMEMBER(string $key) Get a random member from a set
* @method string SYNC() Internal command used for replication
*/
class RedisServer extends MemoryObject implements IRedisServer
{
protected $connection;
public function __construct($host = 'localhost', $port = '6379')
{
$this->connection = $this->connect($host, $port);
}
protected function connect($host, $port)
{
$socket = fsockopen($host, $port, $errno, $errstr);
if (!$socket) return $this->ReportError('Connection error: '.$errno.':'.$errstr, __LINE__);
return $socket;
}
/**
* Execute send_command and return the result
* Each entity of the send_command should be passed as argument
* Example:
* send_command('set','key','example value');
* or:
* send_command('multi');
* send_command('set','a', serialize($arr));
* send_command('set','b', 1);
* send_command('execute');
* @return array|bool|int|null|string
*/
public function send_command()
{
$args = func_get_args();
return $this->_send($args);
}
protected function _send($args)
{
$command = '*'.count($args)."\r\n";
foreach ($args as $arg) $command .= "$".strlen($arg)."\r\n".$arg."\r\n";
$w = fwrite($this->connection, $command);
if (!$w) return $this->ReportError('command was not sent', __LINE__);
return $this->read_reply();
}
/* If some command is not wrapped... */
public function __call($name, $args)
{
array_unshift($args, str_replace('_', ' ', $name));
return $this->_send($args);
}
public function read_reply()
{
$reply = trim(fgets($this->connection));
$response = null;
/**
* Thanks to Justin Poliey for original code of parsing the answer
* https://github.com/jdp
* Error was fixed there: https://github.com/jamm/redisent
*/
switch (substr($reply, 0, 1))
{
/* Error reply */
case '-':
return $this->ReportError('error: '.$reply, __LINE__);
break;
/* Inline reply */
case '+':
return substr(trim($reply), 1);
break;
/* Bulk reply */
case '$':
$response = null;
if ($reply=='$-1') return null;
$read = 0;
$size = intval(substr($reply, 1));
$chi = 0;
if ($size > 0)
{
do
{
$chi++;
$block_size = $size-$read;
if ($block_size > 1024) $block_size = 1024;
if ($block_size < 1) break;
if ($chi > 1000) return $this->ReportError('loooop', __LINE__);
$response .= fread($this->connection, $block_size);
$read += $block_size;
} while ($read < $size);
}
fread($this->connection, 2); /* discard crlf */
break;
/* Multi-bulk reply */
case '*':
$count = substr($reply, 1);
if ($count=='-1') return null;
$response = array();
for ($i = 0; $i < $count; $i++)
{
$response[] = $this->read_reply();
}
break;
/* Integer reply */
case ':':
return intval(substr(trim($reply), 1));
break;
default:
return $this->ReportError('unkown answer: '.$reply, __LINE__);
break;
}
return $response;
}
public function get($key)
{
return $this->send_command('get', $key);
}
public function set($key, $value)
{
return $this->send_command('set', $key, $value);
}
public function SetEx($key, $seconds, $value)
{
return $this->send_command('setex', $key, $seconds, $value);
}
public function Keys($pattern)
{
return $this->send_command('keys', $pattern);
}
public function multi()
{
$this->send_command('multi');
return $this;
}
public function sAdd($set, $value)
{
return $this->send_command('sadd', $set, $value);
}
public function sMembers($set)
{
return $this->send_command('smembers', $set);
}
public function hSet($hash, $field, $value)
{
return $this->send_command('hset', $hash, $field, $value);
}
public function hGetAll($hash)
{
$arr = $this->send_command('hgetall', $hash);
$c = count($arr);
$r = array();
for ($i = 0; $i < $c; $i += 2)
{
$r[$arr[$i]] = $arr[$i+1];
}
return $r;
}
public function FlushDB()
{
return $this->send_command('flushdb');
}
public function info()
{
return $this->send_command('info');
}
public function __destruct()
{
if (!empty($this->connection)) fclose($this->connection);
}
/**
* Set the value of a key, only if the key does not exist
* @param string $key
* @param string $value
*/
public function SetNX($key, $value)
{
return $this->send_command('setnx', $key, $value);
}
/**
* Marks the given keys to be watched for conditional execution of a transaction
* each argument is a key:
* watch('key1', 'key2', 'key3', ...)
*/
public function watch()
{
$args = func_get_args();
array_unshift($args, 'watch');
return call_user_func_array(array($this, 'send_command'), $args);
}
/**
* Executes all previously queued commands in a transaction and restores the connection state to normal.
* When using WATCH, EXEC will execute commands only if the watched keys were not modified, allowing for a check-and-set mechanism.
*/
public function exec()
{
return $this->send_command('exec');
}
/**
* Flushes all previously queued commands in a transaction and restores the connection state to normal.
* If WATCH was used, DISCARD unwatches all keys.
*/
public function discard()
{
return $this->send_command('discard');
}
/**
* Returns if value is a member of the set.
* @param string $set
* @param string $value
* @return boolean
*/
public function sIsMember($set, $value)
{
return $this->send_command('sismember', $set, $value);
}
/**
* Remove member from the set. If 'value' is not a member of this set, no operation is performed.
* An error is returned when the value stored at key is not a set.
* @param string $set
* @param string $value
* @return boolean
*/
public function sRem($set, $value)
{
return $this->send_command('srem', $set, $value);
}
public function Expire($key, $seconds)
{
return $this->send_command('expire', $key, $seconds);
}
public function TTL($key)
{
return $this->send_command('ttl', $key);
}
/**
* Delete a key
* Parameters: $key1, $key2, ...
* or: array($key1, $key2, ...)
* @param string $key
* @return int
*/
public function Del($key)
{
if (!is_array($key)) $key = func_get_args();
return $this->__call('del', $key);
}
/**
* Increment the integer value of a key by the given number
* @param string $key
* @param int $increment
* @return int
*/
public function IncrBy($key, $increment)
{
return $this->send_command('incrby', $key, $increment);
}
/**
* Append a value to a key
* @param string $key
* @param string $value
* @return int
*/
public function Append($key, $value)
{
return $this->send_command('append', $key, $value);
}
/**
* Request for authentication in a password protected Redis server.
* @param string $pasword
* @return boolean
*/
public function Auth($pasword)
{
return $this->send_command('Auth', $pasword);
}
/** Rewrites the append-only file to reflect the current dataset in memory. */
public function bgRewriteAOF()
{
return $this->send_command('bgRewriteAOF');
}
/** Asynchronously save the dataset to disk */
public function bgSave()
{
return $this->send_command('bgSave');
}
/**
* Remove and get the first element in a list, or block until one is available
* Parameters format:
* key1,key2,key3,...,keyN,timeout
* or
* array(key1,key2,keyN), timeout
* @param string|array $keys
* @param int $timeout - time of waiting
*/
public function BLPop($keys, $timeout)
{
if (!is_array($keys)) $keys = func_get_args();
else array_push($keys, $timeout);
return $this->__call('BLPop', $keys);
}
/**
* Remove and get the last element in a list, or block until one is available
* Parameters format:
* key1,key2,key3,...,keyN,timeout
* or
* array(key1,key2,keyN), timeout
* @param string|array $keys
* @param int $timeout - time of waiting
*/
public function BRPop($keys, $timeout)
{
if (!is_array($keys)) $keys = func_get_args();
else array_push($keys, $timeout);
return $this->__call('BRPop', $keys);
}
/**
* Pop a value from a list, push it to another list and return it; or block until one is available
* @param string $source
* @param string $destination
* @param int $timeout
* @return string|boolean
*/
public function BRPopLPush($source, $destination, $timeout)
{
return $this->send_command('BRPopLPush', $source, $destination, $timeout);
}
/**
* Get the value of a configuration parameter
* @param string $pattern
* @return string
*/
public function Config_Get($pattern)
{
return $this->send_command('Config_Get', $pattern);
}
/**
* Set the value of a configuration parameter
* @param $parameter
* @param $value
* @return boolean
*/
public function Config_Set($parameter, $value)
{
return $this->send_command('Config_Set', $parameter, $value);
}
/**
* Resets the statistics reported by Redis using the INFO command.
* These are the counters that are reset:
* Keyspace hits
* Keyspace misses
* Number of commands processed
* Number of connections received
* Number of expired keys
*/
public function Config_ResetStat()
{
return $this->send_command('Config_ResetStat');
}
/**
* Return the number of keys in the selected database
* @return int
*/
public function DBsize()
{
return $this->send_command('DBsize');
}
/**
* Decrement the integer value of a key by one
* @param string $key
* @return int
*/
public function Decr($key)
{
return $this->send_command('Decr', $key);
}
/**
* Decrement the integer value of a key by the given number
* @param string $key
* @param int $decrement
* @return int
*/
public function DecrBy($key, $decrement)
{
return $this->send_command('DecrBy', $key, $decrement);
}
/**
* Determine if a key exists
* @param string $key
* @return int
*/
public function Exists($key)
{
return $this->send_command('Exists', $key);
}
/**
* Set the expiration for a key as a UNIX timestamp
* @param string $key
* @param int $timestamp
* @return int
*/
public function Expireat($key, $timestamp)
{
return $this->send_command('Expireat', $key, $timestamp);
}
/** Remove all keys from all databases */
public function FlushAll()
{
return $this->send_command('FlushAll');
}
/**
* Returns the bit value at offset in the string value stored at key
* @param string $key
* @param int $offset
*/
public function GetBit($key, $offset)
{
return $this->send_command('GetBit', $key, $offset);
}
/**
* Get a substring of the string stored at a key
* @param string $key
* @param int $start
* @param int $end
* @return string
*/
public function GetRange($key, $start, $end)
{
return $this->send_command('GetRange', $key, $start, $end);
}
/**
* Atomically sets key to value and returns the old value stored at key. Returns an error when key exists but does not hold a string value.
* From time to time we need to get the value of the counter and reset it to zero atomically. This can be done using GETSET mycounter "0".
* @param string $key
* @param string $value
* @return string
*/
public function GetSet($key, $value)
{
return $this->send_command('GetSet', $key, $value);
}
/**
* Removes the specified fields from the hash stored at key.
* Non-existing fields are ignored. Non-existing keys are treated as empty hashes and this command returns 0.
* Parameters: ($key, $field1, $field2...)
* or: ($key, array($field1,$field2...))
* @param $key
* @param array|string $field
* @return int
*/
public function hDel($key, $field)
{
if (is_array($field)) array_unshift($field, $key);
else $field = array($key, $field);
return $this->__call('hDel', $field);
}
/**
* Determine if a hash field exists
* @param string $key
* @param string $field
* @return int
*/
public function hExists($key, $field)
{
return $this->send_command('hExists', $key, $field);
}
/**
* Get the value of a hash field
* @param string $key
* @param string $field
* @return string|int
*/
public function hGet($key, $field)
{
return $this->send_command('hGet', $key, $field);
}
/**
* Increments the number stored at field in the hash stored at key by increment.
* If key does not exist, a new key holding a hash is created.
* If field does not exist or holds a string that cannot be interpreted as integer, the value is set to 0 before the operation is performed.
* Returns the value at field after the increment operation.
* @param string $key
* @param string $field
* @param int $increment
* @return int
*/
public function hIncrBy($key, $field, $increment)
{
return $this->send_command('hIncrBy', $key, $field, $increment);
}
/**
* Get all the fields in a hash
* @param string $key name of hash
* @return array
*/
public function hKeys($key)
{
return $this->send_command('hKeys', $key);
}
/**
* Get the number of fields in a hash
* @param string $key
* @return int
*/
public function hLen($key)
{
return $this->send_command('hLen', $key);
}
/**
* Returns the values associated with the specified fields in the hash stored at key.
* For every field that does not exist in the hash, a nil value is returned.
* @param string $key
* @param array $fields
* @return array
*/
public function hMGet($key, array $fields)
{
array_unshift($fields, $key);
return $this->__call('hMGet', $fields);
}
/**
* Set multiple hash fields to multiple values
* @param string $key
* @param array $fields (field => value)
*/
public function hMSet($key, $fields)
{
$args[] = $key;
foreach ($fields as $field => $value)
{
$args[] = $field;
$args[] = $value;
}
return $this->__call('hMSet', $args);
}
/**
* Set the value of a hash field, only if the field does not exist
* @param string $key
* @param string $field
* @param string $value
* @return int
*/
public function hSetNX($key, $field, $value)
{
return $this->send_command('hSetNX', $key, $field, $value);
}
/**
* Get all the values in a hash
* @param string $key
* @return array
*/
public function hVals($key)
{
return $this->send_command('hVals', $key);
}
/**
* Increment the integer value of a key by one
* @param string $key
* @return int
*/
public function Incr($key)
{
return $this->send_command('Incr', $key);
}
/**
* Returns the element at index $index in the list stored at $key.
* The index is zero-based, so 0 means the first element, 1 the second element and so on.
* Negative indices can be used to designate elements starting at the tail of the list.
* Here, -1 means the last element, -2 means the penultimate and so forth.
* When the value at key is not a list, an error is returned.
* @param string $key
* @param int $index
* @return string|boolean
*/
public function LIndex($key, $index)
{
return $this->send_command('LIndex', $key, $index);
}
/**
* Insert an element before or after another element in a list
* @param string $key
* @param string $position see Position* constants
* @param string $pivot
* @param string $value
* @return int
*/
public function LInsert($key, $position, $pivot, $value)
{
return $this->send_command('LInsert', $key, $position, $pivot, $value);
}
/**
* Get the length of a list
* @param string $key
* @return int
*/
public function LLen($key)
{
return $this->send_command('LLen', $key);
}
/**
* Remove and get the first element in a list
* @param string $key
* @return string|boolean
*/
public function LPop($key)
{
return $this->send_command('LPop', $key);
}
/**
* Inserts value at the head of the list stored at key.
* If key does not exist, it is created as empty list before performing the push operation.
* When key holds a value that is not a list, an error is returned.
* @param string $key
* @param string $value
* @return int
*/
public function LPush($key, $value)
{
return $this->send_command('LPush', $key, $value);
}
/**
* Inserts value at the head of the list stored at key, only if key already exists and holds a list.
* In contrary to LPush, no operation will be performed when key does not yet exist.
* @param string $key
* @param string $value
* @return int
*/
public function LPushX($key, $value)
{
return $this->send_command('LPushX', $key, $value);
}
/**
* Returns the specified elements of the list stored at key.
* The offsets $start and $stop are zero-based indexes, with 0 being the first element of the list (the head of the list),
* 1 being the next element and so on.
* These offsets can also be negative numbers indicating offsets starting at the end of the list.
* For example, -1 is the last element of the list, -2 the penultimate, and so on.
* @param string $key
* @param int $start
* @param int $stop
* @return array
*/
public function LRange($key, $start, $stop)
{
return $this->send_command('LRange', $key, $start, $stop);
}
/**
* Removes the first count occurrences of elements equal to value from the list stored at key.
* The count argument influences the operation in the following ways:
* count > 0: Remove elements equal to value moving from head to tail.
* count < 0: Remove elements equal to value moving from tail to head.
* count = 0: Remove all elements equal to value.
* For example, LREM list -2 "hello" will remove the last two occurrences of "hello" in the list stored at list.
* @param string $key
* @param int $count
* @param string $value
* @return int
*/
public function LRem($key, $count, $value)
{
return $this->send_command('LRem', $key, $count, $value);
}
/**
* Sets the list element at index to value.
* For more information on the index argument, see LINDEX.
* An error is returned for out of range indexes.
* @param $key
* @param $index
* @param $value
* @return boolean
*/
public function LSet($key, $index, $value)
{
return $this->send_command('LSet', $key, $index, $value);
}
/**
* Trim a list to the specified range
* @link http://redis.io/commands/ltrim
* @param string $key
* @param int $start
* @param int $stop
* @return boolean
*/
public function LTrim($key, $start, $stop)
{
return $this->send_command('LTrim', $key, $start, $stop);
}
/**
* Returns the values of all specified keys.
* For every key that does not hold a string value or does not exist, the special value nil is returned.
* Parameters: $key, [key ...]
* or: array($key1, $key2...)
* @param string $key
* @return array
*/
public function MGet($key)
{
if (!is_array($key)) $key = func_get_args();
return $this->__call('MGet', $key);
}
/**
* Move key from the currently selected database (see SELECT) to the specified destination database.
* When key already exists in the destination database, or it does not exist in the source database, it does nothing.
* It is possible to use MOVE as a locking primitive because of this.
* @param string $key
* @param int $db
* @return int
*/
public function Move($key, $db)
{
return $this->send_command('Move', $key, $db);
}
/**
* Set multiple keys to multiple values
* @param array $keys (key => value)
* @return string
*/
public function MSet(array $keys)
{
return $this->__call('MSet', $keys);
}
/**
* Set multiple keys to multiple values, only if none of the keys exist
* @param array $keys (key => value)
* Returns:
* 1 if the all the keys were set.
* 0 if no key was set (at least one key already existed).
* @return int
*/
public function MSetNX(array $keys)
{
return $this->__call('MSetNX', $keys);
}
/**
* Remove the expiration from a key
* @param string $key
* @return int
*/
public function Persist($key)
{
return $this->send_command('Persist', $key);
}
/**
* Subscribes the client to the given patterns.
* @param string $pattern
* @return void
*/
public function PSubscribe($pattern)
{
$this->send_command('PSubscribe', $pattern);
}
/**
* Post a message to a channel
* Returns the number of clients that received the message.
* @param string $channel
* @param string $message
* @return int
*/
public function Publish($channel, $message)
{
return $this->send_command('Publish', $channel, $message);
}
/**
* Stop listening for messages posted to channels matching the given patterns
* @param array|string|null $patterns
* @return int
*/
public function PUnsubscribe($patterns = null)
{
if (!empty($patterns))
{
if (!is_array($patterns)) $patterns = array($patterns);
return $this->__call('PUnsubscribe', $patterns);
}
else return $this->send_command('PUnsubscribe');
}
/** Close the connection */
public function Quit()
{
return $this->send_command('Quit');
}
/**
* Renames key to newkey.
* It returns an error when the source and destination names are the same, or when key does not exist.
* If newkey already exists it is overwritten.
* @param string $key
* @param string $newkey
* @return boolean
*/
public function Rename($key, $newkey)
{
return $this->send_command('Rename', $key, $newkey);
}
/**
* Rename a key, only if the new key does not exist
* @param string $key
* @param string $newkey
* @return int
*/
public function RenameNX($key, $newkey)
{
return $this->send_command('RenameNX', $key, $newkey);
}
/**
* Removes and returns the last element of the list stored at key.
* @param string $key
* @return string|boolean
*/
public function RPop($key)
{
return $this->send_command('RPop', $key);
}
/**
* Atomically returns and removes the last element (tail) of the list stored at source,
* and pushes the element at the first element (head) of the list stored at destination.
* If source does not exist, the value nil is returned and no operation is performed.
* @param string $source
* @param string $destination
* @return string
*/
public function RPopLPush($source, $destination)
{
return $this->send_command('RPopLPush', $source, $destination);
}
/**
* Inserts value at the tail of the list stored at key.
* If key does not exist, it is created as empty list before performing the push operation.
* When key holds a value that is not a list, an error is returned.
* Parameters: key value [value ...]
* or: key, array(value,value,...)
* @param string $key
* @param string|array $value
* @return int|boolean
*/
public function RPush($key, $value)
{
if (!is_array($value)) $value = func_get_args();
else array_unshift($value, $key);
return $this->__call('RPush', $value);
}
/**
* Append a value to a list, only if the list exists
* @param string $key
* @param string $value
* @return int
*/
public function RPushX($key, $value)
{
return $this->send_command('RPushX', $key, $value);
}
/**
* Get the number of members in a set
* @param string $key
* @return int
*/
public function sCard($key)
{
return $this->send_command('sCard', $key);
}
/**
* Returns the members of the set resulting from the difference between the first set and all the successive sets.
* For example:
* key1 = {a,b,c,d}
* key2 = {c}
* key3 = {a,c,e}
* SDIFF key1 key2 key3 = {b,d}
* Keys that do not exist are considered to be empty sets.
*
* Parameters: key1, key2, key3...
* @param string|array $key
* @return array
*/
public function sDiff($key)
{
if (!is_array($key)) $key = func_get_args();
return $this->__call('sDiff', $key);
}
/**
* This command is equal to SDIFF, but instead of returning the resulting set, it is stored in destination.
* If destination already exists, it is overwritten.
* Returns the number of elements in the resulting set.
* Parameters: destination, key [key, ...]
* or: destination, array(key,key, ...)
* @param string $destination
* @param string|array $key
* @return int
*/
public function sDiffStore($destination, $key)
{
if (!is_array($key)) $key = func_get_args();
else array_unshift($key, $destination);
return $this->__call('sDiffStore', $key);
}
/**
* Select the DB with having the specified zero-based numeric index. New connections always use DB 0.
* @param int $index
* @return void
*/
public function Select($index)
{