Skip to content

Instantly share code, notes, and snippets.

@dragon3
Created October 26, 2010 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dragon3/646562 to your computer and use it in GitHub Desktop.
Save dragon3/646562 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Plack::Request;
use Plack::Response;
use AnyEvent::HTTP;
use Cache::FastMmap;
my $cache = Cache::FastMmap->new( expire_time => "1h" );
my $app = sub {
my $env = shift;
my $req = Plack::Request->new($env);
my ( $id, $res );
if ( $req->path_info =~ m|/([^/]+)$| ) {
$id = $1;
if ( my $img = $cache->get($id) ) {
return sub {
my $respond = shift;
$respond->(
[ 200, [ %{ $img->{headers} } ], [ $img->{body} ] ] );
};
}
my $url = "http://img.tweetimag.es/i/${id}_b";
return sub {
my $respond = shift;
http_get $url, sub {
my ( $body, $headers ) = @_;
$respond->( [ 200, [ %{$headers} ], [$body] ] );
$cache->set( $id, { "headers" => $headers, body => $body } );
};
};
}
else {
$res = Plack::Response->new(404);
return $res->finalize;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment