Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement cacheing

  • Loading branch information...
commit 8f3a3348ae272b33221f5b913dca568b335a63ce 1 parent 6fc17a3
@kappa authored
Showing with 27 additions and 16 deletions.
  1. +27 −16 cpanhubble.pm
View
43 cpanhubble.pm
@@ -4,33 +4,44 @@ use Dancer;
use lib './lib';
use CpanHub;
use AnyEvent;
+use Encode;
+use CHI;
+
+my $cache = CHI->new(driver => 'FastMmap', root_dir => '/tmp/hubble-cache', cache_size => '10m');
get '/' => sub {
- template 'index';
+ template 'index'; # XXX
};
get '/search' => sub {
my ($cpan, $gh);
- my $cv = AnyEvent->condvar;
- $cv->begin;
- cpan_search_req(params->{q}, sub {
- $cpan = $_[0];
- $cv->end;
- });
+ my $cache_miss;
+ my $res = $cache->get(params->{q});
+ unless ($res) {
+ $cache_miss = 1;
+
+ my $cv = AnyEvent->condvar;
+ $cv->begin;
+ cpan_search_req(params->{q}, sub {
+ $cpan = $_[0];
+ $cv->end;
+ });
+
+ $cv->begin;
+ github_search_req(params->{q}, sub {
+ $gh = $_[0];
+ $cv->end;
+ });
- $cv->begin;
- github_search_req(params->{q}, sub {
- $gh = $_[0];
- $cv->end;
- });
+ $cv->recv;
- $cv->recv;
+ $res = merge_cpan_and_github($cpan, $gh, params->{q});
- my $res =
- merge_cpan_and_github($cpan, $gh, params->{q});
+ $cache->set(params->{q}, $res, '2 days');
+ }
- template 'serp', { res => $res, query => params->{q} };
+ template 'serp', { res => $res, cache_miss => $cache_miss };
};
true;
Please sign in to comment.
Something went wrong with that request. Please try again.