Skip to content

support expires in Store::Cache #12

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

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
13 changes: 10 additions & 3 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 expires ];

sub new {
my ($class, %params) = @_;
Expand All @@ -30,7 +30,7 @@ sub fetch {

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

sub remove {
Expand Down Expand Up @@ -61,7 +61,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'),
expires => 86400,
);
$app;
};
Expand Down Expand Up @@ -89,6 +90,12 @@ exception if that is not the case.

A simple accessor for the cache handle.

=item B<expires>

This value uses in set method, Like this

$cache->set($key, $data, $expires)

=back

=head1 BUGS
Expand Down
63 changes: 62 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,32 @@ use t::lib::TestSession;
delete $self->{$key};
}
}
{
package TestCacheMatchExpires;
use base 'TestCache';

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

Test::More::is $self->{expires} => $expires;

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

}
{
package TestCacheDenyExpires;
use base 'TestCache';

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

Test::More::is $expires => undef;

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

}

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

t::lib::TestSession::run_all_tests(
store => Plack::Session::Store::Cache->new( cache => TestCacheMatchExpires->new(expires => 111), expires => 111 ),
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] || +{}},
};
},
);

t::lib::TestSession::run_all_tests(
store => Plack::Session::Store::Cache->new( cache => TestCacheDenyExpires->new ),
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;
62 changes: 61 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,32 @@ use t::lib::TestSessionHash;
delete $self->{$key};
}
}
{
package TestCacheMatchExpires;
use base 'TestCache';

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

Test::More::is $self->{expires} => $expires;

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

}
{
package TestCacheDenyExpires;
use base 'TestCache';

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

Test::More::is $expires => undef;

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

}

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

t::lib::TestSessionHash::run_all_tests(
store => Plack::Session::Store::Cache->new( cache => TestCacheMatchExpires->new(expires => 111), expires => 111 ),
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] || +{}},
};
},
);

t::lib::TestSessionHash::run_all_tests(
store => Plack::Session::Store::Cache->new( cache => TestCacheDenyExpires->new ),
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;