#
# constructor
#
sub new {
my $class = shift;
my (%args) = @_;
my $sessions = {};
my $session = {};
# settings / defaults
my $session_id = $args{SESSION_ID} || "";
my $full_db_path = $args{FILE} || die "session db filename not defined";
my $timeout = ( $args{TIMEOUT} || $DEFAULT_TIMEOUT ) * 60; # and convert to seconds
$sync_dbm_obj = tie(%{$sessions}, 'MLDBM::Sync', $full_db_path, O_CREAT|O_RDWR, 0640)
or die "could not create / open db file $full_db_path: $!";
for ( $session_id ) {
if( $session_id ) {
$sync_dbm_obj->Lock;
my $aref = $sessions->{$session_id};
if( $aref ) {
my $timestamp = $aref->{timestamp};
if( time() - $timestamp > $timeout ) {
# session expired and has to be removed
delete( $sessions->{$session_id} );
$sync_dbm_obj->UnLock;
$session_id = "";
redo;
}
else {
# update last access timestamp
$aref->{timestamp} = time();
$aref->{a_session}->{_timestamp} = time(); #debug only
$sessions->{$session_id} = $aref;
$sync_dbm_obj->UnLock;
$session = $aref->{a_session};
}
}
else {
$sync_dbm_obj->UnLock;
$session_id = "";
redo;
}
}
else {
$session_id = create_session_id();
$sync_dbm_obj->Lock;
$sessions->{$session_id} = {
a_session => { _session_id => $session_id,
_timestamp => time() }, # debug only
timestamp => time()
};
$session = $sessions->{$session_id}->{a_session};
$sync_dbm_obj->UnLock;
}
}
return bless $session, $class;
}
#
#
#
sub add_key {
my $self = shift;
my ($key, $value) = @_;
$sync_dbm_obj->Lock;
my $aref = $self;
$aref->{$key} = $value;
$self = $aref;
$sync_dbm_obj->UnLock;
}
#
# general destructor
#
sub DESTROY {
my $self = shift;
undef $sync_dbm_obj;
untie(%{$self});
};
####
my $session = MLDBMSession->new(FILE => "c:\\perl_user\sessions.db", SESSION_ID => $session_id, TIMEOUT => 100);
$session->add_key( value1 => 20 );
####
$VAR1 = bless( {
'_timestamp' => 1034001952,
'value1' => 20,
'_session_id' => '1ca9437eec103d4d533dc22b5b2c45ab'
}, 'MLDBMSession' );
Time now: Mon Oct 7 10:45:52 2002
$VAR1 = {
'a_session' => {
'_timestamp' => 1034001952,
'_session_id' => '1ca9437eec103d4d533dc22b5b2c45ab'
},
'timestamp' => 1034001952
};
Created/Last Accessed: Mon Oct 7 10:45:52 2002