NAME

Dancer2::Plugin::Redis - Perl Dancer2 plugin for interaction with key-value-store Redis.

VERSION

version 0.009

SYNOPSIS

In your config.yml:

plugins:
  Redis:
    # if you use TCP/IP:
    server: "localhost:6379"
    # if you use UNIX/Linux sockets:
    sock: "/path/to/sock"
    # (optional) Redis password used with auth:
    password: "Very secure password 123!"
    # (optional) Reconnect up to 60 seconds (reconnect) every 5000 milliseconds (every):
    reconnect: 60
    every: 5000
    # (optional) Redis connection name (NOT the Redis database ID):
    name: "my_connection_name"
    # (optional) Function called on Redis connect:
    on_connect: "MyDancer2App::redis_on_connect"
    # (optional) Use serialization for storing values other than simple scalars with Redis:
    serialization:
      # Use Sereal as serialization module:
      module: "Dancer2::Plugin::Redis::Serialization::Sereal"
      # Serialization module configuration:
      # Use snappy compression
      compression: "snappy"

In your source files:

package MyDancer2App;
use Dancer2;
use Dancer2::Plugin::Redis;

# Outputs the counter value stored in Redis, increments and saves it back to Redis.
get '/' => sub {
  my $counter = redis_get('counter');  # Get the counter value from Redis.
  redis_set( ++$counter );             # Increment counter value by 1 and save it back to Redis.
  return $counter;
};

# (optional) Function called on Redis connect.
sub redis_on_connect {
  my ( $redis ) = @_;
  
  # do some stuff with the bare Redis interface. This is NOT Dancer2::Plugin::Redis!
  
  return;
}

DESCRIPTION

This Perl Dancer2 plugin adds various Domain-specific language (dsl) symbols to interact with Redis server configured in config.yml.

It uses the Redis module to communicate internally with the Redis server. It also provides serialization features to store values which are more than just simple scalars (strings). By default there is no serialization used.

I wrote this with my colleague to cover the use cases we're having with our own Dancer2 application. If you need additional functionality that is not included, please don't hesitate and create a pull request on GitHub or just file your feature request with the GitHub issue tracker.

SEREAL

In order to use the supplied Sereal broker you have to install Sereal::Decoder and Sereal::Encoder. Both modules listed as runtime recommends with Dancer2::Plugin::Redis.

METHODS

redis_plugin

Returns a Dancer2::Plugin::Redis instance. You can use redis_plugin to pass the plugin instance to 3rd party modules (backend api) so you can access the existing Redis connection there. You will need to access the actual methods of the the plugin instance.

my $business_logic = Business::Logic->new( redis_plugin => redis_plugin() );

# somewhere else ...
package Business::Logic;

sub frobnicate {
  return $self->redis_plugin->_get( 'key' );
}

If you need access to the underlying Redis object you can access this by calling the plugin's _redis method:

my $redis = redis_plugin->_redis;

redis_get

Returns the actual value stored in Redis of a single key.

redis_mget

Returns the values stored in Redis for one or more keys.

redis_set

Assign a new value to a singe key in Redis.

redis_mset

Assign one or more new values to keys in Redis.

redis_expire

Assign a new expiration timeout to a singe key in Redis. undef or a false value will turn off expiration.

redis_expire 'key', 10; # will expire in ten seconds

redis_expire 'key', undef; # removes expire from key
redis_expire 'key';        # so will this

redis_del

Deletes a key within Redis.

SEE ALSO

Dancer2
Redis
Sereal

CONTRIBUTORS

The following people have contributed to Dancer2::Plugin::Redis. Thanks!

SysPete

AUTHOR

BURNERSK <burnersk@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2018 by BURNERSK <burnersk@cpan.org>.

This is free software, licensed under:

The MIT (X11) License