Cache-RedisDB

 view release on metacpan or  search on metacpan

lib/Cache/RedisDB.pm  view on Meta::CPAN

package Cache::RedisDB;

use 5.010;
use strict;
use warnings;
use Carp;
use RedisDB 2.14;
use Sereal qw(looks_like_sereal);

=head1 NAME

Cache::RedisDB - RedisDB based cache system

=head1 DESCRIPTION

This is just a wrapper around RedisDB to have a single Redis object and connection per process. By default uses server redis://127.0.0.1, but it may be overwritten by REDIS_CACHE_SERVER environment variable. It transparently handles forks.

=head1 COMPATIBILITY AND REQUIREMENTS

Redis 2.6.12 and higher strongly recommended.  Required if you want to use
extended options in ->set().

=cut

our $VERSION = '0.13';

=head1 SYNOPSIS

    use Cache::RedisDB;
    Cache::RedisDB->set("namespace", "key", "value");
    Cache::RedisDB->get("namespace", "key");

=head1 METHODS

=head2 redis_uri

Returns a C<< redis:// >> redis URI which will be used for the initial Redis connection.

This will default to localhost on the standard port, and can be overridden with the
C<REDIS_CACHE_SERVER> environment variable.

=cut

sub redis_uri {

    my $redis_uri = $ENV{REDIS_CACHE_SERVER} // 'redis://127.0.0.1';

    # Probably a legacy TCP host:port
    $redis_uri = 'redis://' . $redis_uri if ($redis_uri =~ m#^[^/]+:[0-9]{1,5}$#);

    return $redis_uri;
}

=head2 redis_connection

Creates new connection to a Redis server and returns the corresponding L<RedisDB> object.

=cut

sub redis_connection {
    return RedisDB->new(
        url                => redis_uri(),
        reconnect_attempts => 3,
        on_connect_error   => sub {
            confess "Cannot connect: " . redis_uri();
        });
}

=head2 redis

Returns a singleton L<RedisDB> instance.

=cut

sub redis {
    state $redis;



( run in 1.604 second using v1.01-cache-2.11-cpan-39bf76dae61 )