|
| 1 | +package Apache::Session::Browseable::Store::Cassandra; |
| 2 | + |
| 3 | +use strict; |
| 4 | + |
| 5 | +use Apache::Session::Store::DBI; |
| 6 | +use Apache::Session::Browseable::Store::DBI; |
| 7 | +use Apache::Session::Browseable::Store::Cassandra; |
| 8 | + |
| 9 | +our @ISA = qw(Apache::Session::Browseable::Store::DBI); |
| 10 | +our $VERSION = '1.3.12'; |
| 11 | + |
| 12 | +our $DataSource = undef; |
| 13 | +our $UserName = undef; |
| 14 | +our $Password = undef; |
| 15 | + |
| 16 | +sub connection { |
| 17 | + my $self = shift; |
| 18 | + my $session = shift; |
| 19 | + |
| 20 | + return if ( defined $self->{dbh} ); |
| 21 | + |
| 22 | + if ( exists $session->{args}->{Handle} ) { |
| 23 | + $self->{dbh} = $session->{args}->{Handle}; |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + my $datasource = $session->{args}->{DataSource} |
| 28 | + || $DataSource; |
| 29 | + my $username = $session->{args}->{UserName} |
| 30 | + || $UserName; |
| 31 | + my $password = $session->{args}->{Password} |
| 32 | + || $Password; |
| 33 | + |
| 34 | + $self->{dbh} = |
| 35 | + DBI->connect( $datasource, $username, $password, { RaiseError => 1 } ) |
| 36 | + || die $DBI::errstr; |
| 37 | + |
| 38 | + #If we open the connection, we close the connection |
| 39 | + $self->{disconnect} = 1; |
| 40 | +} |
| 41 | + |
| 42 | +sub materialize { |
| 43 | + my $self = shift; |
| 44 | + my $session = shift; |
| 45 | + |
| 46 | + $self->connection($session); |
| 47 | + |
| 48 | + local $self->{dbh}->{RaiseError} = 1; |
| 49 | + local $self->{dbh}->{LongReadLen} = |
| 50 | + $session->{args}->{LongReadLen} || 8 * 2**10; |
| 51 | + |
| 52 | + if ( !defined $self->{materialize_sth} ) { |
| 53 | + $self->{materialize_sth} = $self->{dbh}->prepare_cached( |
| 54 | + qq{ |
| 55 | + SELECT a_session FROM sessions WHERE id = ? FOR UPDATE} |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + $self->{materialize_sth}->bind_param( 1, $session->{data}->{_session_id} ); |
| 60 | + $self->{materialize_sth}->execute; |
| 61 | + |
| 62 | + my $results = $self->{materialize_sth}->fetchrow_arrayref; |
| 63 | + |
| 64 | + if ( !( defined $results ) ) { |
| 65 | + die "Object does not exist in the data store"; |
| 66 | + } |
| 67 | + |
| 68 | + $self->{materialize_sth}->finish; |
| 69 | + |
| 70 | + $session->{serialized} = $results->[0]; |
| 71 | +} |
| 72 | + |
| 73 | +sub DESTROY { |
| 74 | + my $self = shift; |
| 75 | + |
| 76 | + if ( $self->{disconnect} ) { |
| 77 | + $self->{dbh}->disconnect; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +1; |
| 82 | + |
| 83 | +=pod |
| 84 | +
|
| 85 | +=head1 NAME |
| 86 | +
|
| 87 | +Apache::Session::Browseable::Store::Cassandra - Store persistent data in a Cassandra database |
| 88 | +
|
| 89 | +=head1 SYNOPSIS |
| 90 | +
|
| 91 | + use Apache::Session::Browseable::Store::Cassandra; |
| 92 | +
|
| 93 | + my $store = new Apache::Session::Browseable::Store::Cassandra; |
| 94 | +
|
| 95 | + $store->insert($ref); |
| 96 | + $store->update($ref); |
| 97 | + $store->materialize($ref); |
| 98 | + $store->remove($ref); |
| 99 | +
|
| 100 | +=head1 DESCRIPTION |
| 101 | +
|
| 102 | +Apache::Session::Browseable::Store::Cassandra fulfills the storage interface of |
| 103 | +Apache::Session. Session data is stored in a Cassandra database. |
| 104 | +
|
| 105 | +=head1 SCHEMA |
| 106 | +
|
| 107 | +To use this module, you will need at least these columns in a table |
| 108 | +called 'sessions': |
| 109 | +
|
| 110 | + id char(32) # or however long your session IDs are. |
| 111 | + a_session lvarchar |
| 112 | +
|
| 113 | +To create this schema, you can execute this command using the sqlplus program: |
| 114 | +
|
| 115 | + CREATE TABLE sessions ( |
| 116 | + id char(32) not null primary key, |
| 117 | + a_session lvarchar |
| 118 | + ); |
| 119 | +
|
| 120 | +If you use some other command, ensure that there is a unique index on the |
| 121 | +table's id column. |
| 122 | +
|
| 123 | +=head1 CONFIGURATION |
| 124 | +
|
| 125 | +The module must know what datasource, username, and password to use when |
| 126 | +connecting to the database. These values can be set using the options hash |
| 127 | +(see Apache::Session documentation). The options are DataSource, UserName, |
| 128 | +and Password. |
| 129 | +
|
| 130 | +Example: |
| 131 | +
|
| 132 | + tie %hash, 'Apache::Session::Cassandra', $id, { |
| 133 | + DataSource => 'dbi:Cassandra:database', |
| 134 | + UserName => 'database_user', |
| 135 | + Password => 'K00l' |
| 136 | + }; |
| 137 | +
|
| 138 | +Instead, you may pass in an already-opened DBI handle to your database. |
| 139 | +
|
| 140 | + tie %hash, 'Apache::Session::Cassandra', $id, { |
| 141 | + Handle => $dbh |
| 142 | + }; |
| 143 | +
|
| 144 | +The last option is LongReadLen, which specifies the maximum size of the session |
| 145 | +object. If not supplied, the default maximum size is 8 KB. |
| 146 | +
|
| 147 | +=head1 AUTHOR |
| 148 | +
|
| 149 | +This module was written by Mike Langen <mike.langen@tamedia.ch>, based |
| 150 | +on the original for Oracle. |
| 151 | +
|
| 152 | +=head1 SEE ALSO |
| 153 | +
|
| 154 | +L<Apache::Session>, L<Apache::Session::Store::DBI> |
| 155 | +1; |
| 156 | +
|
0 commit comments