[Jifty-commit] jifty branch, master, updated. b7bf5998fc5fd3d3230b959c9f5c03700765e5f9

Jifty commits jifty-commit at lists.jifty.org
Fri Feb 5 11:36:22 EST 2010


The branch, master has been updated
       via  b7bf5998fc5fd3d3230b959c9f5c03700765e5f9 (commit)
      from  bbd5a22caa118517ceb557976b0cb57d6fb22cb7 (commit)

Summary of changes:
 lib/Jifty/Web/Session.pm                      |  129 ++----------------------
 lib/Jifty/Web/{Session.pm => Session/JDBI.pm} |  135 +------------------------
 2 files changed, 11 insertions(+), 253 deletions(-)
 copy lib/Jifty/Web/{Session.pm => Session/JDBI.pm} (66%)

- Log -----------------------------------------------------------------
commit b7bf5998fc5fd3d3230b959c9f5c03700765e5f9
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Fri Feb 5 08:18:52 2010 -0800

    Extract our JDBI-based session handler from the core session handler class

diff --git a/lib/Jifty/Web/Session.pm b/lib/Jifty/Web/Session.pm
index 320d71c..2d20257 100644
--- a/lib/Jifty/Web/Session.pm
+++ b/lib/Jifty/Web/Session.pm
@@ -5,10 +5,6 @@ package Jifty::Web::Session;
 use base qw/Jifty::Object/;
 use CGI::Cookie ();
 use DateTime    ();
-use Storable    ();
-$Storable::Deparse    = 1;
-$Storable::Eval       = 1;
-$Storable::forgive_me = 1;
 
 =head1 NAME
 
@@ -32,14 +28,10 @@ Returns a new, empty session.
 sub new {
     my $class = shift;
 
-    my $session_class = Jifty->config->framework('Web')->{'SessionClass'};
+    my $session_class = Jifty->config->framework('Web')->{'SessionClass'} || 'Jift::Web::Session::JDBI';
     my $cookie_name = Jifty->config->framework('Web')->{'SessionCookieName'};
-    if ( $session_class and $class ne $session_class ) {
         Jifty::Util->require($session_class);
         return $session_class->new(@_);
-    } else {
-        return bless { _cookie_name => $cookie_name }, $class;
-    }
 }
 
 =head2 id
@@ -49,8 +41,7 @@ Returns the session's id if it has been loaded, or C<undef> otherwise.
 =cut
 
 sub id {
-    my $self = shift;
-    return $self->loaded ? $self->_session->session_id : undef;
+    die "Subclass must implement 'id'";
 }
 
 =head2 load [ID]
@@ -62,19 +53,7 @@ creates a session in the database.
 =cut
 
 sub load {
-    my $self       = shift;
-    my $session_id = shift;
-
-    $session_id ||= $self->_get_session_id_from_client();
-
-    my $session = Jifty::Model::Session->new;
-    $session->load_by_cols(
-        session_id => $session_id,
-        key_type   => "session"
-    ) if $session_id;
-
-    $session->create( key_type => "session" ) unless $session->id;
-    $self->_session($session);
+    die "Subclass must implement 'load'";
 }
 
 =head2 load_by_kv key => value 
@@ -87,34 +66,8 @@ based on, say, a timestamp, then you're asking for trouble.
 =cut
 
 sub load_by_kv {
-    my $self = shift;
-    my $k    = shift;
-    my $v    = shift;
-
-# XXX TODO: we store this data in a storable. so we now want to match on the storable version
-# It would be so nice if Jifty::DBI could do this for us.
-    my $encoded = Storable::nfreeze( \$v );
-
-    my $session = Jifty::Model::Session->new;
-    $session->load_by_cols(
-        key_type => 'key',
-        data_key => $k,
-        value    => $encoded,
-    );
-    my $session_id = $session->session_id;
 
-    # XXX: if $session_id is undef, then bad things happen. This *can* happen.
-
-    $self->load($session_id);
-    $self->set( $k => $v ) if !$session_id;
-}
-
-sub _get_session_id_from_client {
-    my $self        = shift;
-    my %cookies     = CGI::Cookie->fetch();
-    my $cookie_name = $self->cookie_name;
-    my $session_id
-        = $cookies{$cookie_name} ? $cookies{$cookie_name}->value() : undef;
+    die "Subclass must implement load_by_kv";
 }
 
 =head2 unload
