Skip to content
Permalink
Browse files

tableをcacheするように

  • Loading branch information
typester committed Nov 3, 2012
1 parent 0ad272d commit 765ede5f5f7f843931f29d86911987dda640525c
Showing with 51 additions and 17 deletions.
  1. +33 −2 app/perl/lib/Isucon2.pm
  2. +1 −15 app/perl/views/ticket.tx
  3. +17 −0 app/perl/views/ticket_table.tx
@@ -8,6 +8,7 @@ use Kossy;
use DBIx::Sunny;
use JSON::XS;
use Redis;
use Encode;

sub load_config {
my $self = shift;
@@ -127,17 +128,47 @@ get '/ticket/:ticketid' => [qw(recent_sold)] => sub {
'SELECT id, name FROM variation WHERE ticket_id = ? ORDER BY id',
$ticket->{id},
);

for my $variation (@$variations) {
my @stocks = $self->redis->smembers('stock:' . $variation->{id});
$variation->{stock}{$_} = 1 for @stocks;
$variation->{vacancy} = $self->redis->scard('stock:' . $variation->{id});
}

$c->render('ticket.tx', {
ticket => $ticket,
variations => $variations,
table => decode_utf8 $self->redis->get('table_cache:' . $ticket->{id}),
});
};

post '/ticket/update_table_cache' => sub {
my ($self, $c) = @_;

my $tickets = $self->dbh->select_all(
'SELECT id FROM ticket',
);

for my $ticket_id (map { $_->{id} } @$tickets) {
my $variations = $self->dbh->select_all(
'SELECT id, name FROM variation WHERE ticket_id = ?', $ticket_id,
);

for my $variation (@$variations) {
my @stocks = $self->redis->smembers('stock:' . $variation->{id});
$variation->{stock}{$_} = 1 for @stocks;
$variation->{vacancy} = $self->redis->scard('stock:' . $variation->{id});
}

my $res = $c->render('ticket_table.tx', {
variations => $variations,
});

$self->redis->set('table_cache:' . $ticket_id, encode_utf8 $res->body);
}

$c->res->body('');
$c->res;
};

post '/buy' => sub {
my ($self, $c) = @_;
my $variation_id = $c->req->param('variation_id');
@@ -17,19 +17,5 @@
</ul>

<h3>席状況</h3>
: macro to_02d -> $i { $i < 10 ? '0' ~ $i : $i }
: for $variations -> $variation {
: my $stock = $variation.stock;
<h4><: $variation.name :></h4>
<table class="seats" data-variationid="<: $variation.id :>">
: for [0 .. 63] -> $row {
<tr>
: for [0 .. 63] -> $col {
: my $key = to_02d($row) ~ '-' ~ to_02d($col)
<td id="<: $key :>" class="<: $stock[$key] ? 'available' : 'unavailable' :>"></td>
: }
</tr>
: }
</table>
: }
<: $table | mark_raw:>
: }
@@ -0,0 +1,17 @@
: macro to_02d -> $i { $i < 10 ? '0' ~ $i : $i }
: for $variations -> $variation {
: my $stock = $variation.stock;
<h4><: $variation.name :></h4>
<table class="seats" data-variationid="<: $variation.id :>">
: for [0 .. 63] -> $row {
<tr>
: for [0 .. 63] -> $col {
: my $key = to_02d($row) ~ '-' ~ to_02d($col)
<td id="<: $key :>" class="<: $stock[$key] ? 'available' : 'unavailable' :>"></td>
: }
</tr>
: }
</table>
: }


0 comments on commit 765ede5

Please sign in to comment.
You can’t perform that action at this time.