"Fossies" - the Fresh Open Source Software Archive

Member "Apache-Session-1.93/lib/Apache/Session/Store/File.pm" (21 Sep 2010, 4790 Bytes) of package /linux/www/Apache-Session-1.93.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "File.pm" see the Fossies "Dox" file reference documentation.

    1 #############################################################################
    2 #
    3 # Apache::Session::Store::File
    4 # Implements session object storage via flat files
    5 # Copyright(c) 1998, 1999, 2000, 2004 Jeffrey William Baker (jwbaker@acm.org)
    6 # Distribute under the Perl License
    7 #
    8 ############################################################################
    9 
   10 package Apache::Session::Store::File;
   11 
   12 use strict;
   13 use Symbol;
   14 use IO::File;
   15 use Fcntl;
   16 use vars qw($VERSION);
   17 
   18 $VERSION = '1.04';
   19 
   20 $Apache::Session::Store::File::Directory = '/tmp';
   21 
   22 sub new {
   23     my $class = shift;
   24     my $self;
   25     
   26     $self->{fh}     = Symbol::gensym();
   27     $self->{opened} = 0;
   28     
   29     return bless $self, $class;
   30 }
   31 
   32 sub insert {
   33     my $self    = shift;
   34     my $session = shift;
   35  
   36     my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
   37 
   38     if (-e $directory.'/'.$session->{data}->{_session_id}) {
   39         die "Object already exists in the data store";
   40     }
   41     
   42     my $file = $directory.'/'.$session->{data}->{_session_id};
   43     sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
   44         die "Could not open file $file: $!";
   45 
   46     $self->{opened} = 1;
   47     
   48     print {$self->{fh}} $session->{serialized};
   49     $self->{fh}->flush();
   50 }
   51 
   52 sub update {
   53     my $self    = shift;
   54     my $session = shift;
   55     
   56     my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
   57 
   58     if (!$self->{opened}) {
   59         my $file = $directory.'/'.$session->{data}->{_session_id};
   60         sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
   61             die "Could not open file $file: $!";
   62 
   63         $self->{opened} = 1;
   64     }
   65     
   66     truncate($self->{fh}, 0) || die "Could not truncate file: $!";
   67     seek($self->{fh}, 0, 0);
   68     print {$self->{fh}} $session->{serialized};
   69     $self->{fh}->flush();
   70 }
   71 
   72 sub materialize {
   73     my $self    = shift;
   74     my $session = shift;
   75     
   76     my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
   77     
   78     my $file = $directory.'/'.$session->{data}->{_session_id};
   79     if (-e $file) {
   80         if (!$self->{opened}) {
   81             sysopen ($self->{fh}, $file, O_RDWR|O_CREAT) ||
   82                 die "Could not open file $file: $!";
   83 
   84             $self->{opened} = 1;
   85         }
   86         else {
   87             seek($self->{fh}, 0, 0) || die "Could not seek file: $!";
   88         }
   89 
   90         $session->{serialized} = '';
   91         
   92         my $fh = $self->{fh};
   93         while (my $line = <$fh>) {
   94             $session->{serialized} .= $line;
   95         }
   96     }
   97     else {
   98         die "Object does not exist in the data store";
   99     }
  100 }    
  101 
  102 sub remove {
  103     my $self    = shift;
  104     my $session = shift;
  105         
  106     my $directory = $session->{args}->{Directory} || $Apache::Session::Store::File::Directory;
  107 
  108     if ($self->{opened}) {
  109         CORE::close $self->{fh};
  110         $self->{opened} = 0;
  111     }
  112 
  113     my $file = $directory.'/'.$session->{data}->{_session_id};
  114     if (-e $file) {
  115         unlink ($file) ||
  116             die "Could not remove file $file: $!";
  117     }
  118     else {
  119         die "Object does not exist in the data store";
  120     }
  121 }
  122 
  123 sub close {
  124     my $self = shift;
  125     
  126     if ($self->{opened}) {
  127         CORE::close $self->{fh};
  128         $self->{opened} = 0;
  129     }
  130 }
  131 
  132 sub DESTROY {
  133     my $self = shift;
  134     
  135     if ($self->{opened}) {    
  136         CORE::close $self->{fh};
  137     }
  138 }
  139 
  140 1;
  141 
  142 =pod
  143 
  144 =head1 NAME
  145 
  146 Apache::Session::Store::File - Store persistent data on the filesystem
  147 
  148 =head1 SYNOPSIS
  149 
  150 
  151  use Apache::Session::Store::File;
  152 
  153  my $store = new Apache::Session::Store::File;
  154 
  155  $store->insert($ref);
  156  $store->update($ref);
  157  $store->materialize($ref);
  158  $store->remove($ref);
  159 
  160 =head1 DESCRIPTION
  161 
  162 This module fulfills the storage interface of Apache::Session.  The serialized
  163 objects are stored in files on your filesystem.
  164 
  165 =head1 OPTIONS
  166 
  167 This module requires one argument in the usual Apache::Session style.  The
  168 name of the option is Directory, and the value is the full path of the 
  169 directory where you wish to place the files.  Example
  170 
  171  tie %s, 'Apache::Session::File', undef,
  172     {Directory => '/tmp/sessions'};
  173 
  174 =head1 NOTES
  175 
  176 All session objects are stored in the same directory.  Some filesystems, such
  177 as Linux's ext2fs, have O(n) performance where n is the number of files in a
  178 directory.  Other filesystems, like Sun's UFS, and Linux's reiserfs, do not
  179 have this problem.  You should consider your filesystem's performance before
  180 using this module to store many objects.
  181 
  182 =head1 AUTHOR
  183 
  184 This module was written by Jeffrey William Baker <jwbaker@acm.org>.
  185 
  186 =head1 SEE ALSO
  187 
  188 L<Apache::Session>