Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support get_cache callback #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
48 changes: 37 additions & 11 deletions lib/Plack/Session/Store/Cache.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,46 @@ use Scalar::Util qw[ blessed ];

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

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

sub new {
my ($class, %params) = @_;

die('cache require get, set and remove method.')
unless blessed $params{cache}
&& $params{cache}->can('get')
&& $params{cache}->can('set')
&& $params{cache}->can('remove');
my $cache;
if ( $params{get_cache} ) {
$cache = $params{get_cache}->();
}
else {
$cache = $params{cache};
$params{get_cache} = sub { $params{cache} };
}

bless { %params } => $class;
die('cache require get, set and remove method.')
unless blessed $cache
&& $cache->can('get')
&& $cache->can('set')
&& $cache->can('remove');

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


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

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

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

1;
Expand Down Expand Up @@ -66,6 +79,15 @@ Plack::Session::Store::Cache - Cache session store
$app;
};

# set get_cache callback for ondemand
builder {
enable 'Session',
store => Plack::Session::Store::Cache->new(
get_cache => sub { MyAppSingleton->cache }
);
$app;
};

=head1 DESCRIPTION

This will persist session data using any module which implements the
Expand All @@ -81,14 +103,18 @@ its full interface.

=item B<new ( %params )>

The constructor expects the I<cache> param to be an object instance
The constructor expects the I<cache> param or return of I<get_cache> to be an object instance
which has the I<get>, I<set>, and I<remove> methods, it will throw an
exception if that is not the case.

=item B<cache>

A simple accessor for the cache handle.

=item B<get_cache>

A callback for the cache handle.

=back

=head1 BUGS
Expand Down
18 changes: 18 additions & 0 deletions t/005_basic_w_cache_store.t
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,23 @@ t::lib::TestSession::run_all_tests(
},
);

my $cache = TestCache->new;
t::lib::TestSession::run_all_tests(
store => Plack::Session::Store::Cache->new( get_cache => sub { $cache } ),
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;
18 changes: 18 additions & 0 deletions t/005a_basic_w_cache_store.t
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,23 @@ t::lib::TestSessionHash::run_all_tests(
},
);

my $cache = TestCache->new;
t::lib::TestSessionHash::run_all_tests(
store => Plack::Session::Store::Cache->new( get_cache => sub { $cache } ),
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;