Skip to content

Commit

Permalink
Update session_set() to just take a new session
Browse files Browse the repository at this point in the history
  • Loading branch information
chilts committed Dec 28, 2010
1 parent d7cc1d8 commit 586f912
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions lib/CGI/Conduit/Session.pm
Expand Up @@ -13,6 +13,8 @@ with 'CGI::Conduit::Log';

use Log::Log4perl qw(get_logger);

my $log = get_logger();

## ----------------------------------------------------------------------------

has 'session_id' => ( is => 'rw' );
Expand All @@ -29,8 +31,6 @@ sub session {
my $cookie = $self->cookie_get('session');
return unless defined $cookie;

my $log = get_logger();

# get the cookie value which is the session id and check it for validity
my $id = $cookie->value();
unless ( $self->is_session_id_valid($id) ) {
Expand Down Expand Up @@ -63,21 +63,20 @@ sub is_session_id_valid {
return;
}

# Note: if make a session twice, the second Cookie will override the first and
# the first session will be inaccessible
sub session_new {
my ($self, $value) = @_;

unless ( $value ) {
my $log = get_logger();
$log->fatal(qq{Trying to set a session to undef});
croak qq{Trying to set a session to undef};
}

my $log = get_logger();

my $id = id(32);
my $mc = $self->memcache();
unless ( $mc->set("session:$id", $value) ) {
$log->warn("session_new(): Trying to set session $id failed");
$log->warn("session_new(): Trying to set new session '$id' failed");
return;
}

Expand All @@ -91,31 +90,42 @@ sub session_new {
}

sub session_set {
my ($self, $id, $value) = @_;
my ($self, $value) = @_;

unless ( $self->session ) {
$log->fatal(qq{Trying to set a session that doesn't yet exist});
croak qq{Trying to set a session that doesn't yet exist};
}

# must be there if $self->session() succeeded
my $id = $self->session_id();

unless ( $value ) {
my $log = get_logger();
$log->fatal(qq{Trying to set a session to undef});
croak qq{Trying to set a session to undef};
}

my $mc = $self->memcache();
$mc->set("session:$id", $value);
unless ( $mc->set("session:$id", $value) ) {
$log->warn("session_set(): Trying to set session '$id' failed");
return;
}

# remember this new session
$self->{session} = $value;
}

sub session_get {
my ($self, $id) = @_;

# no need to check for a valid session id since that's already been done,
# so just return the session if it's there
# just return the session if it's there
return $self->memcache()->get( qq{session:$id} );
}

sub session_del {
my ($self) = @_;

unless ( $self->session ) {
my $log = get_logger();
$log->fatal("session_del(): trying to delete a session which doesn't (yet) exist");
croak "session_del(): trying to delete a session which doesn't (yet) exist";
}
Expand Down

0 comments on commit 586f912

Please sign in to comment.