package WWW::Session::Storage::File;

use 5.006;
use strict;
use warnings;

=head1 NAME

WWW::Session::Storage::File - File storage engine for WWW::Session


File backend for WWWW::Session

=head1 VERSION

Version 0.12


our $VERSION = '0.12';


This module is used for storring serialized WWW::Session objects

Usage : 

    use WWW::Session::Storage::File;

    my $storage = WWW::Session::Storage::File->new({path => '/tmp/sessions'});
    my $serialized_data = $storage->retrive($session_id);


=head2 new

Creates a new WWW::Session::Storage::File object

This method accepts only one argument, a hashref that must contain a key named
"path" which defines the path where the sessions will be saved


sub new {
    my ($class,$params) = @_;
    die "You must specify the path where to save the sessions!" unless defined $params->{path};
    die "Cannot save sessions in folder '".$params->{path}."' because the folder does not exist!" unless -d $params->{path};
    my $self = {
                path => $params->{path}
    bless $self, $class;
    return $self;

=head2 save

Stores the given information into the file

sub save {
    my ($self,$sid,$expires,$string) = @_;
    open(my $fh,">",$self->{path}."/".$sid);
    print $fh $expires . "\n";
    print $fh $string;

=head2 retrieve

Retrieves the informations for a session, verifies that it's not expired and returns
the string containing the serialized data

sub retrieve {
    my ($self,$sid) = @_;
    my $filename = $self->{path}."/".$sid;
    return undef unless -f $filename;
    my @file_info = stat($filename);
    open(my $fh,"<",$self->{path}."/".$sid) || return undef;
    local $/ = "\n";
    my $expires= readline($fh);
    local $/ = undef;
    my $string = readline($fh);
    #check if it didn't expire
    if ($expires != -1 && ($file_info[8] + $expires) < time() ) {
        unlink $filename;
        return undef;
    return $string;

=head2 delete

Completely removes the session data for the given session id

sub delete {
    my ($self,$sid) = @_;
    my $filename = $self->{path}."/".$sid;
    if (-f $filename) {

=head1 AUTHOR

Gligan Calin Horea, C<< <gliganh at> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-www-session at>, or through
the web interface at L<>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc WWW::Session::Storage::File

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)


=item * AnnoCPAN: Annotated CPAN documentation


=item * CPAN Ratings


=item * Search CPAN





Copyright 2012 Gligan Calin Horea.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See for more information.


1; # End of WWW::Session::Storage::File