Skip to content

Commit

Permalink
Support streaming API
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Dec 25, 2013
1 parent 73c962c commit 2bc43ba
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/Plack/Middleware/Cached.pm
Expand Up @@ -56,6 +56,32 @@ sub call {

# pass through and cache afterwards
my $response = $self->app_code->($env);

return $self->response_cb($response, sub {
my ($ret) = @_;
my $seen;
my $body = '';
return sub {
my ($chunk) = @_;
if ($seen++ and not defined $chunk) {
my $new_response = [ $ret->[0], $ret->[1], [ $body ] ];
$self->cache_reponse($key, $new_response, $env);
return;
}
$body .= $chunk if defined $chunk;
return $chunk;
};
}) if ref $response eq 'CODE';

$self->cache_reponse($key, $response, $env);

return $response;
}


sub cache_reponse {
my ($self, $key, $response, $env) = @_;

my @options = $self->set->($response, $env);
if (@options and $options[0]) {
$options[0] = [ $options[0] ];
Expand All @@ -68,8 +94,6 @@ sub call {
}
$self->cache->set( $key, @options );
}

return $response;
}

# allows caching PSGI-like applications not derived from Plack::Component
Expand Down

0 comments on commit 2bc43ba

Please sign in to comment.