-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathredis.class.php
More file actions
1589 lines (1474 loc) · 60.1 KB
/
redis.class.php
File metadata and controls
1589 lines (1474 loc) · 60.1 KB
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
/**
* php redis客户端,采用sentinel方式部署redis
* key分配算法采用hash一致性算法
*
* 2015.3.12 18:22
* @author enze.wei <[enzewei@gmail.com]>
*/
/**
* 使用lib_redis实现php的redis client,分布式采用哈希一致性
*
* 同时实现环境选择接口
*
* @example
*
* $redis = new RedisDB(group); //master
* $redisRead = new RedisDB(group); //slave
*/
class RedisDB {
const TIME_OUT = 3;
/*
* 300 second
*/
const EXPIRE = 3000;
private $_redis = null;
private $_sentinel = null;
private $_conn = array();
private $_servers = array();
private $_config = array();
private $_group = null;
public function __construct($group) {
$this->_init($group);
}
private function _init($group) {
if (true === empty($this->_config)) {
$config = Loader::loadConfig('redis');
$this->_config = $config[$group];
}
if (null === $this->_sentinel) {
$this->_sentinel = new SentinelMonitor;
}
if (true === empty($this->_sentinel->servers)) {
$this->_sentinel->addServers($this->_config);
}
$this->_group = $group;
}
//1 slave 2 master
private function _connect($method = 1) {
switch ($method) {
case 2:
if (true === empty($this->_conn[$this->_group]['master'])) {
$host = $this->_sentinel->getMasterServer();
$this->_conn[$this->_group]['master'] = new Redis;
$this->_conn[$this->_group]['master']->connect($host[0], $host[1], self::TIME_OUT);
$this->_servers['host'] = $host[0];
$this->_servers['port'] = $host[1];
$this->_conn[$this->_group]['master']->auth($host[2]);
$this->_conn[$this->_group]['master']->select($host[3]);
}
break;
case 1:
default:
if (true === empty($this->_conn[$this->_group]['slave'])) {
$host = $this->_sentinel->getSlaveServer();
$this->_conn[$this->_group]['slave'] = new Redis;
$this->_conn[$this->_group]['slave']->connect($host[0], $host[1], self::TIME_OUT);
$this->_servers['host'] = $host[0];
$this->_servers['port'] = $host[1];
$this->_conn[$this->_group]['slave']->auth($host[2]);
$this->_conn[$this->_group]['slave']->select($host[3]);
}
break;
}
}
private function _encode($data) {
return json_encode($data);
}
private function _decode($data) {
return json_decode($data, true);
}
private function _write($key) {
$this->_sentinel->key = $key;
if (true === empty($this->_conn)) {
$this->_connect(2);
}
}
private function _read($key) {
$this->_sentinel->key = $key;
if (true === empty($this->_conn)) {
$this->_connect();
}
}
/********************************* Redis Command *************************/
/****** about keys *******/
/**
* 删除一个key
* @param string $key
* @return boolean [true|false]
*/
public function del($key) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$result = $this->_conn[$this->_group]['master']->del($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('del key:{' . $key . '} return {' . (int) $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 获取指定key的存活周期,单位毫秒
* @param string $key
* @return long [-1 no ttl|-2 key doesn't exist|numric]
*/
public function pttl($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$result = $this->_conn[$this->_group]['slave']->pttl($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('pttl key:{' . $key . '} return {' . $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 获取指定key的存活周期,单位秒
* @param string $key
* @return long [-1 no ttl|-2 key doesn't exist|numric]
*/
public function ttl($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$result = $this->_conn[$this->_group]['slave']->ttl($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('ttl key:{' . $key . '} return {' . $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 检察某一个key是否存在
* @param string $key
* @return boolean [true|false]
*/
public function exists($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$result = $this->_conn[$this->_group]['slave']->exists($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('exists key:{' . $key . '} return {' . $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 返回key所存储的值的类型
* @param string $key
* @return int [none|string|list|set|zset|hash]
*
* string: Redis::REDIS_STRING 1
* set: Redis::REDIS_SET 2
* list: Redis::REDIS_LIST 3
* zset: Redis::REDIS_ZSET 4
* hash: Redis::REDIS_HASH 5
* other: Redis::REDIS_NOT_FOUND 0
*/
public function type($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$result = $this->_conn[$this->_group]['slave']->type($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('type key:{' . $key . '} return {' . $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 设置key的过期时间。
* 如果key已过期,将会被自动删除。设置了过期时间的key被称之为volatile。
* 在key过期之前可以重新更新他的过期时间,也可以使用PERSIST命令删除key的过期时间。
* @param string $key
* @param integer $expire
* @return boolean
*/
public function expire($key, $expire = 0) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$expire = intval($expire);
$result = $this->_conn[$this->_group]['master']->expire($key, $expire);
$endTime = RpcLog::getMicroTime();
RpcLog::log('expire key:{' . $key . '} expire {' . $expire . '} return {' . $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 移除一个key的有效期,但并不删除该key,使之永不失效
* @param string $key
* @return boolean [true|false]
*/
public function persist($key) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$result = $this->_conn[$this->_group]['master']->persist($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('persist key:{' . $key . '} return {' . $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 设置key的过期时间。 时间戳
* 如果key已过期,将会被自动删除。设置了过期时间的key被称之为volatile。
* 在key过期之前可以重新更新他的过期时间,也可以使用PERSIST命令删除key的过期时间。
* @param string $key
* @param integer $timestamp
* @return boolean
*/
public function expireat($key, $timestamp = 0) {
$startTime = RpcLog::getMicroTime();
if (true === empty($timestamp)) {
$timestamp = time() + self::EXPIRE;
}
$this->_write($key);
$result = $this->_conn[$this->_group]['master']->expireat($key, $timestamp);
$endTime = RpcLog::getMicroTime();
RpcLog::log('expireat key:{' . $key . '} timestamp {' . $timestamp . '} return {' . $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 设置key的过期时间。毫秒
* 如果key已过期,将会被自动删除。设置了过期时间的key被称之为volatile。
* 在key过期之前可以重新更新他的过期时间,也可以使用PERSIST命令删除key的过期时间。
* @param string $key
* @param integer $expire
* @return boolean
*/
public function pexpire($key, $expire = 0) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$expire = intval($expire);
$result = $this->_conn[$this->_group]['master']->pexpire($key, $expire);
$endTime = RpcLog::getMicroTime();
RpcLog::log('pexpire key:{' . $key . '} expire {' . $expire . '} return {' . $result . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $result;
}
/**
* 排序
*
* 该处推荐使用场景如下:
* 1、简单的数字、字母的value,并且value是单一的值,不是序列化的字串等形式,直接sort加排序方式即可,根据需要可增加alpha、limit修饰
* 2、有序集合,可根据by进行排序。
* 其他场景不推荐使用,如根据by进行外部key的权重排序,因为外部key不一定与当前的server是同一个server
* 因此推荐列表页的数据都存储到有序集合中,然后针对该集合排序
*
* @param string $key
* @param array $option 配置参数,格式参考示例
* @return array
*
* @example
* $option = array(
* 'by' => 'field', //需要排序的字段
* 'limit' => array(startIndex, offset), //起始位置,一次取多少条数据,与mysql中的limit类似
* 'sort' => 'asc|desc',
* 'alpha' => true|false, //是否采用alpha排序
* 'store' => 'new_key', //排序结果存储到指定的key中,alert,当前模式下,不能保证在read的时候会选择存储的机器,因此不推荐该方式
* );
*/
public function sort($key, $option = array()) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
if (true === empty($option)) {
$result = $this->_conn[$this->_group]['master']->sort($key);
$command = 'sort use default key:{' . $key . '}';
} else {
$result = $this->_conn[$this->_group]['master']->sort($key, $option);
$command = 'sort use option key:{' . $key . '} option:{' . json_encode($option) . '}';
}
$endTime = RpcLog::getMicroTime();
RpcLog::log($command . ' return {' . json_encode($result) . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
if (true === is_array($result)) {
foreach ($result as $key => $value) {
$result[$key] = $this->_decode($value);
}
}
return $result;
}
/**
* 移动一个key到另一个数据库
* 当使用过该方法后,以后的查询必须做selectDb操作
*
* @param string $key
* @param integer $dbNumber db数量,最大不超过配置的数量,默认是15 (从0开始)
* @return boolean
*/
public function move($key, $dbNumber = 1) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$res = $this->_conn[$this->_group]['master']->move($key, $dbNumber);
$endTime = RpcLog::getMicroTime();
RpcLog::log('move key:{' . $key . '} dbnumber:{' . $dbNumber . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/****** about strings *******/
/**
* 根据key获取值
* @param string $key
* @return mix
*/
public function get($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$data = $this->_conn[$this->_group]['slave']->get($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('get key:{' . $key . '} value:{' . $data . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
$data = $this->_decode($data);
return $data;
}
/**
* 设置key的值
* 第三个参数有几种用法,这里贴出在php的redis扩展中C代码是怎么实现的
*
* @param [type] $key [description]
* @param [type] $data [description]
* @param mix $option [empty|numeric|array] 该参数分别对应几种设置方法 [NX以及XX暂不支持,有问题]
*
* redis 2.6.12 support extend third args (array)
*
* @tips php_method set的c代码实现
* if(exp_type && set_type) {
* //SET <key> <value> NX|XX PX|EX <timeout>
* cmd_len = redis_cmd_format_static(&cmd, "SET", "ssssl", key, key_len, * val, val_len, set_type, 2, exp_type, * 2, expire);
* } else if(exp_type) {
* //SET <key> <value> PX|EX <timeout>
* cmd_len = redis_cmd_format_static(&cmd, "SET", "sssl", key, key_len, * val, val_len, exp_type, 2, expire);
* } else if(set_type) {
* //SET <key> <value> NX|XX
* cmd_len = redis_cmd_format_static(&cmd, "SET", "sss", key, key_len, * val, val_len, set_type, 2);
* } else if(expire > 0) {
* //Backward compatible SETEX redirection
* cmd_len = redis_cmd_format_static(&cmd, "SETEX", "sls", key, key_len, * expire, val, val_len);
* } else {
* //SET <key> <value>
* cmd_len = redis_cmd_format_static(&cmd, "SET", "ss", key, key_len, * val, val_len);
* }
*/
public function set($key, $data, $option = array()) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
if (true === empty($option)) {
/*
* default
*/
$res = $this->_conn[$this->_group]['master']->set($key, $data);
$command = 'set';
} else if (true === is_numeric($option)) {
/*
* use setex default second
*/
$res = $this->_conn[$this->_group]['master']->set($key, $data, (int) $option);
$command = 'set expire:' . $option . 's';
} else if (true === is_array($option)) {
/*
* option array(
* 'type' => 'NX|XX|PX|EX',
* 'expire' => array('PX|EX' => 1000)|1000,
* )
*/
try {
switch (strtoupper($option['type'])) {
/*
case 'NX' :
if (true === empty($option['expire']) || false === is_array($option['expire'])) {
$option['expire'] = array('EX' => self::EXPIRE);
}
$res = $this->_conn[$this->_group]['master']->set($key, $data, (array('NX') + $option['expire']));
$command = '(NX) set use' . json_encode($option['expire']);
break;
case 'XX' :
if (true === empty($option['expire']) || false === is_array($option['expire'])) {
$option['expire'] = array('EX' => self::EXPIRE);
}
$res = $this->_conn[$this->_group]['master']->set($key, $data, (array('XX') + $option['expire']));
debug((array('XX') + $option['expire']));
$command = '(xx) set use' . json_encode($option['expire']);
break;
*/
case 'PX' :
if (true === empty($option['expire'])) {
$option['expire'] = self::EXPIRE;
}
$res = $this->_conn[$this->_group]['master']->psetex($key, (int) $option['expire'], $data);
$command = '(px) set use psetex expire:' . $option['expire'] . 'ms';
break;
case 'EX' :
default :
if (true === empty($option['expire'])) {
$option['expire'] = self::EXPIRE;
}
debug($option);
$res = $this->_conn[$this->_group]['master']->set($key, $data, (int) $option['expire']);
$command = '(ex) set expire ' . $option['expire'] . 's';
break;
}
} catch (Exception $e) {
throw $e;
}
} else {
$res = $this->_conn[$this->_group]['master']->set($key, $data, self::EXPIRE);
$command = '(default) set';
}
$endTime = RpcLog::getMicroTime();
RpcLog::log($command . ' key:{' . $key . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 以秒设置expire
* @param string $key
* @param mix $data
* @param integer $expire 生命周期
* @return boolean
*/
public function setex($key, $data, $expire = 0) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->setex($key, (int) $expire, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('setex key:{' . $key . '} value:{' . $data . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 以毫秒设置expire
* @param string $key
* @param mix $data
* @param integer $expire 生命周期
* @return boolean
*/
public function psetex($key, $data, $expire = 0) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->psetex($key, (int) $expire, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('psetex key:{' . $key . '} value:{' . $data . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 追加一个值到指定的key
* @param string $key
* @param mix $data
* @return integer 当前key的值对应的长度
*/
public function append($key, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->append($key, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('append key:{' . $key . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* SET if Not Exists
* 如果key不存在,就设置key对应字符串value。在这种情况下,该命令和SET一样。当key已经存在时,就不做任何操作。
*
* @param string $key
* @param mix $data
* @return boolean
*/
public function setnx($key, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->setnx($key, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('setnx key:{' . $key . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 自动将key对应到value并且返回原来key对应的value。如果key存在但是对应的value不是字符串,就返回错误。
*
* @tips GETSET可以和INCR一起使用实现支持重置的计数功能。举个例子:每当有事件发生的时候,
* 一段程序都会调用INCR给key mycounter加1,但是有时我们需要获取计数器的值,并且自动将其重置为0。
* 这可以通过GETSET mycounter "0"来实现
*
* @param string $key
* @param mix $data
* @return mix
*/
public function getset($key, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
if (false === is_numeric($data)) {
$data = $this->_encode($data);
} else {
$data = intval($data);
}
$res = $this->_conn[$this->_group]['master']->getset($key, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('getset key:{' . $key . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 对key对应的数字做减1操作。
*
* @tips 如果key不存在,那么在操作之前,这个key对应的值会被置为0。
* 如果key有一个错误类型的value或者是一个不能表示成数字的字符串,
* 就返回错误。这个操作最大支持在64位有符号的整型数字。
*
* @param string $key
* @param int [$offset]
* @return int 减小之后的value
*/
public function decr($key, $offset = 1) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$offset = intval($offset);
if (1 != $offset) {
$res = $this->_conn[$this->_group]['master']->decrBy($key, $offset);
} else {
$res = $this->_conn[$this->_group]['master']->decr($key);
}
$endTime = RpcLog::getMicroTime();
RpcLog::log('decr key:{' . $key . '} offset:{' . $offset . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 对key对应的数字做加1操作。
*
* @tips 如果key不存在,那么在操作之前,这个key对应的值会被置为0。
* 如果key有一个错误类型的value或者是一个不能表示成数字的字符串,
* 就返回错误。这个操作最大支持在64位有符号的整型数字。
*
* @param string $key
* @param int [$offset]
* @return int 减小之后的value
*/
public function incr($key, $offset = 1) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$offset = intval($offset);
if (1 != $offset) {
$res = $this->_conn[$this->_group]['master']->incrBy($key, $offset);
} else {
$res = $this->_conn[$this->_group]['master']->incr($key);
}
$endTime = RpcLog::getMicroTime();
RpcLog::log('incr key:{' . $key . '} offset:{' . $offset . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 对key对应的数字做加float操作。
*
* @param string $key
* @param float [$offset]
* @return int 减小之后的value
*/
public function incrByFloat($key, $offset) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$offset = floatval($offset);
$res = $this->_conn[$this->_group]['master']->incrByFloat($key, $offset);
$endTime = RpcLog::getMicroTime();
RpcLog::log('incrByFloat key:{' . $key . '} offset:{' . $offset . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 获取key对应value的长度
*
* @param string $key
* @return int key对应value的长度
*/
public function strlen($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->strlen($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('strlen key:{' . $key . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/****** about hashs *******/
/**
* 读取哈希里的一个字段的值
* @param string $key
* @param string $field hash中的字段名
* @return mix [false失败]
*/
public function hGet($key, $field) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->hGet($key, $field);
if (false !== $res) {
$res = $this->_decode($res);
}
$endTime = RpcLog::getMicroTime();
RpcLog::log('hget key:{' . $key . '} field:{' . $field . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 判断给定字段是否存在于哈希表中
* @param string $key
* @param string $field hash中的字段名
* @return boolean [true存在|false不存在]
*/
public function hExists($key, $field) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->hExists($key, $field);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hexists key:{' . $key . '} field:{' . $field . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 删除一个或多个哈希字段
* @param string $key
* @param string $field hash中的字段名
* @return boolean [true成功|false失败]
*/
public function hDel($key, $field) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$res = $this->_conn[$this->_group]['master']->hDel($key, $field);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hdel key:{' . $key . '} field:{' . $field . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 设置hash里面一个字段的值
*
* @param string $key
* @param string $field hash中的字段名
* @param mix $data field对应的值
* @return mix [false出错|0值已存在,被替换|1添加成功,并且原先不存在这个值]
*/
public function hSet($key, $field, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->hSet($key, $field, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hset key:{' . $key . '} field:{' . $field . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 设置hash字段值
*
* @param string $key
* @param array $field hash中的字段名
* @param array $data field对应的值
* @return boolean
*/
public function hMSet($key, $field = array(), $data = array()) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
foreach ($data as $k => $v) {
$data[$k] = $this->_encode($v);
}
$option = array_combine($field, $data);
$res = $this->_conn[$this->_group]['master']->hMSet($key, $option);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hmset key:{' . $key . '} field:{' . $this->_encode($field) . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 获取hash里面指定字段的值
*
* @param string $key
* @param array $field hash中的字段名
* @return array
*/
public function hMGet($key, $field = array()) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->hMGet($key, $field);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hMGet key:{' . $key . '} field:{' . $this->_encode($field) . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
foreach ($res as $k => $v) {
if (false !== $v) {
$res[$k] = $this->_decode($v);
}
}
return $res;
}
/**
* 设置hash的一个字段,只有当这个字段不存在时有效
*
* @param string $key
* @param array $field hash中的字段名
* @param array $data field对应的值
* @return boolean
*/
public function hSetNx($key, $field, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->hSetNx($key, $field, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hsetnx key:{' . $key . '} field:{' . $field . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 将哈希集中指定字段的值增加给定的数字
*
* @param string $key
* @param array $field hash中的字段名
* @param integer $offset
* @return mix [false|int]
*/
public function hIncrBy($key, $field, $offset = 1) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$res = $this->_conn[$this->_group]['master']->hIncrBy($key, $field, $offset);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hIncrBy key:{' . $key . '} field:{' . $field . '} offset:{' . $offset . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 将哈希集中指定域的值增加给定的浮点数
*
* @param string $key
* @param array $field hash中的字段名
* @param float $offset
* @return mix [false|float]
*/
public function hIncrByFloat($key, $field, $offset = 1.0) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$res = $this->_conn[$this->_group]['master']->hIncrByFloat($key, $field, $offset);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hIncrByFloat key:{' . $key . '} field:{' . $field . '} offset:{' . $offset . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 获得hash的所有值
*
* @param string $key
* @return array
*/
public function hVals($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->hVals($key);
foreach($res as $k => $v) {
$res[$k] = $this->_decode($v);
}
$endTime = RpcLog::getMicroTime();
RpcLog::log('hVals key:{' . $key . '} return:{' . $this->_encode($res) . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 获得hash的所有field
*
* @param string $key
* @return array
*/
public function hKeys($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->hKeys($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hKeys key:{' . $key . '} return:{' . $this->_encode($res) . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 获得hash的所有field与值
*
* @param string $key
* @return array
*/
public function hGetAll($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->hGetAll($key);
foreach($res as $k => $v) {
$res[$k] = $this->_decode($v);
}
$endTime = RpcLog::getMicroTime();
RpcLog::log('hGetAll key:{' . $key . '} return:{' . $this->_encode($res) . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 获取hash里所有字段的数量
* @param string $key
* @return int
*/
public function hLen($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->hLen($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('hLen key:{' . $key . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/****** about lists *******/
/**
* 将所有指定的值插入到存于 key 的列表的头部。
* 如果 key 不存在,那么在进行 push 操作前会创建一个空列表。 如果 key 对应的值不是一个 list 的话,那么会返回一个错误。
*
* @param string $key
* @param mix $data
* @return int 在 push 操作后的 list 长度。
*/
public function lPush($key, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->lpush($key, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('lpush key:{' . $key . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 将所有指定的值插入到存于 key 的列表的尾部。
* 如果 key 不存在,那么在进行 push 操作前会创建一个空列表。 如果 key 对应的值不是一个 list 的话,那么会返回一个错误。
*
* @param string $key
* @param mix $data
* @return int 在 push 操作后的 list 长度。
*/
public function rPush($key, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->rpush($key, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('rpush key:{' . $key . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 只有当 key 已经存在并且存着一个 list 的时候,在这个 key 下面的 list 的头部插入 value。
* key 不尊在的时候不进行任何操作
*
* @param string $key
* @param mix $data
* @return int 在 push 操作后的 list 长度。| key不存在,则返回0
*/
public function lPushx($key, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->lpushx($key, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('lpushx key:{' . $key . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 将所有指定的值插入到存于 key 的列表的尾部。
* 如果 key 不存在,那么在进行 push 操作前会创建一个空列表。 如果 key 对应的值不是一个 list 的话,那么会返回一个错误。
*
* @param string $key
* @param mix $data
* @return int 在 push 操作后的 list 长度。
*/
public function rPushx($key, $data) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$data = $this->_encode($data);
$res = $this->_conn[$this->_group]['master']->rpushx($key, $data);
$endTime = RpcLog::getMicroTime();
RpcLog::log('rpushx key:{' . $key . '} data:{' . $data . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 弹出 key 对应的 list 的第一个元素。
* @param string $key
* @return mix [false空队列]
*/
public function lPop($key) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$res = $this->_conn[$this->_group]['master']->lpop($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('lpop key:{' . $key . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
if (false !== $res) {
$res = $this->_decode($res);
}
return $res;
}
/**
* 弹出 key 对应的 list 的最后一个元素。
* @param string $key
* @return mix [false空队列]
*/
public function rPop($key) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$res = $this->_conn[$this->_group]['master']->rpop($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('rpop key:{' . $key . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
if (false !== $res) {
$res = $this->_decode($res);
}
return $res;
}
/**
* 弹出 key 对应的 list 的第一个元素。 (阻塞,当前模式下不支持非阻塞)
* @param string $key
* @return mix [false空队列|value]
*/
public function bLpop($key, $timeout = 0) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$res = $this->_conn[$this->_group]['master']->blpop($key, $timeout);
$endTime = RpcLog::getMicroTime();
RpcLog::log('blpop key:{' . $key . '} timeout:{' . $timeout . '} return:{' . $this->_encode($res) . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
if (false !== $res) {
$res = $this->_decode($res[1]);
}
return $res;
}
/**
* 弹出 key 对应的 list 的最后一个元素。 (阻塞,当前模式下不支持非阻塞)
* @param string $key
* @return mix [false空队列|value]
*/
public function bRpop($key, $timeout = 0) {
$startTime = RpcLog::getMicroTime();
$this->_write($key);
$res = $this->_conn[$this->_group]['master']->brpop($key, $timeout);
$endTime = RpcLog::getMicroTime();
RpcLog::log('brpop key:{' . $key . '} timeout:{' . $timeout . '} return:{' . $this->_encode($res) . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
if (false !== $res) {
$res = $this->_decode($res[1]);
}
return $res;
}
/**
* 返回存储在 key 里的list的长度。
* @param string $key
* @return mix [false不是list|int 0空队列]
*/
public function lLen($key) {
$startTime = RpcLog::getMicroTime();
$this->_read($key);
$res = $this->_conn[$this->_group]['slave']->llen($key);
$endTime = RpcLog::getMicroTime();
RpcLog::log('llen key:{' . $key . '} return:{' . $res . '} server:{host:' . $this->_servers['host'] . ' port:' . $this->_servers['port'] . '}', $startTime, $endTime, RpcLogEnvConfig::RPC_LOG_TYPE_REDIS);
return $res;
}
/**
* 从存于 key 的列表里移除前 count 次出现的值为 value 的元素。
* count > 0: 从头往尾移除值为 value 的元素。
* count < 0: 从尾往头移除值为 value 的元素。