Skip to content
Snippets Groups Projects
Verified Commit 279764e7 authored by Andrej Ramašeuski's avatar Andrej Ramašeuski
Browse files

Pridan redis cache

parent 51f25247
No related branches found
No related tags found
No related merge requests found
Pipeline #2956 passed
......@@ -2,7 +2,7 @@ image: docker:19.03.12
variables:
DOCKER_TLS_CERTDIR: "/certs"
IMAGE_VER: 1.10.2
IMAGE_VER: 1.11.0
services:
- docker:19.03.12-dind
......
......@@ -18,6 +18,7 @@ RUN apt-get update && apt-get install -y \
RUN cpanm \
Mojolicious \
Mojo::Pg \
Mojo::Redis \
Mojo::JWT \
Mojolicious::Plugin::OpenAPI
......
......@@ -2,6 +2,7 @@ package CF;
use Mojo::Base 'Mojolicious';
use Mojo::Pg;
use Mojo::JWT;
use Mojo::Redis;
use CF::Schema;
# This method will run once at server start
......@@ -37,6 +38,10 @@ sub startup {
});
$self->helper( schema => sub { return $schema; } );
# Redis
my $redis = Mojo::Redis->new( $cfg->{redis} );
$self->helper( redis => sub { return $redis; } );
$self->plugin('CF::Helpers::Core');
$self->plugin('CF::Helpers::Auth');
......
package CF::Controller::SSO;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Pg::PubSub;
use feature 'signatures';
no warnings qw{ experimental::signatures };
use Mojo::Base 'Mojolicious::Controller';
use Mojo::Pg::PubSub;
use Mojo::UserAgent;
use JSON;
use constant REDIS_CACHE_LIFETIME => 300;
use constant REDIS_CACHE_KEY => 'SSO SUBJECTS %s';
sub subjects ($c) {
$c->openapi->valid_input or return;
my $args = $c->validation->output;
my $cache_key = sprintf(REDIS_CACHE_KEY, $args->{search});
if ($c->redis->db->exists( $cache_key )) {
$c->render(openapi => from_json($c->redis->db->get( $cache_key )));
return;
}
my ( $groups, $users, @subjects );
if ( $args->{class} =~ /all|group/ ) {
......@@ -20,6 +30,7 @@ sub subjects ($c) {
@subjects = (@subjects, $c->_users( $args->{search} ));
}
$c->redis->db->set( $cache_key, to_json(\@subjects), 'EX', REDIS_CACHE_LIFETIME );
$c->render(openapi => \@subjects);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment