PApp::Session - manage session-specific data.


PApp documentation Contained in the PApp distribution.

Index


Code Index:

NAME

Top

PApp::Session - manage session-specific data.

SYNOPSIS

Top

 use PApp::Session;
 # see also PApp::Prefs and PApp::Env

DESCRIPTION

Top

This module manages session-specific variables, that is, values that get associated with all accesses within a single session. Session variables keep their value when old states get re-requested, as opposed to state variables that change back to their old value, and can be used for transactions or other data that belongs to a whole session and not a single access.

Functions

locksession { BLOCK }

Execute the given block while the session table is locked against changes from other processes. Needless to say, the block should execute as fast as possible. Returns the return value of BLOCK (which is called in scalar context).

Methods

PApp::Session::get ($key)

Return the named session variable (or undef, when the variable does not exist).

PApp::Session::set ($key, $value)

Set the named session variable. If $value is undef, then the variable will be deleted. You can pass in (serializable) references.

PApp::Session::ref ($key)

Return a reference to the session value (i.e. a PApp::DataRef object). Updates to the referend will be seen by all processes.

SEE ALSO

Top

PApp::Prefs, PApp::Env, PApp, PApp::User.

AUTHOR

Top

 Marc Lehmann <schmorp@schmorp.de>
 http://home.schmorp.de/


PApp documentation Contained in the PApp distribution.
##########################################################################
## All portions of this code are copyright (c) 2003,2004 nethype GmbH   ##
##########################################################################
## Using, reading, modifying or copying this code requires a LICENSE    ##
## from nethype GmbH, Franz-Werfel-Str. 11, 74078 Heilbronn,            ##
## Germany. If you happen to have questions, feel free to contact us at ##
## license@nethype.de.                                                  ##
##########################################################################

package PApp::Session;

use Compress::LZF qw(:freeze);

use PApp::SQL;
use PApp::Exception qw(fancydie);
use PApp::Callback ();
use PApp::Config qw(DBH $DBH); DBH;

use base Exporter;

$VERSION = 1.45;
@EXPORT = qw( 
   locksession
);

use Convert::Scalar ();

sub locksession(&) {
   sql_fetch $DBH, "select get_lock('PAPP_SESSION_LOCK_SESSION', 60)"
      or fancydie "PApp::Session::locksession: unable to aquire database lock";
   my $res = eval { $_[0]->() };
   {
      local $@;
      sql_exec $DBH, "select release_lock('PAPP_SESSION_LOCK_SESSION')";
   }
   die if $@;
   $res;
}

sub get ($) {
   sthaw sql_ufetch $DBH, "select value from session where sid = ? and name = ?",
                    $PApp::sessionid, Convert::Scalar::utf8_upgrade "$_[0]";
}

sub set ($;$) {
   if (defined $_[1]) {
      sql_exec $DBH, "replace into session (sid, name, value) values (?, ?, ?)",
               $PApp::sessionid, Convert::Scalar::utf8_upgrade "$_[0]", 
               sfreeze_cr $_[1];
   } else {
      sql_exec $DBH, "delete from session where sid = ? and name = ?",
               $PApp::sessionid, Convert::Scalar::utf8_upgrade "$_[0]";
   }
}

sub ref($) {
   require PApp::DataRef;

   \(new PApp::DataRef 'DB_row',
         database => $PApp::Config::Database,
         table    => "session", 
         key      => [qw(sid name)],
         id       => [$PApp::sessionid, $_[0]],
         utf8     => 1,
   )->{
      ["value", PApp::DataRef::DB_row::filter_sfreeze_cr]
   };
}