package PageSession; use IPC::Shareable; use vars qw($NEXTID $MAXSESSIONS %SESSIONS); $MAX_SESSIONS = 100; tie $NEXTID, IPC::Shareable,'S000',{create=>1,mode=>0600}; tie %SESSIONS,IPC::Shareable,'S001',{create=>1,mode=>0600}; $NEXTID = 0 if $NEXTID eq ''; # Find a new ID to use by the simple expedient of cycling # through a numeric list. In a real application, the ID # should be unique, and maintained in a most-frequently-used cache. sub new { my($package) = @_; tied($NEXTID)->shlock; $NEXTID=0 if $NEXTID > $MAX_SESSIONS; my $self = bless { name => '', article => '', page => 0, id => $NEXTID++ },$package; tied($NEXTID)->shunlock; return $self; } sub fetch { my ($package,$id) = @_; return undef if $id eq ''; # Storeable will automagically make this a PageSession object return $SESSIONS{$id}; } sub save { my $self = shift; # store the object tied(%SESSIONS)->shlock; $SESSIONS{$self->{id}} = $self; tied(%SESSIONS)->shunlock; } sub id { $_[0]->{id}; } sub name { $_[0]->{name} = $_[1] if defined($_[1]); $_[0]->{name}; } sub article { $_[0]->{article} = $_[1] if defined($_[1]); $_[0]->{article}; } sub page { $_[0]->{page} = $_[1] if defined($_[1]); $_[0]->{page} = 0 if $_[0]->{page} < 0; $_[0]->{page}; } 1;