Source

omcbp / memcache.ml

  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
(***********************************************************************
                                                                     
 Memcached Client library module (binary protocol)                   
                                                                     
 @author Alexey Kishkin odobenus-rosmarus@ya.ru                      
 @version 1.0

 This file is part of omcbp.
 
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
     
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
         
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

**********************************************************************)

(* Some constants of operations *)

let opGet = 0x00
let opSet = 0x01
let opAdd = 0x02
let opReplace = 0x03
let opDelete = 0x04
let opIncrement = 0x05
let opDecrement = 0x06
let opQuit = 0x07
let opFlush = 0x08
let opGetQ = 0x09
let opNoop = 0x0a
let opVersion = 0x0b
let opGetK = 0x0c
let opGetKQ = 0x0d
let opAppend = 0x0e
let opPrepend = 0x0f
let opStat = 0x10
let opSetQ = 0x11
let opAddQ = 0x12
let opReplaceQ = 0x13
let opDeleteQ = 0x14
let opIncrementQ = 0x15
let opDecrementQ = 0x16
let opQuitQ = 0x17
let opFlushQ = 0x18
let opAppendQ = 0x19
let opPrependQ = 0x1a


(** packet header *)
type magic = Request | Response;;

(** Response from memcache server*)
type response_status =
      MC_No_error
   | MC_Key_not_found
   | MC_Key_exists
   | MC_Value_too_large
   | MC_Invalid_argument
   | MC_Item_not_stored
   | MC_Unknown_comamnd
   | MC_Out_of_memory;;

(** Main exception that is used for any problems in memcache protocol *)
exception Bad_memcache_format of string;;

(* Some auxilary functions for buffer constructing *)

let set_i8 buffer i =
   Buffer.add_char buffer (Char.chr (i land 0xff));;

let set_i16 buffer i =
   let badd = Buffer.add_char buffer in
   let ex x shift = (Char.chr ( (x lsr shift) land 0xff)) in
   badd (ex i 8);
   badd (ex i 0);;

let set_i32 buffer i =
   let badd = Buffer.add_char buffer in
   let ex x shift = (Char.chr ( (x lsr shift) land 0xff)) in
   badd (ex i 24);
   badd (ex i 16);
   badd (ex i 8);
   badd (ex i 0);;

let set_int32 buffer i =
   let badd = Buffer.add_char buffer in
   let ex x shift = (Char.chr ( Int32.to_int ( Int32.logand (Int32.shift_right x shift) 0xffl))) in
   badd (ex i 24);
   badd (ex i 16);
   badd (ex i 8);
   badd (ex i 0);;

let set_int64 buffer i =
   let badd = Buffer.add_char buffer in
   let ex x shift = (Char.chr ( Int64.to_int ( Int64.logand (Int64.shift_right x shift) 0xffL))) in
   badd (ex i 56);
   badd (ex i 48);
   badd (ex i 40);
   badd (ex i 32);
   badd (ex i 24);
   badd (ex i 16);
   badd (ex i 8);
   badd (ex i 0);;

(* Buffer disassembling *)

let get_i8 str n = Char.code(str.[n]);;

let get_i16 str n =
   let b1 = Char.code str.[n] and b2 = Char.code str.[n + 1] in
   (b1 lsl 8) lor b2;;

let get_i32 str n =
   let b1 = Char.code str.[n] and b2 = Char.code str.[n + 1] and b3 = Char.code str.[n + 2] and b4 = Char.code str.[n + 3] in
   (b1 lsl 24) lor (b2 lsl 16) lor (b3 lsl 8) lor b4;;

let get_int32 str n =
   let b1 = Int32.shift_left (Int32.of_int (Char.code str.[n])) 24
   and b2 = Int32.shift_left (Int32.of_int (Char.code str.[n + 1])) 16
   and b3 = Int32.shift_left (Int32.of_int (Char.code str.[n + 2])) 8
   and b4 = Int32.of_int (Char.code str.[n + 3]) in
   Int32.logor (Int32.logor b1 b2) (Int32.logor b3 b4);;

let get_int64 str n =
   let b1 = Int64.shift_left (Int64.of_int (Char.code str.[n])) 56
   and b2 = Int64.shift_left (Int64.of_int (Char.code str.[n + 1])) 48
   and b3 = Int64.shift_left (Int64.of_int (Char.code str.[n + 2])) 40
   and b4 = Int64.shift_left (Int64.of_int (Char.code str.[n + 3])) 32
   and b5 = Int64.shift_left (Int64.of_int (Char.code str.[n + 4])) 24
   and b6 = Int64.shift_left (Int64.of_int (Char.code str.[n + 5])) 16
   and b7 = Int64.shift_left (Int64.of_int (Char.code str.[n + 6])) 8
   and b8 = Int64.of_int (Char.code str.[n + 7]) in
   Int64.logor (Int64.logor (Int64.logor b1 b2) (Int64.logor b3 b4)) (Int64.logor (Int64.logor b5 b6) (Int64.logor b7 b8));;


(** Main class for memcache connection
    @param  host - string -ip address or hostname of memcache server 
    @param  port - integer - internet port 
 *)

class memcache_connector (host: string) (port: int) =
object(self)
   val host = host
   val port = port
   val mutable i_channel = stdin
   val mutable o_channel = stdout

   method magic_code_encode = function
      | Request -> 0x80
      | Response -> 0x81
   
   method magic_code_decode = function
      | 0x80 -> Request
      | 0x81 -> Response
      | _ -> raise (Bad_memcache_format "memcache wrong magic code")
   
   method response_status_decode = function
      | 0x0000 -> MC_No_error
      | 0x0001 -> MC_Key_not_found
      | 0x0002 -> MC_Key_exists
      | 0x0003 -> MC_Value_too_large
      | 0x0004 -> MC_Invalid_argument
      | 0x0005 -> MC_Item_not_stored
      | 0x0081 -> MC_Unknown_comamnd
      | 0x0082 -> MC_Out_of_memory
      | _ -> raise (Bad_memcache_format "memcache wrong response status")
   
   method encode_packet op key extras vl opaque cas =
      let keylen = String.length key and
      extralen = String.length extras and
      vllen = String.length vl in
      if (keylen > 0xFFFE ) || (extralen > 0xFE) then
         raise (Bad_memcache_format "too big key or extra")
      else
         let buffer = Buffer.create (24 + vllen + keylen + extralen) in
         set_i8 buffer (self#magic_code_encode Request);           (* 0 *)
         set_i8 buffer  op;                                        (* 1 *)
         set_i16 buffer keylen;                                    (*2 3*)
         set_i8 buffer extralen;                                   (* 4 *)
         set_i8 buffer 0;                                          (* 5 *) (* RAW data type *)
         set_i16 buffer 0;                                         (*6 7*) (* RESERVED *)
         set_i32 buffer (vllen + keylen + extralen);                   (*8 9 10 11*)
         set_i32 buffer opaque;                                    (*12 13 14 15*)
         set_int64 buffer cas;                                     (*16 17 18 19 20 21 22 23*)
         if extralen > 0 then (Buffer.add_string buffer extras);
         if keylen > 0 then (Buffer.add_string buffer key);
         if vllen > 0 then (Buffer.add_string buffer vl);
         Buffer.contents buffer
   
  
   method decode_header pct =
      try
         let magic = self#magic_code_decode (get_i8 pct 0) in        (* 0 *)
         let op = get_i8 pct 1 in                                    (* 1 *)
         let keylength = get_i16 pct 2 in                            (*2 3*)
         let extralength = get_i8 pct 4 in                           (* 4 *)
         let datatype = get_i8 pct 5 in                              (* 5 *)
         let status = self#response_status_decode (get_i16 pct 6) in (*6 7*)
         let total_body_length = get_i32 pct 8 in                    (*8 9 10 11*)
         let opaque = get_i32 pct 12 in                              (*12 13 14 15*)
         let cas = get_int64 pct 16 in                               (*16 17 18 19 20 21 22 23*)
         if (datatype <> 0) then raise (Bad_memcache_format "datatype is not RAW");
         if (magic <> Response) then raise (Bad_memcache_format "magic is not response");
         (keylength, extralength, status, total_body_length, opaque, cas, op)
      with
      | Invalid_argument _ -> raise (Bad_memcache_format "packet too short")
   
   method decode_body pct keylength extralength total_body_length =
      try
         let extras = (if extralength > 0 then (String.sub pct 0 extralength) else "") in
         let key = (if keylength > 0 then String.sub pct (extralength) keylength else "") in
         let valuelength = total_body_length - keylength - extralength in
         let value = (if valuelength > 0 then String.sub pct (extralength + keylength) valuelength else "") in
         (key, extras, value)
      with
      | Invalid_argument _ -> raise (Bad_memcache_format "packet too short")
   
   method read_answer =
      let hdr = String.make 24 ' ' in
      let () = really_input i_channel hdr 0 24 in
      let (keylength, extralength, status, total_body_length, opaque, cas, op) = self#decode_header hdr in
      let body = String.make total_body_length ' ' in
      let () = really_input i_channel body 0 total_body_length in
      let (key, extras, value) = self#decode_body body keylength extralength total_body_length in
      (status, op, key, value, extras, opaque, cas)
   
   (** Set cache data.
   
     @param key  - string 
     @param value - string 
     @param flags - integer - 
     @param expiration - integer - seconds to invalidation this record
   *)
   method set key v flags expiration =
      let buf = Buffer.create 8 in
      let _ = set_i32 buf flags and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opSet key extras v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, ky, value, extras, opaque, cas) = self#read_answer in
         (status, ky, value)
      end

   (** Set 'quietly' (don't wait for answer) cache data.
   
     @param key  - string 
     @param value - string 
     @param flags - integer - 
     @param expiration - integer - seconds to invalidation this record
   *)
   
   method setq key v flags expiration =
      let buf = Buffer.create 8 in
      let _ = set_i32 buf flags and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opSetQ key extras v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel
      end
  
  (** Add (operation fails if key already exists) cache data.
   
     @param key  string 
     @param value  string 
     @param flags  integer - 
     @param expiration  integer - seconds to invalidation this record
     
     @return (status key value)
   *)

   
   method add key v flags expiration =
      let buf = Buffer.create 8 in
      let _ = set_i32 buf flags and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opAdd key extras v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, ky, value, extras, opaque, cas) = self#read_answer in
         (status, ky, value)
      end

  (** Add (operation fails if key already exists) cache data. Dont wait the answer.
   
     @param key  - string 
     @param value - string 
     @param flags - integer - 
     @param expiration - integer - seconds to invalidation this record
   *)
   
   method addq key v flags expiration =
      let buf = Buffer.create 8 in
      let _ = set_i32 buf flags and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opAddQ key extras v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
      end
      
      
  (** Replace (operation fails if key doesnt exist) cache data.
   
     @param key  - string 
     @param value - string 
     @param flags - integer - 
     @param expiration - integer - seconds to invalidation this record
   *)
   method replace key v flags expiration =
      let buf = Buffer.create 8 in
      let _ = set_i32 buf flags and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opReplace key extras v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end
   
 (** Replace quietly (dont wait answer)  cache data. Ooperation fails if key doesn't exist
   
     @param key  - string 
     @param value - string 
     @param flags - integer - 
     @param expiration - integer - seconds to invalidation this record
   *)
   method replaceq key v flags expiration =
      let buf = Buffer.create 8 in
      let _ = set_i32 buf flags and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opReplaceQ key extras v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
      end
 
   (** Delete key 
     @param key - string - key to delete
   *)
   
   method delete key =
      let cargo = self#encode_packet opDelete key "" "" 0 0L in
      begin
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end
   
   (** Delete key , dont wait the answer
      @param key - string - key to delete
   
     *)
   
   method deleteq key =
      let cargo = self#encode_packet opDeleteQ key "" "" 0 0L in
      begin
         output_string o_channel cargo;
         flush o_channel;
      end
   
   (** Retrieve key
      @param key - string
      @return  (status, key, value)  Key and value -- strings,  Status is 'response_status" type 
    *)
   method get key =
      let cargo = self#encode_packet opGetK key "" "" 0 0L in
      begin
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end


   (** Retrieve  a lot of records at once
      @param keys - list of string
      @return  list of (status, key, value),    Key and value -- strings,  Status is 'response_status" type 
    *)
   
   method multiget keys =
      List.iter (fun key ->
                  let cargo = self#encode_packet opGetKQ key "" "" 0 0L in
                  let () = output_string o_channel cargo in
                  flush o_channel) keys;
      let cargo = self#encode_packet opNoop "" "" "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let continue = ref true in
         let answers = ref [] in
         while !continue do
            let (status, op, key, value, extras, opaque, cas) = self#read_answer in
            if op = 0x0a
            then continue := false
            else answers := ((status, key, value) :: !answers)
         done;
         !answers
      end

  (** Increment value in the cache
      @param key - string
      @param value_to_add int64
      @param initial_value int64
      @param expiration - time in seconds to invalidation
      
      @return  (status, key, value)  Key and value -- strings,  Status is 'response_status" type 
    *)
   method incr key (value_to_add: int64) (initial_value: int64) expiration =
      let buf = Buffer.create 20 in
      let _ = set_int64 buf value_to_add
      and _ = set_int64 buf initial_value
      and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opIncrement key extras "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end



  (** Increment value in the cache, dont wait for answer
      @param key - string
      @param value_to_add int64
      @param initial_value int64
      @param expiration - time in seconds to invalidation
    *)
   method incrq key (value_to_add: int64) (initial_value: int64) expiration =
      let buf = Buffer.create 20 in
      let _ = set_int64 buf value_to_add
      and _ = set_int64 buf initial_value
      and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opIncrementQ key extras "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
      end

      
   (** Decrement valie in the cache 
      @param key - string
      @param value_to_add int64
      @param initial_value int64
      @param expiration - time in seconds to invalidation
      @return (status, key, value)  Key and value -- strings,  Status is 'response_status" type 
    *)
   method decr key (value_to_sub: int64) (initial_value: int64) expiration =
      let buf = Buffer.create 20 in
      let _ = set_int64 buf value_to_sub
      and _ = set_int64 buf initial_value
      and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opDecrement key extras "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, (get_int64 value 0))
      end
      
   (** The same but dont wait for answer *)
   method decrq key (value_to_sub: int64) (initial_value: int64) expiration =
      let buf = Buffer.create 20 in
      let _ = set_int64 buf value_to_sub
      and _ = set_int64 buf initial_value
      and _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opDecrementQ key extras "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
      end
  
  (** Close connection 
      @return (status, key, value)  Key and value -- strings (must be ignored),  Status is response_status type 
  *) 
   method quit =
      let cargo = self#encode_packet opQuit "" "" "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end

  (** Close connection, dont wait for answer *) 
   method quitq =
      let cargo = self#encode_packet opQuitQ "" "" "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
      end
   
   method flush expiration =
      let buf = Buffer.create 4 in
      let _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opFlush "" extras "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end
   
   method flushq expiration =
      let buf = Buffer.create 4 in
      let _ = set_i32 buf expiration in
      let extras = Buffer.contents buf in
      let cargo = self#encode_packet opFlushQ "" extras "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
      end
   
   method noop =
      let cargo = self#encode_packet opNoop "" "" "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end
   
   
   (** Return version of memcache server *)
   method version =
      let cargo = self#encode_packet opVersion "" "" "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end
   
   method append key v =
      let cargo = self#encode_packet opAppend key "" v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end
   
   method appendq key v =
      let cargo = self#encode_packet opAppendQ key "" v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
      end
   
   method prepend key v =
      let cargo = self#encode_packet opPrepend key "" v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end
   
   method prependq key v =
      let cargo = self#encode_packet opPrependQ key "" v 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
      end
   
   method stat key =
      let cargo = self#encode_packet opStat key "" "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let (status, op, key, value, extras, opaque, cas) = self#read_answer in
         (status, key, value)
      end
   
   method stat_all =
      let cargo = self#encode_packet opStat "" "" "" 0 0L in
      begin
         (* self#printhex cargo; *)
         output_string o_channel cargo;
         flush o_channel;
         let collect = ref [] in
         let continue = ref true in
         while !continue do
            let (status, op, ky, value, extras, opaque, cas) = self#read_answer in
            if ((String.length ky) = 0) && ((String.length value) = 0)
            then (continue := false)
            else (collect := (status, ky, value) :: !collect)
         done;
         !collect
      end
   
   initializer
   let i, o = Unix.open_connection (Unix.ADDR_INET ((Unix.inet_addr_of_string host), port)) in
   i_channel <- i;
   o_channel <- o
   
end;;


(* Example *)
(*
let print_result (status, key, value) =
   print_string (key^" :: ");
   print_endline (match status with
         | MC_Out_of_memory -> "out_of_memory"
         | MC_No_error -> ("value "^value)
         | MC_Key_not_found -> "notfound"
         | MC_Key_exists -> "key exists"
         | MC_Value_too_large -> "too large"
         | MC_Invalid_argument -> "Invalid argument"
         | MC_Item_not_stored -> "not stored"
         | MC_Unknown_comamnd -> "unknown command")

let c = new memcache_connector "127.0.0.1" 11211;;
c#set "walrus" "1963" 0 0;;
c#set "wax" "1969" 0 0;;
c#set "mumka" "1959" 0 0;;
c#add "odobenus rosmarus" "rosmarus" 0 0;;

c#append "walrus" " year";;
List.iter (fun x -> print_result x) (c#multiget ["mumka";"odobenus rosmarus";"abirvalg";"walrus"]);;
List.iter (fun x -> print_result x) (c#stat_all);;
*)