Skip to content

Instantly share code, notes, and snippets.

@soh335
Created April 30, 2010 14:59
Show Gist options
  • Save soh335/385316 to your computer and use it in GitHub Desktop.
Save soh335/385316 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Plack::Request;
use LWP::UserAgent;
use Cache::FastMmap;
my $cache = Cache::FastMmap->new;
return sub {
my $req = Plack::Request->new(shift);
if ( !$cache->get($req->request_uri) ) {
my $_req = HTTP::Request->new(
$req->method,
$req->request_uri,
$req->headers,
$req->raw_body
);
my $ua = LWP::UserAgent->new;
my $res = $ua->request( $_req );
my @res_header;
$res->headers->scan(sub{ push @res_header, @_; });
$cache->set($req->request_uri, [ $res->code, \@res_header, [$res->content] ]);
}
$cache->get($req->request_uri);
};
plackup -p 3000 -s AnyEvent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment