Here's how I'm calling the method:# # 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 def +ined"; my $timeout = ( $args{TIMEOUT} || $DEFAULT_TIMEOUT ) * 60; # and c +onvert 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}); };
Here's what I get when I do print Dumper $session and then print Dumper everything in sessions.db file:my $session = MLDBMSession->new(FILE => "c:\\perl_user\sessions.db", S +ESSION_ID => $session_id, TIMEOUT => 100); $session->add_key( value1 => 20 );
Thank you, Alex$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' => '1ca9437eec103d4d533dc22b5 +b2c45ab' }, 'timestamp' => 1034001952 }; Created/Last Accessed: Mon Oct 7 10:45:52 2002
In reply to Problem storing value in persistent hash with MLDBM by relax99
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |