Coder Social home page Coder Social logo

mojo-redis's Introduction

NAME

Mojo::Redis - asynchronous Redis client for Mojolicious.

SYNOPSIS

use Mojo::Redis;

my $redis = Mojo::Redis->new(server => '127.0.0.1:6379');

# Execute some commands
$redis->ping(
    sub {
        my ($redis, $res) = @_;

        if ($res) {
            print "Got result: ", $res->[0], "\n";
        }
        else {
            print "Error: ", $redis->error, "\n";
        }
    }
);

# Work with keys
$redis->set(key => 'value');

$redis->get(
    key => sub {
        my ($redis, $res) = @_;

        print "Value of 'key' is $res->[0]\n";
    }
);


# Cleanup connection
$redis->quit(sub { shift->stop });

# Start IOLoop (in case it is not started yet)
$redis->start;

Create new Mojo::IOLoop instance if you need to get blocked in a Mojolicious application.

use Mojolicious::Lite;
use Mojo::Redis;

get '/' => sub {
    my $self = shift;

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

    my $value;

    $redis->set(foo => 'bar')->get(
        foo => sub {
            my ($redis, $result) = @_;

            $redis->quit->stop;

            return app->log->error($redis->error) unless $result;

            $value = $result->[0];
        }
    )->start;

    $self->render(text => qq(Foo value is "$value"));
};

app->start;

DESCRIPTION

Mojo::Redis is an asynchronous client to Redis for Mojo.

ATTRIBUTES

Mojo::Redis implements the following attributes.

server

my $server = $redis->server;
$redis     = $redis->server('127.0.0.1:6379');

Redis server connection string, defaults to '127.0.0.1:6379'.

ioloop

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

Loop object to use for io operations, by default a Mojo::IOLoop singleton object will be used.

timeout

my $timeout = $redis->timeout;
$redis      = $redis->timeout(100);

Maximum amount of time in seconds a connection can be inactive before being dropped, defaults to 300.

encoding

my $encoding = $redis->encoding;
$redis       = $redis->encoding('UTF-8');

Encoding used for stored data, defaults to UTF-8.

protocol_redis

use Protocol::Redis::XS;
$redis->protocol_redis("Protocol::Redis::XS");

Protocol::Redis implementation' constructor for parsing. By default Protocol::Redis will be used. Parser library must support APIv1.

Using Protocol::Redis::XS instead of default choice can speedup parsing.

METHODS

Mojo::Redis supports Redis' methods.

$redis->set(key => 'value);
$redis->get(key => sub { ... });

For more details take a look at execute method.

Also Mojo::Redis implements the following ones.

connect

$redis = $redis->connect;

Connect to Redis server.

execute

$redis = $redis->execute("ping" => sub{
    my ($redis, $result) = @_;

    # Process result
});
$redis->execute(lrange => ["test", 0, -1] => sub {...});
$redis->execute(set => [test => "test_ok"]);

Execute specified command on Redis server. If error occurred during request $result will be set to undef, error string can be obtained with $redis->error.

error

$redis->execute("ping" => sub {
    my ($redis, $result) = @_;
    die $redis->error unless defined $result;
});

Returns error occurred during command execution. Note that this method returns error code just from current command and can be used just in callback.

on_error

$redis->on_error(sub{
    my $redis = shift;
    warn 'Redis error ', $redis->error, "\n";
});

Executes if error occurred. Called before commands callbacks.

start

$redis->start;

Starts IOLoop. Shortcut for $redis->ioloop->start;

SEE ALSO

Protocol::Redis, Mojolicious, Mojo::IOLoop

SUPPORT

IRC

#ru.pm on irc.perl.org

DEVELOPMENT

Repository

https://github.com/und3f/mojox-redis

AUTHOR

Sergey Zasenko, [email protected].

COPYRIGHT AND LICENSE

Copyright (C) 2010-2011, Sergey Zasenko

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

mojo-redis's People

Contributors

marcusramberg avatar niczero avatar und3f avatar zpmorgan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.