Skip to content

Using Redis

Stefan Adams edited this page Feb 15, 2015 · 5 revisions

Using Redis in Mojolicious

Asynchronous using Mojo::Redi2

See also Mojo::Redis2

Asynchronous using MojoX::Redis

MojoX::Redis can be used for asynchronous communication with Redis.

    $redis = MojoX::Redis->new(ioloop => Mojo::IOLoop->new);

    $redis->set(test => "test_ok")->get(
        test => sub {
            my ($redis, $result) = @_;

            die $redis->error unless defined $result;

            print qq|"result" = "|, $result->[0], qq|"\n|;

Blocking using Redis

Sometimes a simple, blocking client is enough.

    use Mojolicious::Lite;
    use Redis;

    my $redis = Redis->new;

    # This under block is only needed, if your
    # connections time out. Check the settings
    # of your database.
    under sub {
      $redis = Redis->new;

    get '/' => sub {
      my $self = shift;
      my $counter = $redis->incr('counter');
      $self->render(text => "Counter: $counter!");
Something went wrong with that request. Please try again.