Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Factor out new __run_cmd method

Unlike the previous method of the same name, this one actually sends a
request and enqueues a response handler.
  • Loading branch information...
commit 8c6f8c84b28bc9bc04f495eaa9dcd4b4e8de5cd3 1 parent 89241f4
Aaron Crane authored March 08, 2012

Showing 1 changed file with 37 additions and 52 deletions. Show diff stats Hide diff stats

  1. 89  lib/Redis.pm
89  lib/Redis.pm
@@ -233,17 +233,8 @@ sub __std_cmd {
233 233
 
234 234
   my @cmd_args = @_;
235 235
   $self->__with_reconnect(sub {
236  
-    $self->__queue_cmd($command, $collect_errors, @cmd_args, $cb || sub {
237  
-      my ($reply, $error) = @_;
238  
-      confess "[$command] $error, " if defined $error;
239  
-      $ret = $reply;
240  
-    });
  236
+    $self->__run_cmd($command, $collect_errors, undef, $cb, @cmd_args);
241 237
   });
242  
-
243  
-  return 1 if $cb;
244  
-
245  
-  $self->wait_all_responses;
246  
-  return wantarray && ref $ret eq 'ARRAY' ? @$ret : $ret;
247 238
 }
248 239
 
249 240
 sub __with_reconnect {
@@ -263,6 +254,28 @@ sub __with_reconnect {
263 254
   };
264 255
 }
265 256
 
  257
+sub __run_cmd {
  258
+  my ($self, $command, $collect_errors, $custom_decode, $cb, @args) = @_;
  259
+
  260
+  my $ret;
  261
+  my $wrapper = $cb && $custom_decode ? sub {
  262
+    my ($reply, $error) = @_;
  263
+    $cb->(scalar $custom_decode->($reply), $error);
  264
+  } : $cb || sub {
  265
+    my ($reply, $error) = @_;
  266
+    confess "[$command] $error, " if defined $error;
  267
+    $ret = $reply;
  268
+  };
  269
+
  270
+  $self->__queue_cmd($command, $collect_errors, @args, $wrapper);
  271
+
  272
+  return 1 if $cb;
  273
+
  274
+  $self->wait_all_responses;
  275
+  return $custom_decode ? $custom_decode->($ret, !wantarray)
  276
+       : wantarray && ref $ret eq 'ARRAY' ? @$ret : $ret;
  277
+}
  278
+
266 279
 sub __queue_cmd {
267 280
   my $self    = shift;
268 281
   my $command = shift;
@@ -336,71 +349,43 @@ sub ping {
336 349
   };
337 350
 }
338 351
 
339  
-sub __decode_info {
340  
-  my ($self, $raw) = @_;
341  
-  return @$raw if wantarray && ref $raw eq 'ARRAY'; # very unlikely
342  
-  return { map { split(/:/, $_, 2) } split(/\r\n/, $raw) } if defined $raw;
343  
-  return $raw;
344  
-}
345  
-
346 352
 sub info {
347 353
   my $self = shift;
348 354
   $self->__is_valid_command('INFO');
349 355
 
350  
-  my $info;
351  
-  my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef;
352  
-  my $real_cb = $cb ? sub {
353  
-    my ($reply, $error) = @_;
354  
-    my $decoded = $self->__decode_info($reply);
355  
-    $cb->($decoded, $error);
356  
-  } : sub {
357  
-    my ($reply, $error) = @_;
358  
-    confess "[INFO] $error, " if defined $error;
359  
-    $info = $reply;
  356
+  my $custom_decode = sub {
  357
+    my ($reply) = @_;
  358
+    return $reply if !defined $reply || ref $reply;
  359
+    return { map { split(/:/, $_, 2) } split(/\r\n/, $reply) };
360 360
   };
361 361
 
  362
+  my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef;
  363
+
362 364
   my @cmd_args = @_;
363 365
   $self->__with_reconnect(sub {
364  
-    $self->__queue_cmd('INFO', 0, @cmd_args, $real_cb);
  366
+    $self->__run_cmd('INFO', 0, $custom_decode, $cb, @cmd_args);
365 367
   });
366  
-
367  
-  return 1 if $cb;
368  
-
369  
-  $self->wait_all_responses;
370  
-  return $self->__decode_info($info);
371 368
 }
372 369
 
373 370
 sub keys {
374 371
   my $self = shift;
375 372
   $self->__is_valid_command('KEYS');
376 373
 
377  
-  my $keys;
378  
-  my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef;
379  
-  my $real_cb = $cb ? sub {
380  
-    my ($reply, $error) = @_;
381  
-
382  
-    ## Support redis > 1.26
383  
-    my $decoded = $reply;
  374
+  my $custom_decode = sub {
  375
+    my ($reply, $synchronous_scalar) = @_;
384 376
 
385 377
     ## Support redis <= 1.2.6
386  
-    $decoded = [split(/\s/, $reply)] if defined $reply && !ref $reply;
  378
+    $reply = [split(/\s/, $reply)] if defined $reply && !ref $reply;
387 379
 
388  
-    $cb->($decoded, $error);
389  
-  } : sub {
390  
-    my ($reply, $error) = @_;
391  
-    confess "[KEYS] $error, " if defined $error;
392  
-    $keys = defined $reply && !ref $reply ? [split(/\s/, $reply)] : $reply;
  380
+    return ref $reply && ($synchronous_scalar || wantarray) ? @$reply : $reply;
393 381
   };
394 382
 
  383
+  my $cb = @_ && ref $_[-1] eq 'CODE' ? pop : undef;
  384
+
395 385
   my @cmd_args = @_;
396 386
   $self->__with_reconnect(sub {
397  
-    $self->__queue_cmd('KEYS', 0, @cmd_args, $real_cb);
  387
+    $self->__run_cmd('KEYS', 0, $custom_decode, $cb, @cmd_args);
398 388
   });
399  
-
400  
-  return 1 if $cb;
401  
-
402  
-  $self->wait_all_responses;
403  
-  return ref $keys eq 'ARRAY' ? @$keys : $keys;
404 389
 }
405 390
 
406 391
 

0 notes on commit 8c6f8c8

Please sign in to comment.
Something went wrong with that request. Please try again.