Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions lib/Plack/Session/Store/Cache.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use Scalar::Util qw[ blessed ];

use parent 'Plack::Session::Store';

use Plack::Util::Accessor qw[ cache ];
use Plack::Util::Accessor qw[ cache prefix ];

sub new {
my ($class, %params) = @_;
Expand All @@ -20,22 +20,25 @@ sub new {
&& $params{cache}->can('set')
&& $params{cache}->can('remove');

bless { %params } => $class;
bless {
prefix => '',
%params,
} => $class;
}

sub fetch {
my ($self, $session_id ) = @_;
$self->cache->get($session_id);
$self->cache->get($self->prefix . $session_id);
}

sub store {
my ($self, $session_id, $session) = @_;
$self->cache->set($session_id => $session);
$self->cache->set($self->prefix . $session_id => $session);
}

sub remove {
my ($self, $session_id) = @_;
$self->cache->remove($session_id);
$self->cache->remove($self->prefix . $session_id);
}

1;
Expand All @@ -61,7 +64,8 @@ Plack::Session::Store::Cache - Cache session store
builder {
enable 'Session',
store => Plack::Session::Store::Cache->new(
cache => CHI->new(driver => 'FastMmap')
cache => CHI->new(driver => 'FastMmap'),
prefix => 'session:',
);
$app;
};
Expand Down Expand Up @@ -89,6 +93,10 @@ exception if that is not the case.

A simple accessor for the cache handle.

=item B<prefix>

The prefix associated with this cache. Defaults to "" if not explicitly set.

=back

=head1 BUGS
Expand Down
49 changes: 48 additions & 1 deletion t/005_basic_w_cache_store.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use t::lib::TestSession;
package TestCache;

sub new {
bless {} => shift;
my $class = shift;
bless +{@_} => $class;
}

sub set {
Expand All @@ -37,6 +38,35 @@ use t::lib::TestSession;
delete $self->{$key};
}
}
{
package TestCachePrefix;
use base 'TestCache';


sub set {
my ($self, $key, $val ) = @_;

Test::More::like $key, qr/^$self->{prefix}/;

$self->{$key} = $val;
}

sub get {
my ($self, $key ) = @_;

Test::More::like $key, qr/^$self->{prefix}/;

$self->{$key};
}

sub remove {
my ($self, $key ) = @_;

Test::More::like $key, qr/^$self->{prefix}/;

delete $self->{$key};
}
}

t::lib::TestSession::run_all_tests(
store => Plack::Session::Store::Cache->new( cache => TestCache->new ),
Expand All @@ -55,5 +85,22 @@ t::lib::TestSession::run_all_tests(
},
);

t::lib::TestSession::run_all_tests(
store => Plack::Session::Store::Cache->new( cache => TestCachePrefix->new(prefix => 'test:'), prefix => 'test:' ),
state => Plack::Session::State->new,
env_cb => sub {
open my $in, '<', \do { my $d };
my $env = {
'psgi.version' => [ 1, 0 ],
'psgi.input' => $in,
'psgi.errors' => *STDERR,
'psgi.url_scheme' => 'http',
SERVER_PORT => 80,
REQUEST_METHOD => 'GET',
QUERY_STRING => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
};
},
);


done_testing;
49 changes: 48 additions & 1 deletion t/005a_basic_w_cache_store.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use t::lib::TestSessionHash;
package TestCache;

sub new {
bless {} => shift;
my $class = shift;
bless +{@_} => $class;
}

sub set {
Expand All @@ -36,6 +37,35 @@ use t::lib::TestSessionHash;
delete $self->{$key};
}
}
{
package TestCachePrefix;
use base 'TestCache';


sub set {
my ($self, $key, $val ) = @_;

Test::More::like $key, qr/^$self->{prefix}/;

$self->{$key} = $val;
}

sub get {
my ($self, $key ) = @_;

Test::More::like $key, qr/^$self->{prefix}/;

$self->{$key};
}

sub remove {
my ($self, $key ) = @_;

Test::More::like $key, qr/^$self->{prefix}/;

delete $self->{$key};
}
}

t::lib::TestSessionHash::run_all_tests(
store => Plack::Session::Store::Cache->new( cache => TestCache->new ),
Expand All @@ -54,5 +84,22 @@ t::lib::TestSessionHash::run_all_tests(
},
);

t::lib::TestSessionHash::run_all_tests(
store => Plack::Session::Store::Cache->new( cache => TestCachePrefix->new(prefix => 'test:'), prefix => 'test:' ),
state => Plack::Session::State->new,
env_cb => sub {
open my $in, '<', \do { my $d };
my $env = {
'psgi.version' => [ 1, 0 ],
'psgi.input' => $in,
'psgi.errors' => *STDERR,
'psgi.url_scheme' => 'http',
SERVER_PORT => 80,
REQUEST_METHOD => 'GET',
QUERY_STRING => join "&" => map { $_ . "=" . $_[0]->{ $_ } } keys %{$_[0] || +{}},
};
},
);


done_testing;