/
cpanhubble.pm
47 lines (35 loc) · 920 Bytes
/
cpanhubble.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cpanhubble;
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';
};
get '/search' => sub {
my ($cpan, $gh);
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->recv;
$res = merge_cpan_and_github($cpan, $gh, params->{q});
$cache->set(params->{q}, $res, '2 days');
}
template 'serp', { res => $res, cache_miss => $cache_miss };
};
true;