Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Created August 2, 2013 05:14
Show Gist options
  • Save kazeburo/6137664 to your computer and use it in GitHub Desktop.
Save kazeburo/6137664 to your computer and use it in GitHub Desktop.
diff --git a/webapp/perl/cpanfile b/webapp/perl/cpanfile
index e3d6103..944b12e 100644
--- a/webapp/perl/cpanfile
+++ b/webapp/perl/cpanfile
@@ -2,3 +2,6 @@ requires 'Kossy' => '0.12';
requires 'DBIx::Sunny' => '0.17';
requires 'DBD::mysql' => '4.022';
requires 'Starman' => '0.3003';
+requires 'Starlet';
+requires 'Cache::Memory::Simple';
+
diff --git a/webapp/perl/lib/Isucon2.pm b/webapp/perl/lib/Isucon2.pm
index 4b4121c..e27a84a 100644
--- a/webapp/perl/lib/Isucon2.pm
+++ b/webapp/perl/lib/Isucon2.pm
@@ -7,6 +7,7 @@ use Kossy;
use DBIx::Sunny;
use JSON 'decode_json';
+use Cache::Memory::Simple;
our $VERSION = '0.01';
@@ -54,7 +55,18 @@ filter 'recent_sold' => sub {
}
};
-get '/' => [qw(recent_sold)] => sub {
+my $cache = Cache::Memory::Simple->new();
+filter 'page_cache' => sub {
+ my ($app) = @_;
+ sub {
+ my ($self, $c) = @_;
+ $cache->get_or_set($c->req->uri, sub {
+ $app->($self, $c);
+ },0.9);
+ };
+};
+
+get '/' => [qw(page_cache recent_sold)] => sub {
my ($self, $c) = @_;
my $rows = $self->dbh->select_all(
'SELECT * FROM artist ORDER BY id',
@@ -62,7 +74,7 @@ get '/' => [qw(recent_sold)] => sub {
$c->render('index.tx', { artists => $rows });
};
-get '/artist/:artistid' => [qw(recent_sold)] => sub {
+get '/artist/:artistid' => [qw(page_cache recent_sold)] => sub {
my ($self, $c) = @_;
my $artist = $self->dbh->select_row(
'SELECT id, name FROM artist WHERE id = ? LIMIT 1',
@@ -87,7 +99,7 @@ get '/artist/:artistid' => [qw(recent_sold)] => sub {
});
};
-get '/ticket/:ticketid' => [qw(recent_sold)] => sub {
+get '/ticket/:ticketid' => [qw(page_cache recent_sold)] => sub {
my ($self, $c) = @_;
my $ticket = $self->dbh->select_row(
'SELECT t.*, a.name AS artist_name FROM ticket t INNER JOIN artist a ON t.artist_id = a.id WHERE t.id = ? LIMIT 1',
worker_processes 1;
error_log stderr;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 5000;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:5005;
}
location ~ ^/(css|js|images) {
root /home/isu-user/isucon2/webapp/staticfiles;
}
}
}
carton exec -- plackup -s Starlet -E production --max-workers 3 --max-reqs-per-child 10000 -p 5005 app.psgi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment