Catalyst-Plugin-Session-Store-Redis-Fast

 view release on metacpan or  search on metacpan

lib/Catalyst/Plugin/Session/Store/Redis/Fast.pm  view on Meta::CPAN

package Catalyst::Plugin::Session::Store::Redis::Fast;

our $VERSION = '1.001'; # VERSION

use warnings;
use strict;

use base qw/
    Class::Data::Inheritable
    Catalyst::Plugin::Session::Store
/;
use MRO::Compat;
use MIME::Base64 qw(encode_base64 decode_base64);
use Storable qw/nfreeze thaw/;
use Try::Tiny;
use Redis::Fast;

__PACKAGE__->mk_classdata(qw/_redis_connection/);

sub get_session_data {
    my ($c, $key) = @_;

    $c->_verify_redis_connection;

    if(my ($sid) = $key =~ /^expires:(.*)/) {
        $c->log->debug("Getting expires key for $sid");
        return $c->_redis_connection->get($key);
    } else {
        $c->log->debug("Getting $key");
        my $data = $c->_redis_connection->get($key);
        if(defined($data)) {
            return thaw( decode_base64($data) )
        }
    }

    return;
}

sub store_session_data {
    my ($c, $key, $data) = @_;

    $c->_verify_redis_connection;

    my $time = int($c->session_expires - time);
    if($time == 0) {
        $c->log->warn("skipping $key already expired at " . $c->session_expires );
    } else {
        if(my ($sid) = $key =~ /^expires:(.*)/) {
            $c->log->debug("Setting expires key for '$sid: $data' expiry seconds ($time)");
            $c->_redis_connection->setex($key, $time, $data);
        } else {
            $c->log->debug("Setting key '$key' with expiry seconds ($time)");
            $c->_redis_connection->setex($key, $time, encode_base64(nfreeze($data)));
        }
    }
    return;
}

sub delete_session_data {
    my ($c, $key) = @_;

    $c->_verify_redis_connection;

    $c->log->debug("Deleting: $key");
    $c->_redis_connection->del($key);

    return;
}

sub delete_expired_sessions {
    # my ($c) = @_;
    #redis will delete
}

sub setup_session {



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