| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # Apache::Session::Store::DB_File | ||
| 4 | # Implements session object storage via Perl's DB_File module | ||
| 5 | # Copyright(c) 2000 Jeffrey William Baker (jwbaker@acm.org) | ||
| 6 | # Distribute under the Perl License | ||
| 7 | # | ||
| 8 | ############################################################################ | ||
| 9 | |||
| 10 | package Apache::Session::Store::DB_File; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use vars qw($VERSION); | ||
| 14 | use DB_File; | ||
| 15 | |||
| 16 | $VERSION = '1.01'; | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my $class = shift; | ||
| 20 | |||
| 21 | return bless {dbm => {}}, $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub insert { | ||
| 25 | my $self = shift; | ||
| 26 | my $session = shift; | ||
| 27 | |||
| 28 | if (!tied %{$self->{dbm}}) { | ||
| 29 | my $rv = tie %{$self->{dbm}}, 'DB_File', $session->{args}->{FileName}; | ||
| 30 | |||
| 31 | if (!$rv) { | ||
| 32 | die "Could not open dbm file $session->{args}->{FileName}: $!"; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | if (exists $self->{dbm}->{$session->{data}->{_session_id}}) { | ||
| 37 | die "Object already exists in the data store"; | ||
| 38 | } | ||
| 39 | |||
| 40 | $self->{dbm}->{$session->{data}->{_session_id}} = $session->{serialized}; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub update { | ||
| 44 | my $self = shift; | ||
| 45 | my $session = shift; | ||
| 46 | |||
| 47 | if (!tied %{$self->{dbm}}) { | ||
| 48 | my $rv = tie %{$self->{dbm}}, 'DB_File', $session->{args}->{FileName}; | ||
| 49 | |||
| 50 | if (!$rv) { | ||
| 51 | die "Could not open dbm file $session->{args}->{FileName}: $!"; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | $self->{dbm}->{$session->{data}->{_session_id}} = $session->{serialized}; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub materialize { | ||
| 59 | my $self = shift; | ||
| 60 | my $session = shift; | ||
| 61 | |||
| 62 | if (!tied %{$self->{dbm}}) { | ||
| 63 | my $rv = tie %{$self->{dbm}}, 'DB_File', $session->{args}->{FileName}; | ||
| 64 | |||
| 65 | if (!$rv) { | ||
| 66 | die "Could not open dbm file $session->{args}->{FileName}: $!"; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | $session->{serialized} = $self->{dbm}->{$session->{data}->{_session_id}}; | ||
| 71 | |||
| 72 | if (!defined $session->{serialized}) { | ||
| 73 | die "Object does not exist in data store"; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | sub remove { | ||
| 78 | my $self = shift; | ||
| 79 | my $session = shift; | ||
| 80 | |||
| 81 | if (!tied %{$self->{dbm}}) { | ||
| 82 | my $rv = tie %{$self->{dbm}}, 'DB_File', $session->{args}->{FileName}; | ||
| 83 | |||
| 84 | if (!$rv) { | ||
| 85 | die "Could not open dbm file $session->{args}->{FileName}: $!"; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | delete $self->{dbm}->{$session->{data}->{_session_id}}; | ||
| 90 | } | ||
| 91 | |||
| 92 | 1; | ||
| 93 | |||
| 94 | =pod | ||
| 95 | |||
| 96 | =head1 NAME | ||
| 97 | |||
| 98 | Apache::Session::Store::DB_File - Use DB_File to store persistent objects | ||
| 99 | |||
| 100 | =head1 SYNOPSIS | ||
| 101 | |||
| 102 | use Apache::Session::Store::DB_File; | ||
| 103 | |||
| 104 | my $store = new Apache::Session::Store::DB_File; | ||
| 105 | |||
| 106 | $store->insert($ref); | ||
| 107 | $store->update($ref); | ||
| 108 | $store->materialize($ref); | ||
| 109 | $store->remove($ref); | ||
| 110 | |||
| 111 | =head1 DESCRIPTION | ||
| 112 | |||
| 113 | This module fulfills the storage interface of Apache::Session. The serialized | ||
| 114 | objects are stored in a Berkeley DB file using the DB_File Perl module. If | ||
| 115 | DB_File works on your platform, this module should also work. | ||
| 116 | |||
| 117 | =head1 OPTIONS | ||
| 118 | |||
| 119 | This module requires one argument in the usual Apache::Session style. The | ||
| 120 | name of the option is FileName, and the value is the full path of the database | ||
| 121 | file to be used as the backing store. If the database file does not exist, | ||
| 122 | it will be created. Example: | ||
| 123 | |||
| 124 | tie %s, 'Apache::Session::DB_File', undef, | ||
| 125 | {FileName => '/tmp/sessions'}; | ||
| 126 | |||
| 127 | =head1 AUTHOR | ||
| 128 | |||
| 129 | This module was written by Jeffrey William Baker <jwbaker@acm.org>. | ||
| 130 | |||
| 131 | =head1 SEE ALSO | ||
| 132 | |||
| 133 | L<Apache::Session>, L<DB_File> |