How to use Dancer::Plugin::Redis

Dancer::Plugin::Redis is a plugin created to make an easy way to use Redis database in Dancer apps. In fact, Dancer::Plugin::Redis is an abstraction from Redis plugin. It has only one function redis, which returns a database handle. See Redis module documentation for the list of controlling functions.

Introduction to Redis

Redis is key-value, networking, in-memory data store with optional durability. It is very useful technology because it simple and fast. It supports many data types (integers, strings, sets, etc).

Synopsis

use Dancer;
use Dancer::Plugin::Email;
use Dancer::Plugin::Redis;
        
post '/write_us' => sub{
        email{
        to => redis->get('support_email'), #gets destination email from database
        subject => $subject,
        encoding => redis->get('email_encoding'), #get email encoding from database
        message => $msg
        };
        redis->incr ('count_of_messages'); #increments count of all emails sent by users
        };
                
};

dance;

When you use redis keyword it gives you a connected database handle or (if already connected) checks connection.

Simple examples

Getting a value

use Dancer;
use Dancer::Plugin::Redis;

get '/widget/view/:id' => sub {
template 'display_widget', { widget => redis->get('time_applet'); };
};

dance;

Incrementing count of views in Redis database

use Dancer;
use Dancer::Plugin::Redis;

get '/index' => sub{
redis->incr('count_of_views');
};
dance;

Author

This article was written by Andrew Inishev, Google Code-in student, mentored by David Precious.