Verified Commit c91d46bc authored by Luc Didry's avatar Luc Didry
Browse files

Fix #83 Use Mojolicious::Plugin::Chi + Add memcached ability

parent 8dd2ab87
......@@ -4,8 +4,9 @@ stages:
- carton
- carton_bdd
- tests
- cover
before_script:
- rm -f *db
- rm -f *.db
variables:
POSTGRES_DB: lutim_db
POSTGRES_USER: lutim
......@@ -25,18 +26,22 @@ variables:
.sqlite_template: &sqlite_definition
stage: tests
retry: 2
artifacts:
paths:
- cover_db/
dependencies:
- carton_sqlite
coverage: '/Total .*\d+\.\d+$/'
.pg_template: &pg_definition
stage: tests
retry: 2
artifacts:
paths:
- cover_db/
dependencies:
- carton_postgresql
services:
- name: postgres:9.6
alias: postgres
coverage: '/Total .*\d+\.\d+$/'
### Podcheck
##
......@@ -56,7 +61,7 @@ carton:
- local/
dependencies: []
script:
- carton install --deployment --without=sqlite --without=postgresql --without=minion --without=cache
- carton install --deployment --without=sqlite --without=postgresql --without=minion --without=cache --without=memcached
when: always
retry: 2
......@@ -66,11 +71,11 @@ carton:
carton_sqlite:
<<: *carton_bdd_definition
script:
- carton install --deployment --without=postgresql --without=minion --without=cache --without=test
- carton install --deployment --without=postgresql --without=minion --without=cache --without=memcached
carton_postgresql:
<<: *carton_bdd_definition
script:
- carton install --deployment --without=sqlite --without=minion --without=cache --without=test
- carton install --deployment --without=sqlite --without=minion --without=cache --without=memcached
### SQLite tests
##
......@@ -78,17 +83,16 @@ carton_postgresql:
sqlite1:
<<: *sqlite_definition
script:
- carton install --deployment --without=postgresql --without=minion --without=cache --without=test
- carton install --deployment --without=postgresql --without=minion --without=cache --without=memcached
- MOJO_CONFIG=t/sqlite1.conf make test-sqlite
- MOJO_CONFIG=t/sqlite1.conf make watch
- MOJO_CONFIG=t/sqlite1.conf make cleanbdd
- MOJO_CONFIG=t/sqlite1.conf make cleanfiles
- MOJO_CONFIG=t/sqlite1.conf make stats
- MOJO_CONFIG=t/sqlite1.conf make cover
sqlite2:
<<: *sqlite_definition
script:
- carton install --deployment --without=postgresql --without=cache --without=test
- carton install --deployment --without=postgresql --without=cache --without=memcached
- MOJO_CONFIG=t/sqlite2.conf make minion &
- sleep 3
- MOJO_CONFIG=t/sqlite2.conf make test-sqlite
......@@ -96,14 +100,13 @@ sqlite2:
- MOJO_CONFIG=t/sqlite2.conf make cleanbdd
- MOJO_CONFIG=t/sqlite2.conf make cleanfiles
- MOJO_CONFIG=t/sqlite2.conf make stats
- MOJO_CONFIG=t/sqlite2.conf make cover
sqlite3:
<<: *sqlite_definition
services:
- name: postgres:9.6
alias: postgres
script:
- carton install --deployment --without=cache --without=test
- carton install --deployment --without=cache --without=memcached
- export PGPASSWORD=lutim_pwd; echo 'CREATE DATABASE lutim_minion WITH OWNER lutim;' | psql -h postgres -U lutim lutim_db
- MOJO_CONFIG=t/sqlite3.conf make minion &
- sleep 3
......@@ -112,7 +115,6 @@ sqlite3:
- MOJO_CONFIG=t/sqlite3.conf make cleanbdd
- MOJO_CONFIG=t/sqlite3.conf make cleanfiles
- MOJO_CONFIG=t/sqlite3.conf make stats
- MOJO_CONFIG=t/sqlite3.conf make cover
### PostgreSQL tests
##
......@@ -120,17 +122,16 @@ sqlite3:
postgresql1:
<<: *pg_definition
script:
- carton install --deployment --without=sqlite --without=minion --without=cache --without=test
- carton install --deployment --without=sqlite --without=minion --without=cache --without=memcached
- MOJO_CONFIG=t/postgresql1.conf make test-pg
- MOJO_CONFIG=t/postgresql1.conf make watch
- MOJO_CONFIG=t/postgresql1.conf make cleanbdd
- MOJO_CONFIG=t/postgresql1.conf make cleanfiles
- MOJO_CONFIG=t/postgresql1.conf make stats
- MOJO_CONFIG=t/postgresql1.conf make cover
postgresql2:
<<: *pg_definition
script:
- carton install --deployment --without=cache --without=test
- carton install --deployment --without=cache --without=memcached
- MOJO_CONFIG=t/postgresql2.conf make minion &
- sleep 3
- MOJO_CONFIG=t/postgresql2.conf make test-pg
......@@ -138,11 +139,10 @@ postgresql2:
- MOJO_CONFIG=t/postgresql2.conf make cleanbdd
- MOJO_CONFIG=t/postgresql2.conf make cleanfiles
- MOJO_CONFIG=t/postgresql2.conf make stats
- MOJO_CONFIG=t/postgresql2.conf make cover
postgresql3:
<<: *pg_definition
script:
- carton install --deployment --without=sqlite --without=cache --without=test
- carton install --deployment --without=sqlite --without=cache --without=memcached
- export PGPASSWORD=lutim_pwd; echo 'CREATE DATABASE lutim_minion WITH OWNER lutim;' | psql -h postgres -U lutim lutim_db
- MOJO_CONFIG=t/postgresql3.conf make minion &
- sleep 3
......@@ -151,4 +151,12 @@ postgresql3:
- MOJO_CONFIG=t/postgresql3.conf make cleanbdd
- MOJO_CONFIG=t/postgresql3.conf make cleanfiles
- MOJO_CONFIG=t/postgresql3.conf make stats
- MOJO_CONFIG=t/postgresql3.conf make cover
### Code coverage
##
#
cover:
stage: cover
script:
- make cover
coverage: '/Total .*\d+\.\d+$/'
......@@ -34,9 +34,14 @@ feature 'minion', 'Minion support' => sub {
requires 'Minion';
};
feature 'cache', 'Cache system' => sub {
requires 'CHI';
requires 'Mojolicious::Plugin::CHI';
requires 'Data::Serializer';
};
feature 'memcached', 'Cache system using Memcached' => sub {
requires 'Mojolicious::Plugin::CHI';
requires 'CHI::Driver::Memcached';
requires 'Cache::Memcached';
};
feature 'test' => sub {
requires 'Devel::Cover';
};
This diff is collapsed.
......@@ -52,21 +52,32 @@ sub startup {
db_path => 'minion.db'
},
cache_max_size => 0,
memcached_servers => [],
quiet_logs => 0,
disable_img_stats => 0,
}
});
if ($config->{cache_max_size} != 0) {
require CHI;
if (scalar(@{$config->{memcached_servers}})) {
$self->plugin(CHI => {
lutim_images_cache => {
driver => 'Memcached',
servers => $config->{memcached_servers},
expires_in => '1 day',
expires_on_backend => 1,
}
});
} elsif ($config->{cache_max_size} != 0) {
my $cache_max_size = 8 * 1024 * 1024 * $config->{cache_max_size};
$self->{images_cache} = CHI->new(
driver => 'Memory',
global => 1,
is_size_aware => 1,
max_size => $cache_max_size,
expires_in => '1 day'
);
$self->plugin(CHI => {
lutim_images_cache => {
driver => 'Memory',
global => 1,
is_size_aware => 1,
max_size => $cache_max_size,
expires_in => '1 day'
}
});
}
die "You need to provide a contact information in lutim.conf !" unless (defined($config->{contact}));
......@@ -241,22 +252,22 @@ sub startup {
to('Controller#get_counter')->
name('counter');
$r->get('/about/(:short).(:f)')->
$r->get('/about/<:short>.<:f>')->
to('Controller#about_img')->
name('about_img');
$r->get('/about/:short/(:key).(:f)')->
$r->get('/about/:short/<:key>.<:f>')->
to('Controller#about_img')->
name('about_img');
$r->get('/(:short).(:f)')->
$r->get('/<:short>.<:f>')->
to('Controller#short')->
name('short');
$r->get('/:short')->
to('Controller#short');
$r->get('/:short/(:key).(:f)')->
$r->get('/:short/<:key>.<:f>')->
to('Controller#short');
$r->get('/:short/:key')->
......
......@@ -99,39 +99,38 @@ sub _render_file {
$c->res->content->headers($headers);
my $cache;
if ($c->config('cache_max_size') != 0) {
$cache = $c->app->{images_cache}->compute($img->short, undef, sub {
if ($c->config('cache_max_size') != 0 || scalar(@{$c->config('memcached_servers')})) {
$cache = $c->chi('lutim_images_cache')->compute($img->short, undef, sub {
if ($key) {
return {
asset => $c->decrypt($key, $path, $iv),
key => $key
asset => $c->decrypt($key, $path, $iv),
key => $key
};
} else {
return {
asset => Mojo::File->new($path)->slurp,
asset => Mojo::File->new($path)->slurp,
};
}
});
if ($key && $key ne $cache->{key}) {
my $tmp = $c->decrypt($key, $path, $iv);
$cache->{asset} = $tmp;
$c->app->{images_cache}->replace(
$c->chi('lutim_images_cache')->replace(
$img->short,
{
asset => $tmp,
key => $key
asset => $tmp,
key => $key
},
);
}
} else {
if ($key) {
$cache = {
asset => $c->decrypt($key, $path, $iv),
key => $key
asset => $c->decrypt($key, $path, $iv),
};
} else {
$cache = {
asset => Mojo::File->new($path)->slurp,
asset => Mojo::File->new($path)->slurp,
};
}
}
......@@ -307,8 +306,8 @@ sub _decrypt {
sub _delete_image {
my $c = shift;
my $img = shift;
if ($c->config('cache_max_size') != 0 && $c->app->{images_cache}) {
$c->app->{images_cache}->remove($img->short);
if ($c->config('cache_max_size') != 0 || scalar(@{$c->config('memcached_servers')})) {
$c->chi('lutim_images_cache')->remove($img->short);
}
unlink $img->path or warn "Could not unlink ".$img->path.": $!";
$img->disable();
......
......@@ -47,6 +47,7 @@ sub startup {
db_path => 'minion.db'
},
cache_max_size => 0,
memcached_servers => [],
quiet_logs => 0,
disable_img_stats => 0,
}
......
......@@ -191,6 +191,15 @@
# optional, default is 0
#cache_max_size => 0,
# array of memcached servers to cache URL in order to accelerate responses to often-viewed URL.
# If set to [], the use of memcached is disabled.
# If you use memcached, the internal cache (see cache_max_size option above) will not be used.
# Please see https://framagit.org/luc/lutim/wikis/memcached to know how to configure your memcached
# servers.
# exemple of valid value: ['127.0.0.1:11211']
# optional, default is []
#memcached_servers => [],
# enable or disable Lutim built-in logs
# set to 1 to disable logs
# optional, default is 0
......
......@@ -182,6 +182,15 @@
# optional, default is 0
#cache_max_size => 0,
# array of memcached servers to cache URL in order to accelerate responses to often-viewed URL.
# If set to [], the use of memcached is disabled.
# If you use memcached, the internal cache (see cache_max_size option above) will not be used.
# Please see https://framagit.org/luc/lutim/wikis/memcached to know how to configure your memcached
# servers.
# exemple of valid value: ['127.0.0.1:11211']
# optional, default is []
#memcached_servers => [],
##########################
# Lutim cron jobs settings
##########################
......
......@@ -182,6 +182,15 @@
# optional, default is 0
#cache_max_size => 0,
# array of memcached servers to cache URL in order to accelerate responses to often-viewed URL.
# If set to [], the use of memcached is disabled.
# If you use memcached, the internal cache (see cache_max_size option above) will not be used.
# Please see https://framagit.org/luc/lutim/wikis/memcached to know how to configure your memcached
# servers.
# exemple of valid value: ['127.0.0.1:11211']
# optional, default is []
#memcached_servers => [],
##########################
# Lutim cron jobs settings
##########################
......
......@@ -182,6 +182,15 @@
# optional, default is 0
#cache_max_size => 0,
# array of memcached servers to cache URL in order to accelerate responses to often-viewed URL.
# If set to [], the use of memcached is disabled.
# If you use memcached, the internal cache (see cache_max_size option above) will not be used.
# Please see https://framagit.org/luc/lutim/wikis/memcached to know how to configure your memcached
# servers.
# exemple of valid value: ['127.0.0.1:11211']
# optional, default is []
#memcached_servers => [],
##########################
# Lutim cron jobs settings
##########################
......
......@@ -182,6 +182,15 @@
# optional, default is 0
#cache_max_size => 0,
# array of memcached servers to cache URL in order to accelerate responses to often-viewed URL.
# If set to [], the use of memcached is disabled.
# If you use memcached, the internal cache (see cache_max_size option above) will not be used.
# Please see https://framagit.org/luc/lutim/wikis/memcached to know how to configure your memcached
# servers.
# exemple of valid value: ['127.0.0.1:11211']
# optional, default is []
#memcached_servers => [],
##########################
# Lutim cron jobs settings
##########################
......
......@@ -182,6 +182,15 @@
# optional, default is 0
#cache_max_size => 0,
# array of memcached servers to cache URL in order to accelerate responses to often-viewed URL.
# If set to [], the use of memcached is disabled.
# If you use memcached, the internal cache (see cache_max_size option above) will not be used.
# Please see https://framagit.org/luc/lutim/wikis/memcached to know how to configure your memcached
# servers.
# exemple of valid value: ['127.0.0.1:11211']
# optional, default is []
#memcached_servers => [],
##########################
# Lutim cron jobs settings
##########################
......
......@@ -182,6 +182,15 @@
# optional, default is 0
#cache_max_size => 0,
# array of memcached servers to cache URL in order to accelerate responses to often-viewed URL.
# If set to [], the use of memcached is disabled.
# If you use memcached, the internal cache (see cache_max_size option above) will not be used.
# Please see https://framagit.org/luc/lutim/wikis/memcached to know how to configure your memcached
# servers.
# exemple of valid value: ['127.0.0.1:11211']
# optional, default is []
#memcached_servers => [],
##########################
# Lutim cron jobs settings
##########################
......
......@@ -32,11 +32,11 @@ msgstr ""
msgid "-or-"
msgstr ""
#: lib/Lutim.pm:203 lib/Lutim/Command/cron/stats.pm:151 lib/Lutim/Command/cron/stats.pm:162 lib/Lutim/Command/cron/stats.pm:179 themes/default/templates/index.html.ep:5 themes/default/templates/raw.html.ep:10 themes/default/templates/raw.html.ep:21 themes/default/templates/raw.html.ep:38
#: lib/Lutim.pm:214 lib/Lutim/Command/cron/stats.pm:151 lib/Lutim/Command/cron/stats.pm:162 lib/Lutim/Command/cron/stats.pm:179 themes/default/templates/index.html.ep:5 themes/default/templates/raw.html.ep:10 themes/default/templates/raw.html.ep:21 themes/default/templates/raw.html.ep:38
msgid "1 year"
msgstr ""
#: lib/Lutim.pm:202 lib/Lutim/Command/cron/stats.pm:148 lib/Lutim/Command/cron/stats.pm:159 lib/Lutim/Command/cron/stats.pm:176 themes/default/templates/index.html.ep:4 themes/default/templates/partial/for_my_delay.html.ep:13 themes/default/templates/partial/lutim.js.ep:151 themes/default/templates/raw.html.ep:18 themes/default/templates/raw.html.ep:35 themes/default/templates/raw.html.ep:7
#: lib/Lutim.pm:213 lib/Lutim/Command/cron/stats.pm:148 lib/Lutim/Command/cron/stats.pm:159 lib/Lutim/Command/cron/stats.pm:176 themes/default/templates/index.html.ep:4 themes/default/templates/partial/for_my_delay.html.ep:13 themes/default/templates/partial/lutim.js.ep:151 themes/default/templates/raw.html.ep:18 themes/default/templates/raw.html.ep:35 themes/default/templates/raw.html.ep:7
msgid "24 hours"
msgstr ""
......@@ -443,7 +443,7 @@ msgid "Uploaded files by days"
msgstr ""
#. ($c->app->config('contact')
#: lib/Lutim/Plugin/Helpers.pm:216
#: lib/Lutim/Plugin/Helpers.pm:217
msgid "Uploading is currently disabled, please try later or contact the administrator (%1)."
msgstr ""
......@@ -495,7 +495,7 @@ msgstr ""
msgid "core developer"
msgstr ""
#: lib/Lutim.pm:201 lib/Lutim/Command/cron/stats.pm:147 lib/Lutim/Command/cron/stats.pm:158 lib/Lutim/Command/cron/stats.pm:175 themes/default/templates/index.html.ep:3 themes/default/templates/raw.html.ep:17 themes/default/templates/raw.html.ep:34 themes/default/templates/raw.html.ep:6
#: lib/Lutim.pm:212 lib/Lutim/Command/cron/stats.pm:147 lib/Lutim/Command/cron/stats.pm:158 lib/Lutim/Command/cron/stats.pm:175 themes/default/templates/index.html.ep:3 themes/default/templates/raw.html.ep:17 themes/default/templates/raw.html.ep:34 themes/default/templates/raw.html.ep:6
msgid "no time limit"
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