@@ -156,20 +109,8 @@ session, including "metadata" and "continuation".
 =cut
 
 sub get {
-    my $self     = shift;
-    my $key      = shift;
-    my $key_type = shift || "key";
-
-    return undef unless $self->loaded;
-
-    my $setting = Jifty::Model::Session->new;
-    $setting->load_by_cols(
-        session_id => $self->id,
-        key_type   => $key_type,
-        data_key   => $key
-    );
-    return $setting->value;
 
+    die "subclass must implement 'get'"
 }
 
 =head2 set KEY => VALUE, [TYPE]
@@ -183,31 +124,7 @@ you.
 =cut
 
 sub set {
-    my $self     = shift;
-    my $key      = shift;
-    my $value    = shift;
-    my $key_type = shift || "key";
-
-    return undef unless $self->loaded;
-    $self->_session->set_updated( DateTime->now );
-
-    my $setting = Jifty::Model::Session->new;
-    $setting->load_by_cols(
-        session_id => $self->id,
-        key_type   => $key_type,
-        data_key   => $key
-    );
-    if ( $setting->id ) {
-        $setting->set_value($value);
-    } else {
-        $setting->create(
-            session_id => $self->id,
-            key_type   => $key_type,
-            data_key   => $key,
-            value      => $value
-        );
-    }
-
+    die "subclass must implement 'set'"
 }
 
 =head2 remove KEY, [TYPE]
@@ -217,20 +134,7 @@ Remove key C<KEY> from the cache.  C<TYPE> defaults to "key".
 =cut
 
 sub remove {
-    my $self     = shift;
-    my $key      = shift;
-    my $key_type = shift || "key";
-
-    return undef unless $self->loaded;
-    $self->_session->set_updated( DateTime->now );
-
-    my $setting = Jifty::Model::Session->new;
-    $setting->load_by_cols(
-        session_id => $self->id,
-        key_type   => $key_type,
-        data_key   => $key
-    );
-    $setting->delete if $setting->id;
+    die "subclass must implement 'remove'"
 }
 
 =head2 remove_all
@@ -240,12 +144,7 @@ Removes the session from the database entirely.
 =cut
 
 sub remove_all {
-    my $self = shift;
-    return unless $self->loaded;
-    my $settings = Jifty::Model::SessionCollection->new;
-    $settings->limit( column => "session_id", value => $self->id );
-    $_->delete while $_ = $settings->next;
-    $self->unload;
+    die "Subclass must implement 'remove_all'"
 }
 
 =head2 set_continuation ID CONT
@@ -291,17 +190,7 @@ continuations' C<id>.
 =cut
 
 sub continuations {
-    my $self = shift;
-
-    return () unless $self->loaded;
-
-    my $conts = Jifty::Model::SessionCollection->new;
-    $conts->limit( column => "key_type",   value => "continuation" );
-    $conts->limit( column => "session_id", value => $self->id );
-
-    my %continuations;
-    $continuations{ $_->data_key } = $_->value while $_ = $conts->next;
-    return %continuations;
+    die "Subclass must implement 'continuations'";
 }
 
 =head2 set_cookie
diff --git a/lib/Jifty/Web/Session.pm b/lib/Jifty/Web/Session/JDBI.pm
similarity index 66%
copy from lib/Jifty/Web/Session.pm
copy to lib/Jifty/Web/Session/JDBI.pm
index 320d71c..4043787 100644
--- a/lib/Jifty/Web/Session.pm
+++ b/lib/Jifty/Web/Session/JDBI.pm
@@ -1,8 +1,8 @@
 use warnings;
 use strict;
 
-package Jifty::Web::Session;
-use base qw/Jifty::Object/;
+package Jifty::Web::Session::JDBI;
+use base qw/Jifty::Web::Session/;
 use CGI::Cookie ();
 use DateTime    ();
 use Storable    ();
@@ -31,15 +31,7 @@ Returns a new, empty session.
 
 sub new {
     my $class = shift;
-
-    my $session_class = Jifty->config->framework('Web')->{'SessionClass'};
-    my $cookie_name = Jifty->config->framework('Web')->{'SessionCookieName'};
-    if ( $session_class and $class ne $session_class ) {
-        Jifty::Util->require($session_class);
-        return $session_class->new(@_);
-    } else {
         return bless { _cookie_name => $cookie_name }, $class;
-    }
 }
 
 =head2 id
@@ -117,36 +109,6 @@ sub _get_session_id_from_client {
         = $cookies{$cookie_name} ? $cookies{$cookie_name}->value() : undef;
 }
 
-=head2 unload
-
-Flush the session, and leaves the session object blank.
-
-=cut
-
-sub unload {
-    my $self = shift;
-
-    return unless $self->loaded;
-    $self->_session(undef);
-}
-
-=head2 loaded
-
-Returns true if the session has already been loaded.
-
-=cut
-
-sub loaded {
-    my $self = shift;
-    return $self->_session;
-}
-
-sub _session {
-    my $self = shift;
-    $self->{'_session'} = shift if (@_);
-    return ( $self->{'_session'} );
-}
-
 =head2 get KEY [TYPE]
 
 Returns the value for C<KEY> for the current user's session.  C<TYPE>,
@@ -248,41 +210,6 @@ sub remove_all {
     $self->unload;
 }
 
-=head2 set_continuation ID CONT
-
-Stores a continuation in the session.
-
-=cut
-
-sub set_continuation {
-    my $self = shift;
-    $self->set( @_, "continuation" );
-}
-
-=head2 get_continuation ID
-
-Pulls a continuation from the current session. Expects a continuation
-C<ID>.
-
-=cut
-
-sub get_continuation {
-    my $self = shift;
-    $self->get( @_, "continuation" );
-
-}
-
-=head2 remove_continuation ID
-
-Removes a continuation with id C<ID> from the store.
-
-=cut
-
-sub remove_continuation {
-    my $self = shift;
-    $self->remove( @_, "continuation" );
-}
-
 =head2 continuations
 
 Return a hash of all the continuations in this session, keyed by the
@@ -304,62 +231,4 @@ sub continuations {
     return %continuations;
 }
 
-=head2 set_cookie
-
-Sets the session cookie.
-
-=cut
-
-sub set_cookie {
-    my $self = shift;
-
-    # never send a cookie with cached content. misbehaving proxies cause
-    # terrific problems
-    return if Jifty->handler->apache->header_out('Expires');
-
-    my $cookie_name = $self->cookie_name;
-    my %cookies     = CGI::Cookie->fetch();
-    my $cookie      = CGI::Cookie->new(
-        -name    => $cookie_name,
-        -value   => $self->id,
-        -expires => $self->expires,
-    );
-
-    # XXX TODO might need to change under mod_perl
-    if ( not $cookies{$cookie_name}
-        or ( $cookies{$cookie_name} ne $cookie->as_string ) )
-    {
-        Jifty->web->response->add_header(
-            'Set-Cookie' => $cookie->as_string );
-    }
-}
-
-=head2 cookie_name
-
-Returns the current session's cookie_name -- it is the same for all
-users, but varies according to the port the server is running on.
-
-=cut
-
-sub cookie_name {
-    my $self        = shift;
-    my $cookie_name = $self->{'_cookie_name'};
-    my $port        = ( $ENV{'SERVER_PORT'} || 'NOPORT' );
-    $cookie_name =~ s/\$PORT/$port/g;
-    return ($cookie_name);
-}
-
-=head2 expires [VALUE]
-
-Get or set the session's expiration date, in a format expected by
-Cache::Cache.
-
-=cut
-
-sub expires {
-    my $self = shift;
-    $self->set( 'expires' => shift, "metadata" ) if @_;
-    return ( $self->get( 'expires', "metadata" ) );
-}
-
 1;

-----------------------------------------------------------------------


More information about the Jifty-commit mailing list