Skip to content

Instantly share code, notes, and snippets.

@scmorrison
Last active February 27, 2017 15:30
Show Gist options
  • Save scmorrison/e48f4352ccd3ef2609aa7cd3944706ea to your computer and use it in GitHub Desktop.
Save scmorrison/e48f4352ccd3ef2609aa7cd3944706ea to your computer and use it in GitHub Desktop.
Bailador + Cache::LRU
#!/usr/bin/env perl6
use v6;
use lib 'lib';
use Cache::LRU;
use Bailador;
Bailador::import(rootdir => "./examples");
my $cache = Cache::LRU.new(size => 1024);
sub template-cache($template-name, %params) {
my $key = [$template-name, |%params.kv].join('-');
my $content = $cache.get($key);
return $content if $content.defined;
return $cache.set($key, template('tmpl.tt', %params) );
}
get / ^ '/template/' (.+) $ / => sub ($x) {
template-cache('tmpl.tt', { name => $x });
}
baile;
# Test with ab:
#
# ab -n 1000 -c 10 http://0.0.0.0:3000/template/bailador
#
# with cache:
# Requests per second: 165.66 [#/sec] (mean)
# without cache:
# Requests per second: 23.88 [#/sec] (mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment