Catalyst-Plugin-Session-Store-BerkeleyDB
view release on metacpan or search on metacpan
lib/Catalyst/Plugin/Session/Store/BerkeleyDB.pm view on Meta::CPAN
$db = $manager->open_db( $db );
}
$app->$_manager($manager);
$app->$_db($db);
return $app->maybe::next::method(@_);
}
sub _data_is_raw {
my ($c, $id, $data) = @_;
return 1 if $id =~ /^expires:/;
return 0;
}
sub get_session_data {
my ($c, $id) = @_;
my $data;
$c->$_manager->txn_do(sub {
my $status = $c->$_db->db_get($id, $data);
confess "BerkeleyDB error while fetching data: $BerkeleyDB::Error ($status)"
if $status && $status != DB_NOTFOUND;
});
if($data) {
if($c->_data_is_raw($id)){
return $data;
}
return thaw($data);
}
return {};
}
sub store_session_data {
my ($c, $id, $data) = @_;
my $frozen = $c->_data_is_raw($id) ? $data : nfreeze($data);
$c->$_manager->txn_do(sub {
$c->$_db->db_put($id, $frozen);
});
}
sub delete_session_data {
my ($c, $id) = @_;
$c->$_manager->txn_do(sub {
$c->$_db->db_del($id);
});
}
sub delete_expired_sessions {
my($c, $id) = @_;
my $manager = $c->$_manager;
my $db = $c->$_db;
$manager->txn_do(sub {
my ($key, $value) = ("", "");
# find out what we need to delete
my %to_delete;
my $all = $db->db_cursor;
while( 0 == $all->c_get( $key, $value, DB_NEXT ) ){
if($key =~ /^expires:(.+)$/){
$to_delete{$1} = 1 if time > $value;
}
}
# then delete all of those
$all = $db->db_cursor;
while( 0 == $all->c_get( $key, $value, DB_NEXT ) ){
my ($name, $id) = split /:/, $key;
$all->c_del() and warn "bye, $key" if $to_delete{$id};
};
});
}
1;
__END__
=head1 NAME
Catalyst::Plugin::Session::Store::BerkeleyDB - store sessions in a berkeleydb
=head1 SYNOPSIS
package YourApp;
use Catalyst qw/Session Session::State::Cookie Session::Store::BerkeleyDB/;
=head1 DESCRIPTION
This module will store Catalyst sessions in a Berkeley database
managed by C<BerkeleyDB::Manager>. Unlike other storage mechanisms,
sessions are never lost before their expiration time.
To cleanup old sessions, you might want to make sure
C<< $c->delete_expired_sessions >> is run periodically.
=head1 CONFIGURATION
You can configure this module in a number of ways. By default, the
module will create a Berkeley database called "catalyst_sessions" in a
directory called "sessions" in your app's temp directory.
You can customize this, though, by setting the values of the "manager"
and "database" keys in C<< $c->config->{'Plugin::Session'} >>.
The C<manager> key can be either an instance of C<BerkeleyDB::Manager>, or
it can be a hash to pass to the constructor of C<BerkeleyDB::Manager>. (Or
it can be empty, and we will use sane defaults.)
The C<database> key can be the result of C<< $manager->open_db( ... )
>>, or it can be a string naming the database. By default, we use
"catalyst_sessions".
Any other keys in the hash will be ignored by this module, but might
be relevant to other session plugins.
=head1 CONTRIBUTING
Patches welcome!
You can get a copy of the repository by running:
$ git clone git://git.jrock.us/Catalyst-Plugin-Session-Store-BerkeleyDB
and you can view the repository in your web browser at:
L<http://git.jrock.us/?p=Catalyst-Plugin-Session-Store-BerkeleyDB.git;a=summary>
( run in 1.463 second using v1.01-cache-2.11-cpan-d7f47b0818f )