Plack-Middleware-Session

 view release on metacpan or  search on metacpan

lib/Plack/Session/Store/File.pm  view on Meta::CPAN

package Plack::Session::Store::File;
use strict;
use warnings;

our $VERSION   = '0.36';
our $AUTHORITY = 'cpan:STEVAN';

use Storable ();
use File::Spec ();

use parent 'Plack::Session::Store';

use Plack::Util::Accessor qw[
    dir
    serializer
    deserializer
];

sub new {
    my ($class, %params) = @_;

    $params{'dir'} ||= $ENV{TMPDIR} || File::Spec->tmpdir;

    die "Storage directory (" . $params{'dir'} . ") is not writeable"
        unless -w $params{'dir'};

    $params{'serializer'}   ||= sub { Storable::lock_nstore( @_ ) };
    $params{'deserializer'} ||= sub { Storable::lock_retrieve( @_ ) };

    bless { %params } => $class;
}

sub fetch {
    my ($self, $session_id) = @_;

    my $file_path = $self->_get_session_file_path( $session_id );
    return unless -f $file_path;

    $self->deserializer->( $file_path );
}

sub store {
    my ($self, $session_id, $session) = @_;
    my $file_path = $self->_get_session_file_path( $session_id );
    $self->serializer->( $session, $file_path );
}

sub remove {
    my ($self, $session_id) = @_;
    unlink $self->_get_session_file_path( $session_id );
}

sub _get_session_file_path {
    my ($self, $session_id) = @_;
    $self->dir . '/' . $session_id;
}

1;

__END__

=pod

=head1 NAME

Plack::Session::Store::File - Basic file-based session store

=head1 SYNOPSIS

  use Plack::Builder;
  use Plack::Middleware::Session;
  use Plack::Session::Store::File;

  my $app = sub {
      return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
  };

  builder {
      enable 'Session',
          store => Plack::Session::Store::File->new(
              dir => '/path/to/sessions'
          );
      $app;
  };

  # with custom serializer/deserializer

  builder {
      enable 'Session',
          store => Plack::Session::Store::File->new(



( run in 0.749 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )