æ¢ã«å¤ãã®åå è
ã®æ¹ã
ãããã°ã«æ¸ãã¦ããã¾ããã #ISUCON ã«åå ãã¾ããã
livedoor Techブログ : なんでもありのWebアプリケーション高速化バトル、#isucon 開催のお知らせ
livedoor Techブログ : ISUCONやりましたーっ! 最終結果発表 #isucon
以前参加したチューニンガソンãé¢ç½ãã£ãããã³ã¬ãåºãããªã¼ã¨æã£ã¦ç¤¾å
ã§èª¿æ´ãã¦ã¿ããããã¡ããã©ãã3人éã¾ã£ããã¨ãããã¨ã§@fujiwaraããã¨@songmuããã¨ä¸ç·ã«åå ãã¾ãããçµæã¯åªåï¼
#isucon で優勝してきました - 酒日記 はてな支店
#isucon ではどんなことを考えながら作業していたか - 酒日記 はてな支店
おそらくはそれさえも平凡な日々: #isucon で優勝させてもらってきました
ãäºäººã®ã¨ã³ããªã®éããfujiwaraããã¨songmuããã§ã¨ã¦ãå¹çããã¤ã¤ã«ã³ã¸ã«ãã¥ã¼ãã³ã°ãã¦ãã ãã£ã¦ãã¾ããã
åããã£ããã¨ã¨è¨ãã°ããµã¤ããã¼ãMySQLã«éãã¯ã¨ãªæããåå¾ããããmemcachedã«å
¥ãã¦ç®¡çããããã«ãã¨ä»¥ä¸ã®ãããªã³ã¼ããæ¸ãããããã§ããã
diff --git a/lib/Isucon.pm b/lib/Isucon.pm index e8d7b52..c0fff8c 100644 --- a/lib/Isucon.pm +++ b/lib/Isucon.pm @@ -6,6 +6,7 @@ use utf8; use Kossy; use DBI; use JSON; +use Cache::Memcached::Fast; our $VERSION = 0.01; @@ -31,13 +32,21 @@ sub dbh { }); } +my $memd; +sub cache { + my $self = shift; + return $memd if $memd; + my $config = $self->load_config; + $memd = Cache::Memcached::Fast->new({ + servers => $config->{servers}{memcached}, + }); +} + filter 'recent_commented_articles' => sub { my $app = shift; sub { my ( $self, $c ) = @_; - $c->stash->{recent_commented_articles} = $self->dbh->selectall_arrayref( - 'SELECT id, title FROM article ORDER BY comment_created_at DESC LIMIT 10', - { Slice => {} }); + $c->stash->{recent_commented_articles} = $self->cache->get('recent_commented_articles'); $app->($self,$c); } }; @@ -83,11 +92,26 @@ post '/comment/:articleid' => sub { $c->req->param('body') ); - $sth = $self->dbh->prepare('UPDATE article SET comment_created_at = NOW() WHERE id = ?'); - $sth->execute( - $c->args->{articleid}, + my $article = $self->dbh->selectrow_hashref( + 'SELECT title FROM article WHERE id=?', + {}, $c->args->{articleid} ); - + # get from cache + my $cached = $self->cache->get('recent_commented_articles'); + my $exists = { $c->args->{articleid} => 1 }; + my $new_data = [{ + id => $c->args->{articleid}, + title => $article->{title}, + }]; + # filtering and push + for my $data (@$cached) { + unless ($exists->{$data->{id}}) { + push @$new_data, $data; + $exists->{$data->{id}} = 1; + } + } + pop @$new_data if @$new_data > 10; + $self->cache->set('recent_commented_articles', $new_data); $c->redirect($c->req->uri_for('/article/'.$c->args->{articleid})); };
memcachedèªä½ã¯fujiwaraãããrevãµã¼ãã«ã¤ã³ã¹ãã¼ã«ãã¦ãã ãã£ã¦ããã®ã§ãhosts.jsonã«revãµã¼ãã®æ
å ±ã追è¨ããã ããæåãã£ãã·ã¥ã§ãã¦ãªãã¦ä½ã ããã¨æã£ããrevãµã¼ã11211ãã¼ããå¡ããã¦ãã¦ç¹ãããªãã£ãã ããä¸å¿ããã§åæãã¼ã¿ããã£ãã·ã¥ã«å
¥ã£ã¦ããã°POSTãããã¿ã¤ãã³ã°ã§ãµã¤ããã¼ãæ´æ°ãããã¯ãâ¦ã ã¨ã¯æã£ãã®ã ãã©ãå¾®å¦ã«å®å®ããªãã£ããããããããã£ãã·ã¥ããåããªãã£ãã¨ãã«ã¯é常éãDBããåãã«è¡ããã¨ãããããªãã§ã¤ã«ãªã¼ãã¼ãèããªãæ¼¢ãããã¯ã½è¨è¨ã§ä½ã£ã¦ãã¾ã£ãããããã³ãã³ãµã¤ããã¼ã丸ãã¨æ¶ããããªã©ã®ä¸å®å®ãªäºæ
ãèµ·ãããããã¯ã¤ã«ã³ãã¨ãããã¨ã§çµå±ãã¸ãã¯é¨åã¯å
¨é¨revertãããæ®ã£ãã®ã¯use Cache::Memcached::Fast;
ã®è¡ã¨C::M::Fã¤ã³ã¹ã¿ã³ã¹ãåå¾ããé¨åã ãã
ãã¨ã¯ä¸ç·ã«ç¸è«ã¯ãã¤ã¤ããã²ãããfujiwaraãããsongmuãããæãåããã¦ããã¨ãããè¦å®ã£ã¦ãã¾ããã
ã¨ã¯è¨ãæ¬å½ã«ã¹ã´ã人ãã¡ãå³æ¹ã¨ãã¦èããã¥ã¼ãã³ã°ãã¦ããæ§ãéè¿ã§ãªã¢ã«ã¿ã¤ã ã«è¦³ããã¨ãã§ãããã¨ããã®ãé常ã«è²´éãªä½é¨ã§ï¼ä¸ç·ã«ä»äºãã¦ããã¨ãã¦ããã¯ãåæ
ããããã¨ãå¤ããæä½ãä¸ç·ã«è¦ãªãããã¨ããæ©ä¼ã¯å°ãªãã§ããããï¼ã¨ã¦ãåºæ¿ã«ãªãã¾ããããã¼ã ã®æ¦åã«ãªãããå¦ãã¯ã¨ããããèªåã¨ãã¦ã¯å¾ããã®ãé常ã«å¤§ãããè¯ãæ©ä¼ã¨ãªãã¾ãããåªåã§ããã®ãè¯ãæãåºã«ãªãã¾ãããã
ãµã¼ãã®æºåããå½æ¥ã®éå¶ããã³ããã¼ã¯ãã¼ã«ããã£ããä½ããã¦ãã¦éä¸çµéãåºãã¤ã¤æå¾ã®å¤å®ã¾ã§ã¹ã ã¼ãºã«ãã¨ä¸»å¬ã®ã©ã¤ããã¢ã®çæ§ã¯ä¸¦ã
ãªãã¬å´åãããã¦åºé¡ãæºåãã·ãã¥ã¬ã¼ã·ã§ã³ããã¦ãããã¨ã¨æãã¾ããé常ã«è¯ãã¤ãã³ãã§ååã«æ¥½ãããã¨ãã§ãã¾ãããæ¬å½ã«ãããã¨ããããã¾ããï¼ åå è
ã®çæ§ããç²ããã¾ã§ããï¼ï¼
追è¨
å¤ãã®åå è
ã®æ¹ã
ãæ¯ãè¿ããæ¤è¨¼ãæ¸ãã¦ããã®ã§ãã¨ã¦ãåå¼·ã«ãªãã¾ãã
タグ「isucon」を検索 - はてなブックマーク