Apache-Session

 view release on metacpan or  search on metacpan

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

#############################################################################
#
# Apache::Session::Store::File
# Implements session object storage via flat files
# Copyright(c) 1998, 1999, 2000, 2004 Jeffrey William Baker (jwbaker@acm.org)
# Distribute under the Perl License
#
############################################################################

package Apache::Session::Store::File;

use strict;
use Symbol;
use IO::File;
use Fcntl;
use vars qw($VERSION);

$VERSION = '1.04';

$Apache::Session::Store::File::Directory = '/tmp';

sub new {
    my $class = shift;
    my $self;
    
    $self->{fh}     = Symbol::gensym();
    $self->{opened} = 0;
    
    return bless $self, $class;
}

sub insert {
    my $self    = shift;
    my $session = shift;
 
    my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;

    if (-e $directory.'/'.$session->{data}->{_session_id}) {
        die "Object already exists in the data store";
    }
    
    my $file = $directory.'/'.$session->{data}->{_session_id};
    sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
        die "Could not open file $file: $!";

    $self->{opened} = 1;
    
    print {$self->{fh}} $session->{serialized};
    $self->{fh}->flush();
}

sub update {
    my $self    = shift;
    my $session = shift;
    
    my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;

    if (!$self->{opened}) {
        my $file = $directory.'/'.$session->{data}->{_session_id};
        sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
            die "Could not open file $file: $!";

        $self->{opened} = 1;
    }
    
    truncate($self->{fh}, 0) || die "Could not truncate file: $!";
    seek($self->{fh}, 0, 0);
    print {$self->{fh}} $session->{serialized};
    $self->{fh}->flush();
}

sub materialize {
    my $self    = shift;
    my $session = shift;
    
    my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
    
    my $file = $directory.'/'.$session->{data}->{_session_id};
    if (-e $file) {
        if (!$self->{opened}) {
            sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
                die "Could not open file $file: $!";

            $self->{opened} = 1;
        }
        else {
            seek($self->{fh}, 0, 0) || die "Could not seek file: $!";
        }

        $session->{serialized} = '';
        
        my $fh = $self->{fh};
        while (my $line = <$fh>) {
            $session->{serialized} .= $line;
        }
    }
    else {
        die "Object does not exist in the data store";
    }
}    

sub remove {
    my $self    = shift;
    my $session = shift;
        
    my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;

    if ($self->{opened}) {
        CORE::close $self->{fh};
        $self->{opened} = 0;
    }



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