Verified Commit fa0ff2ce authored by Luc Didry's avatar Luc Didry

Use CHI for cache system

This commit is dedicated to Obrow, who is supporting me with Ğ1.
Many thanks :-)
parent 477242e5
......@@ -3,8 +3,9 @@ Revision history for Perl application Lstu
0.12 ????-??-??
- Improve LDAP User Authentication Support (beware of configuration
changes!) <Ira W. Snyder>
- Allow users to configure maximum number of database connections
- Allow users to configure maximum number of database connections
<Ira W. Snyder>
- Use CHI for cache system
- Update dependencies
- Fix bugs
......
......@@ -21,3 +21,5 @@ requires 'IO::Socket::SSL';
requires 'Net::LDAP';
requires 'Apache::Htpasswd';
requires 'Image::PNG::QRCode';
requires 'CHI';
requires 'Data::Serializer';
This diff is collapsed.
......@@ -5,6 +5,7 @@ use Mojo::JSON;
use Net::LDAP;
use Apache::Htpasswd;
use Lstu::DB::URL;
use CHI;
$ENV{MOJO_REVERSE_PROXY} = 1;
......@@ -30,6 +31,7 @@ sub startup {
dbtype => 'sqlite',
max_redir => 2,
skip_spamhaus => 0,
cache_max_size => 2,
}
});
......@@ -53,6 +55,16 @@ sub startup {
eval qq(use lib "$lib");
$self->plugin('I18N');
# Cache
my $cache_max_size = ($config->{cache_max_size} > 0) ? 8 * 1024 * 1024 * $config->{cache_max_size} : 1;
$self->{urls_cache} = CHI->new(
driver => 'Memory',
global => 1,
is_size_aware => 1,
max_size => $cache_max_size,
expires_in => '1 day'
);
# Debug
$self->plugin('DebugDumperHelper');
......
......@@ -287,16 +287,11 @@ sub get {
my $c = shift;
my $short = $c->param('short');
my ($url, $db_url);
if (defined $c->cache->{$short}) {
$url = $c->cache->{$short}->{url};
} else {
$db_url = Lstu::DB::URL->new(
app => $c,
short => $short
);
$url = $db_url->url;
}
my $url = $c->app->{urls_cache}->compute($short, undef, sub {
return Lstu::DB::URL->new(app => $c, short => $short)->url;
});
$c->debug($c->app->{urls_cache}->get_keys( ));
if ($url) {
$c->respond_to(
json => { json => { success => Mojo::JSON->true, url => $url } },
......@@ -308,20 +303,10 @@ sub get {
);
# Update counter
$c->on(finish => sub {
$c->cache->{$short} = {
last_used => time,
url => $url
};
if ($c->config('minion')->{enabled} && $c->config('minion')->{db_path}) {
$c->app->minion->enqueue(increase_counter => [$short, $c->{url}]);
} else {
$db_url = Lstu::DB::URL->new(
app => $c,
short => $short
) if (defined $c->cache->{$short});
$db_url->increment_counter;
Lstu::DB::URL->new(app => $c, short => $short)->increment_counter;
my $piwik = $c->config('piwik');
if (defined($piwik) && $piwik->{idsite} && $piwik->{url}) {
......@@ -335,7 +320,6 @@ sub get {
}
}
$c->clear_cache;
});
} else {
my $msg = $c->l('The shortened URL %1 doesn\'t exist.', $c->url_for('/')->to_abs.$short);
......
......@@ -33,8 +33,6 @@ sub register {
}
}
$app->helper(cache => \&_cache);
$app->helper(clear_cache => \&_clear_cache);
$app->helper(ip => \&_ip);
$app->helper(provisioning => \&_provisioning);
$app->helper(prefix => \&_prefix);
......@@ -71,22 +69,6 @@ sub _mysql {
return $mysql;
}
sub _cache {
my $c = shift;
state $cache = {};
}
sub _clear_cache {
my $c = shift;
my $cache = $c->cache;
my @keys = keys %{$cache};
my $limit = ($c->app->mode eq 'production') ? 500 : 1;
map {delete $cache->{$_};} @keys if (scalar(@keys) > $limit);
}
sub _ip {
my $c = shift;
......
......@@ -180,4 +180,15 @@
# set to 1 to skip SpamHaus check (not recommended)
# optional, default is 0
#skip_spamhaus => 0,
# maximum size (in MB) of memory allowed for the URL cache
# Lstu has a built-in memory-based URL cache to accelerate responses to often-viewed URL.
# This setting makes the cache remove oldest viewed image if the cache size is over it.
# WARNING: a cache is created for each hypnotoad worker, which by default is twice the number of
# CPUs you have. See http://mojolicious.org/perldoc/Mojo/Server/Hypnotoad#workers for details
# So, if you have 4 workers and set cache_max_size to 10, the real maximum size of RAM used for
# cache is 40MB.
# If set to 0, the cache is disabled
# optional, default is 2
#cache_max_size => 2,
};
......@@ -154,7 +154,7 @@ msgstr ""
msgid "Password"
msgstr ""
#: lib/Lstu.pm:275 themes/default/templates/api.html.ep:23
#: lib/Lstu.pm:287 themes/default/templates/api.html.ep:23
msgid "Please, check your credentials: unable to authenticate."
msgstr ""
......@@ -195,22 +195,22 @@ msgid "Statistics"
msgstr ""
#. ($url->host)
#: lib/Lstu/Plugin/Helpers.pm:161
#: lib/Lstu/Plugin/Helpers.pm:143
msgid "The URL host or one of its redirection(s) (%1) is blacklisted at Spamhaus. I refuse to shorten it."
msgstr ""
#. ($c->config('max_redir')
#: lib/Lstu/Plugin/Helpers.pm:177
#: lib/Lstu/Plugin/Helpers.pm:159
msgid "The URL redirects %1 times or most. It's most likely a dangerous URL (spam, phishing, etc.). I refuse to shorten it."
msgstr ""
#. ($url->host)
#: lib/Lstu/Plugin/Helpers.pm:153
#: lib/Lstu/Plugin/Helpers.pm:135
msgid "The URL you want to shorten comes from a domain (%1) that is blacklisted on this server (usually because of spammers that use this domain)."
msgstr ""
#. ($c->url_for('/')
#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Lstu.pm:281 lib/Lstu/Controller/Lstu.pm:341
#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Lstu.pm:281 lib/Lstu/Controller/Lstu.pm:325
msgid "The shortened URL %1 doesn't exist."
msgstr ""
......@@ -249,11 +249,11 @@ msgstr ""
msgid "You asked to shorten too many URLs too quickly. You're banned for %1 hour(s)."
msgstr ""
#: lib/Lstu.pm:266 lib/Lstu/Controller/Admin.pm:52 themes/default/templates/api.html.ep:126 themes/default/templates/api.html.ep:22
#: lib/Lstu.pm:278 lib/Lstu/Controller/Admin.pm:52 themes/default/templates/api.html.ep:126 themes/default/templates/api.html.ep:22
msgid "You have been successfully logged in."
msgstr ""
#: lib/Lstu.pm:305 lib/Lstu/Controller/Admin.pm:73 themes/default/templates/api.html.ep:134 themes/default/templates/api.html.ep:36 themes/default/templates/logout.html.ep:3
#: lib/Lstu.pm:317 lib/Lstu/Controller/Admin.pm:73 themes/default/templates/api.html.ep:134 themes/default/templates/api.html.ep:36 themes/default/templates/logout.html.ep:3
msgid "You have been successfully logged out."
msgstr ""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment