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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
|
<?php
// @codingStandardsIgnoreFile It's an external lib and it isn't. Let's not bother.
/**
* Memcached client for PHP.
*
* +---------------------------------------------------------------------------+
* | memcached client, PHP |
* +---------------------------------------------------------------------------+
* | Copyright (c) 2003 Ryan T. Dean <rtdean@cytherianage.net> |
* | All rights reserved. |
* | |
* | Redistribution and use in source and binary forms, with or without |
* | modification, are permitted provided that the following conditions |
* | are met: |
* | |
* | 1. Redistributions of source code must retain the above copyright |
* | notice, this list of conditions and the following disclaimer. |
* | 2. Redistributions in binary form must reproduce the above copyright |
* | notice, this list of conditions and the following disclaimer in the |
* | documentation and/or other materials provided with the distribution. |
* | |
* | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
* +---------------------------------------------------------------------------+
* | Author: Ryan T. Dean <rtdean@cytherianage.net> |
* | Heavily influenced by the Perl memcached client by Brad Fitzpatrick. |
* | Permission granted by Brad Fitzpatrick for relicense of ported Perl |
* | client logic under 2-clause BSD license. |
* +---------------------------------------------------------------------------+
*
* @file
* $TCAnet$
*/
/**
* This is the PHP client for memcached - a distributed memory cache daemon.
* More information is available at http://www.danga.com/memcached/
*
* Usage example:
*
* require_once 'memcached.php';
*
* $mc = new MWMemcached(array(
* 'servers' => array('127.0.0.1:10000',
* array('192.0.0.1:10010', 2),
* '127.0.0.1:10020'),
* 'debug' => false,
* 'compress_threshold' => 10240,
* 'persistent' => true));
*
* $mc->add( 'key', array( 'some', 'array' ) );
* $mc->replace( 'key', 'some random string' );
* $val = $mc->get( 'key' );
*
* @author Ryan T. Dean <rtdean@cytherianage.net>
* @version 0.1.2
*/
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
// {{{ requirements
// }}}
// {{{ class MWMemcached
/**
* memcached client class implemented using (p)fsockopen()
*
* @author Ryan T. Dean <rtdean@cytherianage.net>
* @ingroup Cache
*/
class MWMemcached {
// {{{ properties
// {{{ public
// {{{ constants
// {{{ flags
/**
* Flag: indicates data is serialized
*/
const SERIALIZED = 1;
/**
* Flag: indicates data is compressed
*/
const COMPRESSED = 2;
// }}}
/**
* Minimum savings to store data compressed
*/
const COMPRESSION_SAVINGS = 0.20;
// }}}
/**
* Command statistics
*
* @var array
* @access public
*/
public $stats;
// }}}
// {{{ private
/**
* Cached Sockets that are connected
*
* @var array
* @access private
*/
public $_cache_sock;
/**
* Current debug status; 0 - none to 9 - profiling
*
* @var bool
* @access private
*/
public $_debug;
/**
* Dead hosts, assoc array, 'host'=>'unixtime when ok to check again'
*
* @var array
* @access private
*/
public $_host_dead;
/**
* Is compression available?
*
* @var bool
* @access private
*/
public $_have_zlib;
/**
* Do we want to use compression?
*
* @var bool
* @access private
*/
public $_compress_enable;
/**
* At how many bytes should we compress?
*
* @var int
* @access private
*/
public $_compress_threshold;
/**
* Are we using persistent links?
*
* @var bool
* @access private
*/
public $_persistent;
/**
* If only using one server; contains ip:port to connect to
*
* @var string
* @access private
*/
public $_single_sock;
/**
* Array containing ip:port or array(ip:port, weight)
*
* @var array
* @access private
*/
public $_servers;
/**
* Our bit buckets
*
* @var array
* @access private
*/
public $_buckets;
/**
* Total # of bit buckets we have
*
* @var int
* @access private
*/
public $_bucketcount;
/**
* # of total servers we have
*
* @var int
* @access private
*/
public $_active;
/**
* Stream timeout in seconds. Applies for example to fread()
*
* @var int
* @access private
*/
public $_timeout_seconds;
/**
* Stream timeout in microseconds
*
* @var int
* @access private
*/
public $_timeout_microseconds;
/**
* Connect timeout in seconds
*/
public $_connect_timeout;
/**
* Number of connection attempts for each server
*/
public $_connect_attempts;
/**
* @var LoggerInterface
*/
private $_logger;
// }}}
// }}}
// {{{ methods
// {{{ public functions
// {{{ memcached()
/**
* Memcache initializer
*
* @param array $args Associative array of settings
*
* @return mixed
*/
public function __construct( $args ) {
$this->set_servers( isset( $args['servers'] ) ? $args['servers'] : array() );
$this->_debug = isset( $args['debug'] ) ? $args['debug'] : false;
$this->stats = array();
$this->_compress_threshold = isset( $args['compress_threshold'] ) ? $args['compress_threshold'] : 0;
$this->_persistent = isset( $args['persistent'] ) ? $args['persistent'] : false;
$this->_compress_enable = true;
$this->_have_zlib = function_exists( 'gzcompress' );
$this->_cache_sock = array();
$this->_host_dead = array();
$this->_timeout_seconds = 0;
$this->_timeout_microseconds = isset( $args['timeout'] ) ? $args['timeout'] : 500000;
$this->_connect_timeout = isset( $args['connect_timeout'] ) ? $args['connect_timeout'] : 0.1;
$this->_connect_attempts = 2;
$this->_logger = isset( $args['logger'] ) ? $args['logger'] : new NullLogger();
}
// }}}
// {{{ add()
/**
* Adds a key/value to the memcache server if one isn't already set with
* that key
*
* @param string $key Key to set with data
* @param mixed $val Value to store
* @param int $exp (optional) Expiration time. This can be a number of seconds
* to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
* longer must be the timestamp of the time at which the mapping should expire. It
* is safe to use timestamps in all cases, regardless of expiration
* eg: strtotime("+3 hour")
*
* @return bool
*/
public function add( $key, $val, $exp = 0 ) {
return $this->_set( 'add', $key, $val, $exp );
}
// }}}
// {{{ decr()
/**
* Decrease a value stored on the memcache server
*
* @param string $key Key to decrease
* @param int $amt (optional) amount to decrease
*
* @return mixed False on failure, value on success
*/
public function decr( $key, $amt = 1 ) {
return $this->_incrdecr( 'decr', $key, $amt );
}
// }}}
// {{{ delete()
/**
* Deletes a key from the server, optionally after $time
*
* @param string $key Key to delete
* @param int $time (optional) how long to wait before deleting
*
* @return bool True on success, false on failure
*/
public function delete( $key, $time = 0 ) {
if ( !$this->_active ) {
return false;
}
$sock = $this->get_sock( $key );
if ( !is_resource( $sock ) ) {
return false;
}
$key = is_array( $key ) ? $key[1] : $key;
if ( isset( $this->stats['delete'] ) ) {
$this->stats['delete']++;
} else {
$this->stats['delete'] = 1;
}
$cmd = "delete $key $time\r\n";
if ( !$this->_fwrite( $sock, $cmd ) ) {
return false;
}
$res = $this->_fgets( $sock );
if ( $this->_debug ) {
$this->_debugprint( sprintf( "MemCache: delete %s (%s)\n", $key, $res ) );
}
if ( $res == "DELETED" || $res == "NOT_FOUND" ) {
return true;
}
return false;
}
/**
* @param string $key
* @param int $timeout
* @return bool
*/
public function lock( $key, $timeout = 0 ) {
/* stub */
return true;
}
/**
* @param string $key
* @return bool
*/
public function unlock( $key ) {
/* stub */
return true;
}
// }}}
// {{{ disconnect_all()
/**
* Disconnects all connected sockets
*/
public function disconnect_all() {
foreach ( $this->_cache_sock as $sock ) {
fclose( $sock );
}
$this->_cache_sock = array();
}
// }}}
// {{{ enable_compress()
/**
* Enable / Disable compression
*
* @param bool $enable True to enable, false to disable
*/
public function enable_compress( $enable ) {
$this->_compress_enable = $enable;
}
// }}}
// {{{ forget_dead_hosts()
/**
* Forget about all of the dead hosts
*/
public function forget_dead_hosts() {
$this->_host_dead = array();
}
// }}}
// {{{ get()
/**
* Retrieves the value associated with the key from the memcache server
*
* @param array|string $key key to retrieve
* @param float $casToken [optional]
*
* @return mixed
*/
public function get( $key, &$casToken = null ) {
if ( $this->_debug ) {
$this->_debugprint( "get($key)\n" );
}
if ( !is_array( $key ) && strval( $key ) === '' ) {
$this->_debugprint( "Skipping key which equals to an empty string" );
return false;
}
if ( !$this->_active ) {
return false;
}
$sock = $this->get_sock( $key );
if ( !is_resource( $sock ) ) {
return false;
}
$key = is_array( $key ) ? $key[1] : $key;
if ( isset( $this->stats['get'] ) ) {
$this->stats['get']++;
} else {
$this->stats['get'] = 1;
}
$cmd = "gets $key\r\n";
if ( !$this->_fwrite( $sock, $cmd ) ) {
return false;
}
$val = array();
$this->_load_items( $sock, $val, $casToken );
if ( $this->_debug ) {
foreach ( $val as $k => $v ) {
$this->_debugprint( sprintf( "MemCache: sock %s got %s\n", serialize( $sock ), $k ) );
}
}
$value = false;
if ( isset( $val[$key] ) ) {
$value = $val[$key];
}
return $value;
}
// }}}
// {{{ get_multi()
/**
* Get multiple keys from the server(s)
*
* @param array $keys Keys to retrieve
*
* @return array
*/
public function get_multi( $keys ) {
if ( !$this->_active ) {
return false;
}
if ( isset( $this->stats['get_multi'] ) ) {
$this->stats['get_multi']++;
} else {
$this->stats['get_multi'] = 1;
}
$sock_keys = array();
$socks = array();
foreach ( $keys as $key ) {
$sock = $this->get_sock( $key );
if ( !is_resource( $sock ) ) {
continue;
}
$key = is_array( $key ) ? $key[1] : $key;
if ( !isset( $sock_keys[$sock] ) ) {
$sock_keys[intval( $sock )] = array();
$socks[] = $sock;
}
$sock_keys[intval( $sock )][] = $key;
}
$gather = array();
// Send out the requests
foreach ( $socks as $sock ) {
$cmd = 'gets';
foreach ( $sock_keys[intval( $sock )] as $key ) {
$cmd .= ' ' . $key;
}
$cmd .= "\r\n";
if ( $this->_fwrite( $sock, $cmd ) ) {
$gather[] = $sock;
}
}
// Parse responses
$val = array();
foreach ( $gather as $sock ) {
$this->_load_items( $sock, $val, $casToken );
}
if ( $this->_debug ) {
foreach ( $val as $k => $v ) {
$this->_debugprint( sprintf( "MemCache: got %s\n", $k ) );
}
}
return $val;
}
// }}}
// {{{ incr()
/**
* Increments $key (optionally) by $amt
*
* @param string $key Key to increment
* @param int $amt (optional) amount to increment
*
* @return int|null Null if the key does not exist yet (this does NOT
* create new mappings if the key does not exist). If the key does
* exist, this returns the new value for that key.
*/
public function incr( $key, $amt = 1 ) {
return $this->_incrdecr( 'incr', $key, $amt );
}
// }}}
// {{{ replace()
/**
* Overwrites an existing value for key; only works if key is already set
*
* @param string $key Key to set value as
* @param mixed $value Value to store
* @param int $exp (optional) Expiration time. This can be a number of seconds
* to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
* longer must be the timestamp of the time at which the mapping should expire. It
* is safe to use timestamps in all cases, regardless of exipration
* eg: strtotime("+3 hour")
*
* @return bool
*/
public function replace( $key, $value, $exp = 0 ) {
return $this->_set( 'replace', $key, $value, $exp );
}
// }}}
// {{{ run_command()
/**
* Passes through $cmd to the memcache server connected by $sock; returns
* output as an array (null array if no output)
*
* @param Resource $sock Socket to send command on
* @param string $cmd Command to run
*
* @return array Output array
*/
public function run_command( $sock, $cmd ) {
if ( !is_resource( $sock ) ) {
return array();
}
if ( !$this->_fwrite( $sock, $cmd ) ) {
return array();
}
$ret = array();
while ( true ) {
$res = $this->_fgets( $sock );
$ret[] = $res;
if ( preg_match( '/^END/', $res ) ) {
break;
}
if ( strlen( $res ) == 0 ) {
break;
}
}
return $ret;
}
// }}}
// {{{ set()
/**
* Unconditionally sets a key to a given value in the memcache. Returns true
* if set successfully.
*
* @param string $key Key to set value as
* @param mixed $value Value to set
* @param int $exp (optional) Expiration time. This can be a number of seconds
* to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
* longer must be the timestamp of the time at which the mapping should expire. It
* is safe to use timestamps in all cases, regardless of exipration
* eg: strtotime("+3 hour")
*
* @return bool True on success
*/
public function set( $key, $value, $exp = 0 ) {
return $this->_set( 'set', $key, $value, $exp );
}
// }}}
// {{{ cas()
/**
* Sets a key to a given value in the memcache if the current value still corresponds
* to a known, given value. Returns true if set successfully.
*
* @param float $casToken Current known value
* @param string $key Key to set value as
* @param mixed $value Value to set
* @param int $exp (optional) Expiration time. This can be a number of seconds
* to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
* longer must be the timestamp of the time at which the mapping should expire. It
* is safe to use timestamps in all cases, regardless of exipration
* eg: strtotime("+3 hour")
*
* @return bool True on success
*/
public function cas( $casToken, $key, $value, $exp = 0 ) {
return $this->_set( 'cas', $key, $value, $exp, $casToken );
}
// }}}
// {{{ set_compress_threshold()
/**
* Sets the compression threshold
*
* @param int $thresh Threshold to compress if larger than
*/
public function set_compress_threshold( $thresh ) {
$this->_compress_threshold = $thresh;
}
// }}}
// {{{ set_debug()
/**
* Sets the debug flag
*
* @param bool $dbg True for debugging, false otherwise
*
* @see MWMemcached::__construct
*/
public function set_debug( $dbg ) {
$this->_debug = $dbg;
}
// }}}
// {{{ set_servers()
/**
* Sets the server list to distribute key gets and puts between
*
* @param array $list Array of servers to connect to
*
* @see MWMemcached::__construct()
*/
public function set_servers( $list ) {
$this->_servers = $list;
$this->_active = count( $list );
$this->_buckets = null;
$this->_bucketcount = 0;
$this->_single_sock = null;
if ( $this->_active == 1 ) {
$this->_single_sock = $this->_servers[0];
}
}
/**
* Sets the timeout for new connections
*
* @param int $seconds Number of seconds
* @param int $microseconds Number of microseconds
*/
public function set_timeout( $seconds, $microseconds ) {
$this->_timeout_seconds = $seconds;
$this->_timeout_microseconds = $microseconds;
}
// }}}
// }}}
// {{{ private methods
// {{{ _close_sock()
/**
* Close the specified socket
*
* @param string $sock Socket to close
*
* @access private
*/
function _close_sock( $sock ) {
$host = array_search( $sock, $this->_cache_sock );
fclose( $this->_cache_sock[$host] );
unset( $this->_cache_sock[$host] );
}
// }}}
// {{{ _connect_sock()
/**
* Connects $sock to $host, timing out after $timeout
*
* @param int $sock Socket to connect
* @param string $host Host:IP to connect to
*
* @return bool
* @access private
*/
function _connect_sock( &$sock, $host ) {
list( $ip, $port ) = preg_split( '/:(?=\d)/', $host );
$sock = false;
$timeout = $this->_connect_timeout;
$errno = $errstr = null;
for ( $i = 0; !$sock && $i < $this->_connect_attempts; $i++ ) {
wfSuppressWarnings();
if ( $this->_persistent == 1 ) {
$sock = pfsockopen( $ip, $port, $errno, $errstr, $timeout );
} else {
$sock = fsockopen( $ip, $port, $errno, $errstr, $timeout );
}
wfRestoreWarnings();
}
if ( !$sock ) {
$this->_error_log( "Error connecting to $host: $errstr\n" );
$this->_dead_host( $host );
return false;
}
// Initialise timeout
stream_set_timeout( $sock, $this->_timeout_seconds, $this->_timeout_microseconds );
// If the connection was persistent, flush the read buffer in case there
// was a previous incomplete request on this connection
if ( $this->_persistent ) {
$this->_flush_read_buffer( $sock );
}
return true;
}
// }}}
// {{{ _dead_sock()
/**
* Marks a host as dead until 30-40 seconds in the future
*
* @param string $sock Socket to mark as dead
*
* @access private
*/
function _dead_sock( $sock ) {
$host = array_search( $sock, $this->_cache_sock );
$this->_dead_host( $host );
}
/**
* @param string $host
*/
function _dead_host( $host ) {
$parts = explode( ':', $host );
$ip = $parts[0];
$this->_host_dead[$ip] = time() + 30 + intval( rand( 0, 10 ) );
$this->_host_dead[$host] = $this->_host_dead[$ip];
unset( $this->_cache_sock[$host] );
}
// }}}
// {{{ get_sock()
/**
* get_sock
*
* @param string $key Key to retrieve value for;
*
* @return Resource|bool Resource on success, false on failure
* @access private
*/
function get_sock( $key ) {
if ( !$this->_active ) {
return false;
}
if ( $this->_single_sock !== null ) {
return $this->sock_to_host( $this->_single_sock );
}
$hv = is_array( $key ) ? intval( $key[0] ) : $this->_hashfunc( $key );
if ( $this->_buckets === null ) {
$bu = array();
foreach ( $this->_servers as $v ) {
if ( is_array( $v ) ) {
for ( $i = 0; $i < $v[1]; $i++ ) {
$bu[] = $v[0];
}
} else {
$bu[] = $v;
}
}
$this->_buckets = $bu;
$this->_bucketcount = count( $bu );
}
$realkey = is_array( $key ) ? $key[1] : $key;
for ( $tries = 0; $tries < 20; $tries++ ) {
$host = $this->_buckets[$hv % $this->_bucketcount];
$sock = $this->sock_to_host( $host );
if ( is_resource( $sock ) ) {
return $sock;
}
$hv = $this->_hashfunc( $hv . $realkey );
}
return false;
}
// }}}
// {{{ _hashfunc()
/**
* Creates a hash integer based on the $key
*
* @param string $key Key to hash
*
* @return int Hash value
* @access private
*/
function _hashfunc( $key ) {
# Hash function must be in [0,0x7ffffff]
# We take the first 31 bits of the MD5 hash, which unlike the hash
# function used in a previous version of this client, works
return hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
}
// }}}
// {{{ _incrdecr()
/**
* Perform increment/decriment on $key
*
* @param string $cmd Command to perform
* @param string|array $key Key to perform it on
* @param int $amt Amount to adjust
*
* @return int New value of $key
* @access private
*/
function _incrdecr( $cmd, $key, $amt = 1 ) {
if ( !$this->_active ) {
return null;
}
$sock = $this->get_sock( $key );
if ( !is_resource( $sock ) ) {
return null;
}
$key = is_array( $key ) ? $key[1] : $key;
if ( isset( $this->stats[$cmd] ) ) {
$this->stats[$cmd]++;
} else {
$this->stats[$cmd] = 1;
}
if ( !$this->_fwrite( $sock, "$cmd $key $amt\r\n" ) ) {
return null;
}
$line = $this->_fgets( $sock );
$match = array();
if ( !preg_match( '/^(\d+)/', $line, $match ) ) {
return null;
}
return $match[1];
}
// }}}
// {{{ _load_items()
/**
* Load items into $ret from $sock
*
* @param Resource $sock Socket to read from
* @param array $ret returned values
* @param float $casToken [optional]
* @return bool True for success, false for failure
*
* @access private
*/
function _load_items( $sock, &$ret, &$casToken = null ) {
$results = array();
while ( 1 ) {
$decl = $this->_fgets( $sock );
if ( $decl === false ) {
/*
* If nothing can be read, something is wrong because we know exactly when
* to stop reading (right after "END") and we return right after that.
*/
return false;
} elseif ( preg_match( '/^VALUE (\S+) (\d+) (\d+) (\d+)$/', $decl, $match ) ) {
/*
* Read all data returned. This can be either one or multiple values.
* Save all that data (in an array) to be processed later: we'll first
* want to continue reading until "END" before doing anything else,
* to make sure that we don't leave our client in a state where it's
* output is not yet fully read.
*/
$results[] = array(
$match[1], // rkey
$match[2], // flags
$match[3], // len
$match[4], // casToken
$this->_fread( $sock, $match[3] + 2 ), // data
);
} elseif ( $decl == "END" ) {
if ( count( $results ) == 0 ) {
return false;
}
/**
* All data has been read, time to process the data and build
* meaningful return values.
*/
foreach ( $results as $vars ) {
list( $rkey, $flags, $len, $casToken, $data ) = $vars;
if ( $data === false || substr( $data, -2 ) !== "\r\n" ) {
$this->_handle_error( $sock,
'line ending missing from data block from $1' );
return false;
}
$data = substr( $data, 0, -2 );
$ret[$rkey] = $data;
if ( $this->_have_zlib && $flags & self::COMPRESSED ) {
$ret[$rkey] = gzuncompress( $ret[$rkey] );
}
/*
* This unserialize is the exact reason that we only want to
* process data after having read until "END" (instead of doing
* this right away): "unserialize" can trigger outside code:
* in the event that $ret[$rkey] is a serialized object,
* unserializing it will trigger __wakeup() if present. If that
* function attempted to read from memcached (while we did not
* yet read "END"), these 2 calls would collide.
*/
if ( $flags & self::SERIALIZED ) {
$ret[$rkey] = unserialize( $ret[$rkey] );
}
}
return true;
} else {
$this->_handle_error( $sock, 'Error parsing response from $1' );
return false;
}
}
}
// }}}
// {{{ _set()
/**
* Performs the requested storage operation to the memcache server
*
* @param string $cmd Command to perform
* @param string $key Key to act on
* @param mixed $val What we need to store
* @param int $exp (optional) Expiration time. This can be a number of seconds
* to cache for (up to 30 days inclusive). Any timespans of 30 days + 1 second or
* longer must be the timestamp of the time at which the mapping should expire. It
* is safe to use timestamps in all cases, regardless of exipration
* eg: strtotime("+3 hour")
* @param float $casToken [optional]
*
* @return bool
* @access private
*/
function _set( $cmd, $key, $val, $exp, $casToken = null ) {
if ( !$this->_active ) {
return false;
}
$sock = $this->get_sock( $key );
if ( !is_resource( $sock ) ) {
return false;
}
if ( isset( $this->stats[$cmd] ) ) {
$this->stats[$cmd]++;
} else {
$this->stats[$cmd] = 1;
}
$flags = 0;
if ( !is_scalar( $val ) ) {
$val = serialize( $val );
$flags |= self::SERIALIZED;
if ( $this->_debug ) {
$this->_debugprint( sprintf( "client: serializing data as it is not scalar\n" ) );
}
}
$len = strlen( $val );
if ( $this->_have_zlib && $this->_compress_enable
&& $this->_compress_threshold && $len >= $this->_compress_threshold
) {
$c_val = gzcompress( $val, 9 );
$c_len = strlen( $c_val );
if ( $c_len < $len * ( 1 - self::COMPRESSION_SAVINGS ) ) {
if ( $this->_debug ) {
$this->_debugprint( sprintf( "client: compressing data; was %d bytes is now %d bytes\n", $len, $c_len ) );
}
$val = $c_val;
$len = $c_len;
$flags |= self::COMPRESSED;
}
}
$command = "$cmd $key $flags $exp $len";
if ( $casToken ) {
$command .= " $casToken";
}
if ( !$this->_fwrite( $sock, "$command\r\n$val\r\n" ) ) {
return false;
}
$line = $this->_fgets( $sock );
if ( $this->_debug ) {
$this->_debugprint( sprintf( "%s %s (%s)\n", $cmd, $key, $line ) );
}
if ( $line == "STORED" ) {
return true;
}
return false;
}
// }}}
// {{{ sock_to_host()
/**
* Returns the socket for the host
*
* @param string $host Host:IP to get socket for
*
* @return Resource|bool IO Stream or false
* @access private
*/
function sock_to_host( $host ) {
if ( isset( $this->_cache_sock[$host] ) ) {
return $this->_cache_sock[$host];
}
$sock = null;
$now = time();
list( $ip, /* $port */) = explode( ':', $host );
if ( isset( $this->_host_dead[$host] ) && $this->_host_dead[$host] > $now ||
isset( $this->_host_dead[$ip] ) && $this->_host_dead[$ip] > $now
) {
return null;
}
if ( !$this->_connect_sock( $sock, $host ) ) {
return null;
}
// Do not buffer writes
stream_set_write_buffer( $sock, 0 );
$this->_cache_sock[$host] = $sock;
return $this->_cache_sock[$host];
}
/**
* @param string $text
*/
function _debugprint( $text ) {
$this->_logger->debug( $text );
}
/**
* @param string $text
*/
function _error_log( $text ) {
$this->_logger->error( "Memcached error: $text" );
}
/**
* Write to a stream. If there is an error, mark the socket dead.
*
* @param Resource $sock The socket
* @param string $buf The string to write
* @return bool True on success, false on failure
*/
function _fwrite( $sock, $buf ) {
$bytesWritten = 0;
$bufSize = strlen( $buf );
while ( $bytesWritten < $bufSize ) {
$result = fwrite( $sock, $buf );
$data = stream_get_meta_data( $sock );
if ( $data['timed_out'] ) {
$this->_handle_error( $sock, 'timeout writing to $1' );
return false;
}
// Contrary to the documentation, fwrite() returns zero on error in PHP 5.3.
if ( $result === false || $result === 0 ) {
$this->_handle_error( $sock, 'error writing to $1' );
return false;
}
$bytesWritten += $result;
}
return true;
}
/**
* Handle an I/O error. Mark the socket dead and log an error.
*
* @param Resource $sock
* @param string $msg
*/
function _handle_error( $sock, $msg ) {
$peer = stream_socket_get_name( $sock, true /** remote **/ );
if ( strval( $peer ) === '' ) {
$peer = array_search( $sock, $this->_cache_sock );
if ( $peer === false ) {
$peer = '[unknown host]';
}
}
$msg = str_replace( '$1', $peer, $msg );
$this->_error_log( "$msg\n" );
$this->_dead_sock( $sock );
}
/**
* Read the specified number of bytes from a stream. If there is an error,
* mark the socket dead.
*
* @param Resource $sock The socket
* @param int $len The number of bytes to read
* @return string|bool The string on success, false on failure.
*/
function _fread( $sock, $len ) {
$buf = '';
while ( $len > 0 ) {
$result = fread( $sock, $len );
$data = stream_get_meta_data( $sock );
if ( $data['timed_out'] ) {
$this->_handle_error( $sock, 'timeout reading from $1' );
return false;
}
if ( $result === false ) {
$this->_handle_error( $sock, 'error reading buffer from $1' );
return false;
}
if ( $result === '' ) {
// This will happen if the remote end of the socket is shut down
$this->_handle_error( $sock, 'unexpected end of file reading from $1' );
return false;
}
$len -= strlen( $result );
$buf .= $result;
}
return $buf;
}
/**
* Read a line from a stream. If there is an error, mark the socket dead.
* The \r\n line ending is stripped from the response.
*
* @param Resource $sock The socket
* @return string|bool The string on success, false on failure
*/
function _fgets( $sock ) {
$result = fgets( $sock );
// fgets() may return a partial line if there is a select timeout after
// a successful recv(), so we have to check for a timeout even if we
// got a string response.
$data = stream_get_meta_data( $sock );
if ( $data['timed_out'] ) {
$this->_handle_error( $sock, 'timeout reading line from $1' );
return false;
}
if ( $result === false ) {
$this->_handle_error( $sock, 'error reading line from $1' );
return false;
}
if ( substr( $result, -2 ) === "\r\n" ) {
$result = substr( $result, 0, -2 );
} elseif ( substr( $result, -1 ) === "\n" ) {
$result = substr( $result, 0, -1 );
} else {
$this->_handle_error( $sock, 'line ending missing in response from $1' );
return false;
}
return $result;
}
/**
* Flush the read buffer of a stream
* @param Resource $f
*/
function _flush_read_buffer( $f ) {
if ( !is_resource( $f ) ) {
return;
}
$r = array( $f );
$w = null;
$e = null;
$n = stream_select( $r, $w, $e, 0, 0 );
while ( $n == 1 && !feof( $f ) ) {
fread( $f, 1024 );
$r = array( $f );
$w = null;
$e = null;
$n = stream_select( $r, $w, $e, 0, 0 );
}
}
// }}}
// }}}
// }}}
}
// }}}
class MemCachedClientforWiki extends MWMemcached {
}
|